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代理接收消息时相同的顺序将消息的副本发送到每个订阅者。

任何想法吗?


当前回答

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

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

其他回答

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

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

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

队列

Pros

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

Cons

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

主题

Pros

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

Cons

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

道理很简单:

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

A JMS topic is the type of destination in a 1-to-many model of distribution. The same published message is received by all consuming subscribers. You can also call this the 'broadcast' model. You can think of a topic as the equivalent of a Subject in an Observer design pattern for distributed computing. Some JMS providers efficiently choose to implement this as UDP instead of TCP. For topic's the message delivery is 'fire-and-forget' - if no one listens, the message just disappears. If that's not what you want, you can use 'durable subscriptions'.

JMS队列是1对1的消息目的地。消息仅由一个消费接收者接收(请注意:始终使用订阅者作为“主题客户端”而使用接收者作为“队列客户端”以避免混淆)。发送到队列的消息存储在磁盘或内存中,直到有人拾取或过期。因此,队列(和持久订阅)需要一些活动存储管理,您需要考虑缓慢的消费者。

在大多数环境中,我认为主题是更好的选择,因为您总是可以添加额外的组件,而不必更改架构。添加的组件可以是监控、日志记录、分析等。 在项目开始时,你永远不知道1年、5年、10年后的需求是什么。改变是不可避免的,拥抱它吧:-)