Skip to content

JMS Exception listeners (How and Why)

February 21, 2011

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.

Advertisement

From → Uncategorized

Leave a Comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.