Closing Connections on asynchronous messaging in JMS

Posted by The Elite Gentleman on Stack Overflow See other posts from Stack Overflow or by The Elite Gentleman
Published on 2010-03-19T10:05:54Z Indexed on 2010/03/19 10:21 UTC
Read the original article Hit count: 337

Filed under:
|
|
|
|

Hi Everyone!

I have created a JMS wrapper (similar to Springs JmsTemplate since I'm not using Springs) and I was wondering: If I setup asynchronous messaging, when is a good time to close connections and JMS relates resources (so that the Connection Factory in the Resource Pool can be available)?

Thanks

Here's the source code for receiving JMS messages

public Message receive() throws JMSException {
        QueueConnection connection = null;
        QueueSession session = null;
        QueueReceiver consumer = null;

        try {
            // TODO Auto-generated method stub
            connection = factory.createQueueConnection();
            if (connection != null && getExceptionListener() != null) {
                connection.setExceptionListener(getExceptionListener());
            }
            session = connection.createQueueSession(isSessionTransacted(), getAcknowledgeMode());
            consumer = session.createReceiver(queue);
            if (getMessageListener() != null) {
                consumer.setMessageListener(getMessageListener());
            }
            //Begin
            connection.start();
            if (getMessageListener() == null) {
                return null;
            }

            return receive(session, consumer);
        } catch (JMSException e) {
            // TODO: handle exception
            logger.error(e.getLocalizedMessage(), e);
            throw e;
        } finally {
            JMSUtil.closeMessageConsumer(consumer);
            JMSUtil.closeSession(session, false);  //false = don't commit.
            JMSUtil.closeConnection(connection, true); //true = stop before close.
        }

As you can see, if getMessageListener() != null then apply it to the MessageConsumer. Am I doing this correctly?

The same approach has also been taken for JMS Topic.

© Stack Overflow or respective owner

Related posts about java

Related posts about jms