How to create thread safe MessageListeners in DefaultMessageListenerContainer

791 Views Asked by At

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.

1

There are 1 best solutions below

2
On BEST ANSWER

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.