2025-01-20 10:00:03

JMS主题vs队列

我想知道JMS队列和JMS主题之间的区别是什么。

ActiveMQ页面说

Topics In JMS a Topic implements publish and subscribe semantics. When you publish a message it goes to all the subscribers who are interested - so zero to many subscribers will receive a copy of the message. Only subscribers who had an active subscription at the time the broker receives the message will get a copy of the message. Queues A JMS Queue implements load balancer semantics. A single message will be received by exactly one consumer. If there are no consumers available at the time the message is sent it will be kept until a consumer is available that can process the message. If a consumer receives a message and does not acknowledge it before closing then the message will be redelivered to another consumer. A queue can have many consumers with messages load balanced across the available consumers.

我想要有一个“东西”,它将以与ActiveMQ代理接收消息时相同的顺序将消息的副本发送到每个订阅者。

任何想法吗?


当前回答

道理很简单:

队列=插入>提取(发送到单个用户)1:1 主题=插入>广播(发送给所有订阅用户)1:n

其他回答

队列是JMS托管对象,用于保存等待订阅者消费的消息。当所有订阅者都使用该消息时,消息将从队列中删除。

主题是当消息发布时,主题的所有订阅者都会收到相同的消息。

道理很简单:

队列=插入>提取(发送到单个用户)1:1 主题=插入>广播(发送给所有订阅用户)1:n

如果有N个消费者,那么:

JMS主题将消息传递给N中的N个 JMS队列将消息传递给N中的1

您说过,您正在“寻找一个‘东西’,它将按照ActiveMQ代理接收消息时的顺序向每个订阅者发送消息的副本”。

因此,您希望使用Topic,以便所有N个订阅者都能获得消息的副本。

队列

Pros

带有透明通信流的简单消息传递模式 可以通过将消息放回队列来恢复消息

Cons

只有一个消费者可以得到消息 暗示生产者和消费者之间的耦合,因为它是一对一的关系

主题

Pros

多个消费者可以得到一条消息 生产者和消费者之间的解耦(发布-订阅模式)

Cons

更复杂的通信流程 不能为单个侦听器恢复消息

这意味着一个话题是合适的。队列意味着消息发送到一个且只有一个可能的订阅者。主题将发送给每个订阅者。