Vidya_85
Vidya_85

Reputation: 77

How to create thread safe MessageListeners in DefaultMessageListenerContainer

My MessageListener implementation is not thread safe.

This causes issues when i try to wire it in DefaultMessageListenerContainer with multiple consumers, since, all the consumers share the same MessageListener object.

Is there a way to overcome this problem by making the DefaultMessageListener container create multiple instances of MessageListeners, so that, MessageListener is not shared among consumer threads.

In that way each consumer thread will have its own MessageListener instance.

Please advise.

Upvotes: 0

Views: 826

Answers (1)

Gary Russell
Gary Russell

Reputation: 174759

There's nothing built in to support this. It is generally considered best practice to make services stateless (and thus thread-safe).

If that's not possible, you would need to create a wrapper listener; two simple approaches would be to store instances of your listener in a ThreadLocal or maintain a pool of objects and retrieve/return instances from/to the pool on each message.

Upvotes: 1

Related Questions