天天看點

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.

對于衆多的消費者,隻有其中一個消費者可以接收該消息。也隻有當消費者消費了之後,該消息才能删除。

JMS學習(三)ActiveMQ Message Persistence(轉)

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.

JMS學習(三)ActiveMQ Message Persistence(轉)

為什麼要這樣設計?

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

②不是所有的消費者都線上(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中有存儲方式的配置選項:

JMS學習(三)ActiveMQ Message Persistence(轉)

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

JMS學習(三)ActiveMQ Message Persistence(轉)

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方式向檔案寫入日志。

為了友善,直接截圖了。

JMS學習(三)ActiveMQ Message Persistence(轉)
JMS學習(三)ActiveMQ Message Persistence(轉)

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 

繼續閱讀