views:

719

answers:

3

Is keeping JMS connections / sessions / consumer always open a bad practice?

Code draft example:

// app startup code

ConnectionFactory cf = (ConnectionFactory)jndiContext.lookup(CF_JNDI_NAME); Connection connection = cf.createConnection(user,pass); Session session = connection.createSession(true,Session.TRANSACTIONAL); MessageConsumer consumer = session.createConsumer(new Queue(queueName)); consumer.setMessageListener(new MyListener()); connection.start(); connection.setExceptionListener(new MyExceptionHandler()); // handle connection error

// ... Message are processed on MyListener asynchronously ...

// app shutdown code

consumer.close(); session.close(); connection.close();

Any suggestions to improve this pattern of JMS usage?

Thanks.

A: 

That is a very common and acceptable practice when dealing with long lived connections. For many JMS servers it is in fact preferable to creating a new connection each time it is needed.

John Meagher
+1  A: 

Agreed. Here are some good tips on how to use JMS efficiently which includes keeping around connections/sessions/producers/consumers.

You might also want to check the recommendation on using transactions too if you are interested in maximising performance.

James Strachan
A: 

In our app, we will have connections/sessions/consumers/producers open for months at a time. We've had to work with our vendor (BEA) to make that work reliably. But any troubles with that is a bug the vendor needs to fix.

John M