Is the JMS QueueSender thread safe?

2019-04-04 03:10发布

问题:

I want to use a QueueSender in a multi-threaded environment.

Is QueueSender.send() thread safe?

回答1:

No, a MessageProducer/QueueSender is not thread safe.

Or more specifically: The Session is not thread safe. The JavaDoc for Session explicitly mentions this in its first sentence:

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

And since a MessageProducer/QueueSender is bound to a Session you must not use it from more than one thread at the same time. In fact you must not use it from two different threads at different times either!



回答2:

Following screenshot is from JMS2 specs

As you can see Session Object does not support usage by concurrent threads. Session is not a thread safe Object. Same gos for all Objects created from that Session instance like Messages,Producers,Consumers. So these objects must not be shared by two threads and this is something client should take care of instead of JMS providers.