messaginghub/pooled-jms

ExceptionHandling Edge Case

EnlightenedOne opened this issue · 0 comments

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.