tags:

views:

32

answers:

1

I'm trying to write a multithreaded implementation for JMS message processing from a queue.

I've tried with DefaultMessageListenerContainer and SimpleMessageListenerContainer classes.

The problem I have is that it seems like just a single instance of the MessageListener class gets ever instantiated, no matter how I configure it. This forces me to unnecessarily write stateless or thread-safe MessageListener implementations, since I have the ListenerContainer configured to use multiple threads (concurrentConsumers=8).

Is there an obvious solution to this that I'm overlooking?

+1  A: 

This is by design. The MessageListener is a dependency that you inject into Spring - it has no way of instantiating new ones.

This forces me to unnecessarily write stateless or thread-safe messageListener implementations

You make that sound like a bad thing. Making your MessageListener is a very good idea, Spring just removes the temptation to do otherwise.

skaffman
I don't understand. I am making my MessageListener. I just want it to take advantage of my machine's multi-cores to process things in parallel.
Iker Jimenez
@Iker: Yes, and the MessageListenerContainer will spawn multiple threads to do this, with all threads using the same instance of your `MessageListener`. Spawning multiple listeners is inefficient and unnecessary.
skaffman
@skaffman: Ok, that I understand. But lets say that for each JMS message you have to process a job that takes a few seconds. I want to be able to use a completely separate set of instances to process each of the messages. Should I spawn threads from the MessageListener then?
Iker Jimenez
@Iker: You can do that if you choose, yes.
skaffman