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 

posted @ 2016-04-28 10:47  大熊猫同学  阅读(1908)  评论(1编辑  收藏  举报