messaginghub / pooled-jms

A JMS Connection pool for messaging applications supporting JMS 1.1 and 2.0 and Jakarta JMS clients
Apache License 2.0
50 stars 24 forks source link

ExceptionHandling Edge Case #16

Closed EnlightenedOne closed 4 years ago

EnlightenedOne commented 4 years ago

If using spring boot and wrapping a connection factory as a bean that is referenced in a camel context the PooledConnection ends up destroying the provided exceptionListener.

@Bean
public ConnectionFactory consumerConnectionFactory(ActiveMqProperties properties) {
    JmsPoolConnectionFactory poolConnectionFactory = new JmsPoolConnectionFactory();

    JmsConnectionFactory jmsConnectionFactory = new JmsConnectionFactory(
            properties.getConsumer().getUsername(),
            properties.getConsumer().getPassword(),
            properties.getConsumer().getBrokerUrl()
    );

    poolConnectionFactory.setConnectionFactory(jmsConnectionFactory);

    // This will never run because its overwritten when the PooledConnection is instantiated:
    ExceptionListener exceptionListener = new ExceptionListener() {
        @Override
        public void onException(JMSException arg0) {
            System.out.println("Sample" + arg0.getMessage());
        }
     };

     jmsConnectionFactory.setExceptionListener(exceptionListener);

    return poolConnectionFactory;
}

You can fix this erasure by preserving the original exception listener in the PooledConnection constructor: public PooledConnection(Connection connection) { final GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig<>(); poolConfig.setJmxEnabled(false); this.connection = wrap(connection); try { this.setParentExceptionListener(connection.getExceptionListener()) this.connection.setExceptionListener(this);

Otherwise the connections have to be taken out of the pool and updated manually with an exception handler inbetween spring bean instantiation and camel route configuration.