Tuesday, February 3, 2009

JMS ExceptionListener

The JMS API JavaDoc specified the ExceptionListener interface behavior as follows.

"If a JMS provider detects a serious problem with a Connection object, it informs the Connection object's ExceptionListener, if one has been registered. It does this by calling the listener's onException method, passing it a JMSException argument describing the problem.

An exception listener allows a client to be notified of a problem asynchronously. Some connections only consume messages, so they would have no other way to learn that their connection has failed.

A JMS provider should attempt to resolve connection problems itself before it notifies the client of them."

In practice, when an exception listener is called, the connection is broken and the JMS service is no longer available for the connection.

An ExceptionListener can be used for the following purposes.

1. This is the only standard API that a JMS message listener can be notified of a connection problem. When it is called, the JMS service is no longer available. An application can, at a minimum, log the problem and clean up its resources. Application can also notify any interest parties that needs to be notified of such a problem.

2. An application can, if necessary, implement its own simple high-availability functionality when an ExceptionListener is used. If an application is designed with a clean initialization setup, it would be feasible to reinitialize the JMS objects when the exception listener is called. The reinitialization process can even include (re)connecting to a list of pre-configured messaging servers. This simple JMS service-availability feature can be implemented with just a little effort.

An ExceptionListener should always be used in a JMS application, this is especially true for an application with only asynchronous message consumers.


  1. Nice, I just found out about this because one of our customers encountered problems and we hadn't implemented an exception listener. Yikes....

  2. Cool post.. It helped me...