You close the JMS connection when you are finished using it and the objects used must be released. You can choose either to manually close the sessions associated with the connection and then the connection itself, or to close the connection object only. In the last case, the JMS Provider automatically closes all associated sessions.
We recommend that you unsubscribe the durable subscribers when you no longer need them. The JMS Provider keeps all the messages that durable subscribers have not yet consumed, so unneeded ones can waste a significant amount of resources and degrade JMS Provider performance.
You can handle the connection exceptions using your own implementation of the class javax.jms.ExceptionListener.
When you implement the onException() method, do not close the client connections that were used up to that moment in the method’s implementation. The JMS Provider does this for you. The client has to recreate the JMS resources of the application.
If you are using a point-to-point connection, use the following code to close the connection object:
// Close the queue connection queueConnection.close(); |
If you are using a publish-subscribe connection, close the connection in the same way:
// Close the topic connection topicConnection.close(); |
You have to implement the onException() method of the ExceptionListener, and in case an exception occurs this will allow you to execute custom logic to avoid data loss.
For example:
/** * Handle asynchronous errors with the connection. */ public void onException(javax.jms.JMSException jsme) { // Tries to reconnect } |
To register the implementation use:
// Register this class as the exception listener. connection.setExceptionListener( (javax.jms.ExceptionListener) ExceptionListenerImpl); |