简体   繁体   English

JMS,仅使用一次来自主题的消息

[英]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.

相关问题 每个主题在集​​群中运行的每个侦听器仅消耗一次消息 - Consume message only once from Topic per listeners running in cluster 一旦客户端确认消息在 JMS 主题上的可用性? - Message availability on JMS Topic once client acknowledges it? JMS主题中缺少消息 - Missing Message from JMS Topic 骆驼JMS:使用主题中的消息(是xml)并调用Java bean并将xml对象传递给它 - Camel JMS : consume a message(is an xml) from topic and invoke a java bean and pass the xml object to it 尝试使用 Spring 引导使用 JMS 主题消息时出现异常 - Exception while trying to consume a JMS Topic message using Spring Boot JMS监听器关闭2小时,然后从主题启动如何恢复消息 - JMS is listener is down for 2 hour then How recover message once it is up from topic JMS无法使用来自Oracle队列的消息 - JMS unable to consume message from oracle queue 为什么我的JMS客户端不使用主题中的消息? - Why does my JMS client not consume messages from the Topic? 如何从另一个应用程序使用 ActiveMQ Artemis jms 主题 Java - How to consume ActiveMQ Artemis jms topic from another application Java ActiveMQMessageConsumer每5秒仅从Java主题上收到一次消息 - ActiveMQMessageConsumer receives message only once in 5 seconds from Topic on Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM