JMS學習(三)ActiveMQ Message Persistence


1,JMS規范支持兩種類型的消息傳遞:persistent and non-persistent。ActiveMQ在支持這兩種類型的傳遞方式時,還支持消息的恢復、中間狀態的消息(message are cached in memory)

2,ActiveMQ可將消息存儲在三種類型介質中:file-based(存儲在文件中)、in-memory(存儲在內存中)、relational databases(存儲在關系數據庫中)

 

3,Persistence Message有何用處?

Persistent messages are ideal if you want messages to always be available to a message consumer after they have been delivered to a message broker, or you need messages to survive even if there has been a system failure。

①消息對消費者總是可用。②系統宕機后,消息不被丟失。

4,ActiveMQ中存儲兩種Destination,主題(Topic)與 隊列(Queue)。主題對應的Domain是發布-訂閱模型(Pub/Sub),隊列對應的Domain是點對點模型(P2P)。

隊列存儲:按FIFO的順序存儲消息,Only one message is dispatched between one of potentially many consumers. Only when that message has been consumed and acknowledged can it be deleted from the broker's message store.

對於眾多的消費者,只有其中一個消費者可以接收該消息。也只有當消費者消費了之后,該消息才能刪除。

 

Topic存儲:

For durable subscribers to a topic, each consumer gets a copy of the message. In order to save space (some messages can be very large!), only one copy of a message is stored by the broker.

什么是durable subscribers(持久訂閱者)?對於 durable subscribers而言,盡管當前它是不活躍的(沒有連接到borker),也不會錯失發給broker的消息(類似於QQ的離線消息)。持久訂閱者維護者一個消息指針,指針總是指向該訂閱者需要的下一個消息。

A durable subscriber object in the store maintains a pointer to its next stored message and dispatches a copy of it to its consumer as shown in Figure 5.2.

 

為什么要這樣設計?

①每個消費者消費速率是不同的

②不是所有的消費者都在線(actived)

③同一個消息可能被多個消費者訂閱

通過指針這種形式,可以很好地解決上面三種情況下出現的問題。只有當所有的消費者都獲得了該消息時,消息才能從broker中刪除。

 

5,ActiveMQ中消息存儲的方式:

ActiveMQ provides a pluggable API for message stores as well as multiple message store implementations including:

①AMQ Message Store

②KahaDB Message Store,ActiveMQ 5.13.2版本默認的存儲方式

③JDBC Message Store

在配置文件activemq.xml中有存儲方式的配置選項:

 

AMQ存儲方式的實現思想非常值得一看:

 

The Journal consists of a rolling log of messages and commands (such as transactional boundaries and message deletions) stored in data files of a certain length. When the maximum length of the currently used data file has been reached a new data file is created. All the messages in a data file are reference counted, so that once every message in that data file is no longer required, the data file can be removed or archived. The journal only appends messages to the end of the current data file, so storage is very fast.

存儲Journal的文件是定長的,並且只以append方式向文件寫入日志。

為了方便,直接截圖了。

 

 

 

KahaDB 消息存儲方式就不截圖了。AMQ方式采用HashTable存儲Reference Index,而KahaDB采用B-Tree存儲索引。

此外,關於配置ActiveMQ使用JDBC作為持久存儲的方法,參考這篇文章

 

6,Broker為Consumer緩存消息

由於第5點中講到,消息存儲到持久介質(采用了持久傳輸模式)中,因此就不適用那些需要實時消息的場合。---比如,交易信息需要在秒級時間內獲得,用完之后就不再需要它了。

因此,ActiveMQ可以配置緩存哪種類型的消息、緩存多少消息、這些消息緩存多久?

Messages that are cached by the broker are only dispatched to a topic consumer if it is retroactive; and never to durable topic subscribers 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM