DURABLE在queue和topic中的区别

Durable订阅在JMS中的作用主要在于处理宕机等情况,确保消息不丢失。对于Queue,Durable特性影响较小,因为只有一个消费者会消费消息。然而在Topic中,每个Durable消费者都会保留消息的独立副本,即使消费者离线,重启后仍能接收到所有消息。实现这一功能的关键在于设置clientID,以便JMS provider跟踪订阅者状态并发送正确消息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

durable是为了防止宕机等异常而导致消息无法及时接收设计的。

这个对queue无太多影响,但对topic影响比较大。

本文引用

http://activemq.apache.org/how-do-durable-queues-and-topics-work.html

 

对queue的影响

Durable queues keep messages around persistently for any suitable consumer to consume them. Durable queues do not need to concern themselves with which consumer is going to consume the messages at some point in the future. There is just one copy of a message that any consumer in the future can consume.

 

对topic的影响

Durable topics however are different as they must logically persist an instance of each suitable message for every durable consumer - since each durable consumer gets their own copy of the message.

 

对topic影响举例说明

For example imagine a durable subscriber S starts up subscribing to topic T at time D1. Some publisher sends messages M1, M2, M3 to the topic and S will receive each of these messages. Then S is stopped and the publisher continues to send M4, M5.

When S restarts at D2, the publisher sends M6 and M7. Now S will receive M4, M5 followed by M6 and M7 and all future messages. i.e. S will receive all messages from M1..M7.

This is the difference between durable and non-durable consuming. If S were a non-durable consumer then it would only have received M1, M2, M3 and M6, M7 - not M4 and M5. i.e. because the subscription is durable, S will receive every message sent to T whether the subscriber is running or not. For non-durable topics, only messages delivered to the topic T when S is running are delivered.

So for durable topic subscription, the JMS provider needs to be able to identify S when it shuts down and later on in the future reconnects, so it can know what messages to send to it while it was not running. JMS specification dictates that the identification of S is done by a combination of the clientID and the durable subscriber name. This is so that the JMS connection S uses can have many different durable subscriptions on different topics or on the same topic with different selectors - yet the JMS provider can know which message for which subscription to keep around for it.

So setting the clientID on a JMS connection is vital (along with using a sensible durable consumer name) for durable topic subscription. Its not an issue for other QoS

 

在topic模式下,如果设置durable为true,就要设置clientID给 JMS provider,让他来维护记录 每个订阅者接收消息状态。同时topic的订阅者没接收一条消息也要反馈一条成功接收信息给JMS provider。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值