JMS Exception listeners (How and Why)
Hi folks,
Exception listeners are used to deliver connection related exceptions to the program. It is important to understand that all kind of exceptions will not be delivered to exception listeners. Only the exceptions no where else to throw
.
The implementation is simple. Sub class the javax.jms.ExceptionListener interface.
Implement the onException method.
Register the exception listener to JMS connection object.
Sample code is provided below.
public class JMSExceptionListener implements ExceptionListener {
JMSConnector jmsConnection;
public JMSExceptionListener(JMSConnection jmsConnecion) {
this.jmsConnection = jmsConnection;
}
public void onException(JMSException e)
{ e.printStackTrace();
jmsConnection.restart();
}
}
Here I restart the JMS connection in case of an exception. This is considered as a best practice since most of the exceptions delivered in to the Exception Listeners are connections related Exceptions.
Registering the Exception listener on jms connection.
queueConnection = queueConnectionFactory.createQueueConnection();
queueConnection.setExceptionListener(new JMSExceptionListener(jmsConnection));
queueConnection.start();
Regards,
Lasith.