[英]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.我们使用带有 spring 引导 JMS 的 IBM MQ。 Is this something doable?这是可行的吗?
JMS topics, generally speaking, provide publish-subscribe semantics in which every consumer/subscriber gets every message sent to the topic.一般而言,JMS 主题提供发布-订阅语义,其中每个消费者/订阅者都获取发送到该主题的每条消息。 However, there is a way to get point-to-point semantics with multiple consumers on a JMS topic - shared subscriptions.但是,有一种方法可以在一个 JMS 主题上获得多个消费者的点对点语义——共享订阅。
Shared subscriptions are available from JMS 2 onward.共享订阅从 JMS 2 开始可用。 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()
.创建共享订阅的最简单方法是使用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 .这将创建一个共享的非持久订阅,并且与创建持久订阅设置不同的是,连接上的客户端 ID 是可选的。 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.简而言之,只要所有消费者都使用相同的订阅名称创建共享订阅,那么您就可以获得所需的点对点语义。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.