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

任何想法吗?


当前回答

队列

Pros

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

Cons

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

主题

Pros

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

Cons

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

其他回答

至于订单保存,请参阅ActiveMQ页面。简而言之:订单是为单个消费者保留的,但对于多个消费者,配送订单是不保证的。

TOPIC:: TOPIC是一对多的交流…(多点或发布/订阅) 假设一个发行商在youtub上发布了一部电影,那么它所有的订阅者都会收到通知.... QUEVE: QUEVE是一对一的交流… 当发布一个充值请求时,它只会发送给一个接收者… 永远记住,如果请求去了所有的qreceiver,那么多次充值发生,所以在开发时分析哪个适合应用程序

队列

Pros

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

Cons

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

主题

Pros

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

Cons

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

主题适用于发布者-订阅者模型,而队列适用于点对点。

道理很简单:

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