Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

Exception Listeners

Many messaging applications include consumers who receive messages and perform work but never send any JMS messages. Because these clients are only active when a message is delivered, there is no means to notify them when their JMS Server has failed. JMS provides the ExceptionListener facility to deliver these out-of-band exceptions to clients.

A JMS ExceptionListener asynchronously receives connection error exceptions. ExceptionListener only receives exceptions that cannot be thrown to a method call. For instance, a send call on a broken connection will receive a JMSException, but this exception will not be transmitted to the ExceptionListener.

ExceptionListeners implement javax.jms.ExceptionListener that contains the onException(JMSException) method. When an out-of-band exception occurs, the ExceptionListener's onException method receives the raised exception. ExceptionListeners are not written to be thread-safe or reentrant. A new exception will not be delivered until the ExceptionListener's onException method has returned.

  public class MyExceptionListener 
   implements javax.jms.ExceptionListener
   public void onException(javax.jms.JMSException e) {

    ... //handle exception

A client registers an ExceptionListener on the JMS connection with the setExceptionListener(ExceptionListener) method.

  qConnection.setExceptionListener(new MyExceptionListener());

WebLogic's JMS implementation also enables the ExceptionListener to be registered with the entire JMS session by using WLSession's setExceptionListener(ExceptionListener) method.

  import weblogic.jms.extensions.WLSession;

  WLSession wlSession = (WLSession) session;

  wlSession.setExceptionListener(new ExceptionListener());
  • + Share This
  • 🔖 Save To Your Account