简体   繁体   中英

Spring JMS Message Listener Container

I'm new to JMS and I am working on setting up ActiveMQ with Tomcat 6 and Spring. I have most of the basic things configured however I'm a little bit confused with the Message Listener Containers that Spring provides. Reading the documentation it sounds like the Message Listener Container is used to "handle" subscribing (I'm working with Topics), unsubscribing and delivering the message to the listener. I not sure if I'm thinking about this correctly. If this is the case I don't see any documentation on how I would have multiple classes subscribe to the same topic using the Message Listener Container. I see that you can set the messageListener property but that would only allow one class to be able to subscribe to a topic. It doesn't seem right to create another instance of the same Message Listener Container just to have another subscriber (Message Driven POJO) listen to the same topic.

Can anyone shed some light on this for me? I think I may be a little confused.

Thanks in advance!

If you're familiar with Message-Driven EJBs, then a Spring MessageListenerContainer is effectively replacement for an MDB. It gets its name because it's wired up with the JMS topic/queue, as well as a single JMS MessageListener, and it pulls messages off that topic/queue and feeds them to your MessageListener.

You're quite right that only one MessageListener can be registered with each container at a time, but consider that while the MessageListenerContainer code can be quite complex, it's actually a very lightweight runtime component. Don't be afraid to create several instances of it.

Also, make sure you pick the appropriate MessageListener implementation for your situation. The Simple and Default implementation are really quite different, but neither is "better".

Guys are correct that multiple Containers would help you, but it's look like wrong approach. Since JMS goal is to integrate different application/services rather than internal application processes (several classes you talking about). I would suggest push message to different classes yourself after receiving them by Message Listener. Otherwise you take a look to frameworks like Apache Camel or Spring Integration.

The container is a wrapper of sorts to "adapt" any POJO to receiving messages from a Queue or Topic. If you desire multiple listeners, you will end up with multiple containers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM