The exception listener on JMS connections is never called

Since we were having a few issues with memory leaks while handling JMS connections, we wanted to setup am Exception Listener (using the setExceptionListener method of the connection) to handle a connection failure and reconnect. Especially, if you consume messages asynchronously, this seems like a good way to learn that your connection has failed and reconnect.

Unfortunately, we never were notified although we could clearly see that the connection failed.

The problem is that we were in a JBoss container and as the J2EE specification says:

This method must not be used in a Java EE web or EJB application. Doing so may cause a JMSException to be thrown though this is not guaranteed.

This is actually true not only for setExceptionListener but for all the following methods of the JMS Connection class:

  • createConnectionConsumer
  • createSharedConnectionConsumer
  • createDurableConnectionConsumer
  • createSharedDurableConnectionConsumer
  • setClientID
  • setExceptionListener
  • stop

The first four of them are not allowed because of restrictions on the use of threads in the container. The other ones because they may interfere with the
connection management functions of the container.

In the past this was not always enforced by J2EE container vendors but in order to pass the compatibility test suites, they have started really enforcing this policy. So with many containers, you will get an IllegalStateException or a JMSException when invoking setExceptionListener (which states that you’re not allowed to call it).

Leave a Reply

Your email address will not be published.