Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using a JMS Session from different threads

From the javadoc for Session it states:

A Session object is a single-threaded context for producing and consuming messages.

So I understand that you shouldn't use a Session object from two different threads at the same time. What I'm unclear on is if you could use the Session object (or children such as a Queue) from a different thread than the one it created.

In the case I'm working on, I'm considering putting my Session objects into a pool of available sessions that any thread could borrow from, use, and return to the pool when it is finished with it.

Is this kosher?

(Using ActiveMQ BTW, if that impacts the answer at all.)

like image 624
Evan Avatar asked Mar 22 '10 08:03

Evan


1 Answers

I think the footnote from section 4.4 in the JMS 1.1 spec sheds some light:

There are no restrictions on the number of threads that can use a Session object or those it creates. The restriction is that the resources of a Session should not be used concurrently by multiple threads. It is up to the user to insure that this concurrency restriction is met. The simplest way to do this is to use one thread. In the case of asynchronous delivery, use one thread for setup in stopped mode and then start asynchronous delivery. In more complex cases the user must provide explicit synchronization.

By my reading of the spec what you want to do is OK, provided you correctly manage concurrency.

like image 140
T.Rob Avatar answered Oct 13 '22 23:10

T.Rob