Skip to content
Advertisement

JMS, consume a message from a topic only once

I have a requirement to establish a point to point communication with publisher and consumer in my application. What I’m given is a topic. So I should make sure the message in the topic will be consumed only once and only one instance should consume it. (There are multiple instances of the consumer.)

I understand message queue is the solution for the above requirement. But I will have to work with the topic given instead.

I tried one sample application and my all my consumer instances consumed the message in the topic. I planned to use a table to keep a track of message processing but it does not seem like a good solution. We use IBM MQ with spring boot JMS. Is this something doable?

Advertisement

Answer

JMS topics, generally speaking, provide publish-subscribe semantics in which every consumer/subscriber gets every message sent to the topic. However, there is a way to get point-to-point semantics with multiple consumers on a JMS topic – shared subscriptions.

Shared subscriptions are available from JMS 2 onward. When using this feature multiple consumers/subscribers can share the same subscription and only one of those consumers will receive a given message.

The simplest way to create a shared subscription is by using javax.jms.Session.createSharedConsumer(). This will create a shared, non-durable subscription and unlike when creating a durable subscription setting the client ID on the connection is optional. There are other related methods for creating shared, durable subscriptions, using selectors, etc.

In short, as long as all the consumers are creating a shared subscription using the same subscription name then you can get the point-to-point semantics you need.

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement