0

How to use concurrency in jms topics using spring? Like for queues we can set maxConcurrentConsumers properties to the desired number. But if I do that for a JMS topic, it leads to concurrent consumption of the same message, which is not desirable.

Can somebody please point me in the right direction?

2
  • That's how topics work. You are using the wrong kind of JMS destination for your purpose. Use a JMS Queue destination instead. Apr 17, 2014 at 8:00
  • Then why does ejb allow that? Actually I am migrating an existing ejb project to spring and Thats how its done in ejb. Apr 17, 2014 at 8:10

2 Answers 2

0

Well you said it yourself in a comment, but just to elaborate.

Inside your listener, you can still create multiple threads and execute the processing in an asynchronous way. As soon as you get your messages dispatch it to a asynchronous consumer. This is fairly easily implemented through some sort of executor for example.

I would really go for the guava's EventBus. It is so simple, yet so easy to set up and use. But this is your choice really.

0

You are using the DefaultMessageListenerContainer in a wrong way. From the docs:

Note that dynamic scaling only really makes sense for a queue in the first place; for a topic, you will typically stick with the default number of 1 consumer, else you'd receive the same message multiple times on the same node.

To help your situation you may forward messages from the topic to a queue and let your consumers consume from this queue. If your JMS provider has some forwarding feature, than this is merely a configuration change to your application.

2
  • Correct! But our application is huge and we get thousands of messages per hour!! Also, there are 5 different topics that point to the same listener class. In that case particularly, concurrency is needed to make the processing quick. Since there is no way I can achieve that through topics, I'm thinking of making changes in the code of my listener class (the one which listens to 4 different topics). For now, I am considering using a task executor which implements some sort of threading logic to internally manage the processing. Please correct me if I'm wrong. Thanks!! :) Apr 17, 2014 at 10:03
  • @user3544133 thousands of messages per hour you do realize that is actually very little, right?
    – Eugene
    Apr 17, 2014 at 13:23

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.