JMS学习(五)--ActiveMQ中的消息的持久化和非持久化 以及 持久订阅者 和 非持久订阅者之间的区别与联系...
一,消息的持久化和非持久化
①DeliveryMode
這是傳輸模式。ActiveMQ支持兩種傳輸模式:持久傳輸和非持久傳輸(persistent and non-persistent delivery),默認(rèn)情況下使用的是持久傳輸。
可以通過(guò)MessageProducer 類的 setDeliveryMode方法設(shè)置傳輸模式:
MessageProducer producer = ...; producer.setDeliveryMode(DeliveryMode.PERSISTENT);?
持久傳輸和非持久傳輸最大的區(qū)別是:采用持久傳輸時(shí),傳輸?shù)南?huì)保存到磁盤中(messages are persisted to disk/database),即“存儲(chǔ)轉(zhuǎn)發(fā)”方式。先把消息存儲(chǔ)到磁盤中,然后再將消息“轉(zhuǎn)發(fā)”給訂閱者。
采用非持久傳輸時(shí),發(fā)送的消息不會(huì)存儲(chǔ)到磁盤中。
采用持久傳輸時(shí),當(dāng)Borker宕機(jī) 恢復(fù)后,消息還在。采用非持久傳輸,Borker宕機(jī)重啟后,消息丟失。比如,當(dāng)生產(chǎn)者將消息投遞給Broker后,Broker將該消息存儲(chǔ)到磁盤中,在Broker將消息發(fā)送給Subscriber之前,Broker宕機(jī)了,如果采用持久傳輸,Broker重啟后,從磁盤中讀出消息再傳遞給Subscriber;如果采用非持久傳輸,這條消息就丟失了。
關(guān)于傳輸模式的官方文檔,可參考
?
關(guān)于Message Durability 和 Message Persistence的區(qū)別:我是在《ActiveMQ in Action》中看到的,理解如下:
②Message Persistence
Message persistence is independent of the message domain. It is used to indicate the JMS application's ability to handle missing messages in the event of a JMS provider failure.這說(shuō)明:1)Message Persistence 與Domain無(wú)關(guān)。 2)Message persistence 與傳輸模式有關(guān),如果某消息 使用的是持久傳輸,則該消息具有 Persistence性質(zhì),否則不是。
到這里為止,已經(jīng)知道若ActiveMQ采用持久傳輸模式,則會(huì)把消息持久化到磁盤中。那么,消息是以何種形式存儲(chǔ)的呢?是存儲(chǔ)到文件中還是存儲(chǔ)到數(shù)據(jù)庫(kù)中? 存儲(chǔ)的數(shù)據(jù)結(jié)構(gòu)如何實(shí)現(xiàn)?是B樹還是其他?…… 關(guān)于這些問(wèn)題都是很直接值得研究的。關(guān)于消息持久化內(nèi)容,可簡(jiǎn)單參考:
?
③Durability of messages(Message Durability)
Message durability can only be achieved with the pub/sub domain. When clients connect to a topic, they can do so using a durable or a non-durable subscription.以上說(shuō)明了兩點(diǎn):1)Durability of messages 只針對(duì)發(fā)布訂閱模型(Domain)。 2)持久訂閱和非持久訂閱是針對(duì)Topic而言的,不是針對(duì)Queue的。
也就是說(shuō),如果某個(gè)消息被持久訂閱者訂閱了,那么該消息就具有:Durability,否則就是NonDurability
?
二,持久訂閱者和非持久訂閱者
①Durable Subscribers and NonDurable Subscribers
首先,持久訂閱者和非持久訂閱者針對(duì)的Domain是Pub/Sub,而不是P2P
當(dāng)Broker發(fā)送消息給訂閱者時(shí),如果訂閱者處于 inactive 狀態(tài):持久訂閱者可以收到消息,而非持久訂閱者則收不到消息。
類似于QQ消息,別人給你發(fā)了離線消息,如果是非持久訂閱者 就收到不離線消息。
造成的影響是:當(dāng)持久訂閱者處于 inactive 狀態(tài)時(shí),Broker需要為持久訂閱者保存消息,如果持久訂閱者訂閱的消息太多則會(huì)溢出。(當(dāng)消息投遞成功之后,Broker就可以把消息刪除了)
一個(gè)具體的官方實(shí)例如下:
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.?
②Durable Queues(Topics) and NonDurable Queues(Topics)
區(qū)別:參考鏈接 三,參考資料 http://blog.christianposta.com/guaranteed-messaging-for-topics-the-jms-spec-and-activemq/理解JMS規(guī)范中消息的傳輸模式和消息持久化
理解JMS規(guī)范中的持久訂閱和非持久訂閱
轉(zhuǎn)載于:https://www.cnblogs.com/hapjin/p/5644402.html
總結(jié)
以上是生活随笔為你收集整理的JMS学习(五)--ActiveMQ中的消息的持久化和非持久化 以及 持久订阅者 和 非持久订阅者之间的区别与联系...的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 给source insight添加.cc
- 下一篇: Spark-1.6.0之Applicat