16

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

Is QueueSender.send() thread safe?

2 Answers 2

14

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!

1
  • 1
    You can use it from multiple threads if you synchronize usage. The JMS specification says :'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'
    – olaf
    Commented May 17, 2019 at 10:49
13

Following screenshot is from JMS2 specs

enter image description here

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.