activemq-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christian Schneider <>
Subject Re: Best practices with JMS
Date Fri, 04 Mar 2011 17:19:16 GMT
Hi Filip,

basically connections can be kept open as long as you like. They are 
also thread safe. For connections there is the problem that they can 
become invalid if the server goes away for too long.
Sessions can also be kept open but they are not thread safe. So make 
sure you do not share a session between threads.

I suggest that you take a look at the spring jms abstractions. They help 
with a lot of the problems you may have.
The keywords you need JmsTemplate, DefaultMessageListenerContainer.

Be careful though. The spring jmstemplate always closes the connection 
after a request. So you have to use a pooling connection factory. 
Application servers sometimes provide these.
When doing a standalone application you may want to wrap the connection 
factory in a spring CachingConnectionFactory.

Frameworks like Camel and CXF use this aproach as it makes life a lot 
easier than working with JMS directly.

If your focus is on simply sending and receiving your objects in a 
really easy way you might even want to use Apache Camel.
Messaging canĀ“t be easier than with camel and it embeds nicely in your 

Best regards


Am 04.03.2011 18:07, schrieb Filip Nguyen:
> I am using ActiveMQ and I am reading Java Message Service document 
> which specifies the JMS JSR. However I cant find anywhere what are the 
> best practices for Connection length. From the JMS JSR I understand that:
> Connection is Heavy weight object as JMS JSR suggests. So its ok to 
> create it at start of the application and release it just before 
> application closes
> Going further the Session is supposed to be lightweight object so 
> there should be no problem to create it more often. Butas indicated 
> here: 
> there is James.Strachan suggests to use 1 consumer for whole 
> application to aovid receivenowait problems.
> My questions are (assuming I want to avoid requestNoWait problems and 
> i dont want to call receive(1000)):
>   1. How long should connection be opened (after calling start()) and 
> are there
>      any problems with having it opened for a long long time?
>   2. How long should one Session be opened? (this relates to 
> MessageConsumer
>      because I assume the consumer needs to have session opened).
> Filip Nguyen


View raw message