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 


本文转自hapjin博客园博客,原文链接:http://www.cnblogs.com/hapjin/p/5438377.html,如需转载请自行联系原作者

相关文章
|
4月前
|
消息中间件 存储 Java
flea-msg使用之JMS初识
本篇介绍 JMS 的一些基础知识点、JMS编程对象、JMS点对点模型、JMS发布/订阅模型 和 JMS请求/应答模式
44 1
flea-msg使用之JMS初识
|
6月前
|
消息中间件 Java 数据库连接
03JMS简介
03JMS简介
28 0
|
8月前
|
消息中间件 Java 中间件
JMS 和 AMQP
JMS 和 AMQP简介
125 0
|
消息中间件 Java 数据库连接
了解JMS
当下消息中间件盛行,主要提供了异步、解耦、削峰平谷的作用,其实早期Java本身也提供了类似的消息服务,在研习各个消息中间件之前应该先了解下这个历史。
129 0
了解JMS
|
消息中间件 Java
spring.rabbitmq.template.mandatory 和spring.rabbitmq.publisher-returns
spring.rabbitmq.template.mandatory 和spring.rabbitmq.publisher-returns
148 0
|
消息中间件 存储 网络协议
Spring Boot与消息(JMS、AMQP、RabbitMQ)
1.概述。 大多应用中,可通过消息服务中间件来提升系统异步通信、扩展解耦能力。 消息服务中两个重要概念: 消息代理(message broker)和目的地(destination)。 当消息发送者发送
243 0
|
消息中间件 SQL XML
ActiveMQ系列:详细讲讲JMS(Java Messaging Service)
一条持久性的消息:应该被传送“一次仅仅一次”,这就意味者如果JMS提供者出现故障,该消息并不会丢失,它会在服务器恢复之后再次传递。一条非持久的消息:最多会传送一次,这意味这服务器出现故障,该消息将永远丢失。
157 0
ActiveMQ系列:详细讲讲JMS(Java Messaging Service)
|
消息中间件 存储 Java
JMS&AMQP 简介|学习笔记
快速学习 JMS&AMQP 简介
84 0
JMS&AMQP 简介|学习笔记
|
消息中间件 搜索推荐 Java
消息中间件JMS介绍、入门demo与spring整合
消息中间件JMS介绍、入门demo与spring整合
199 0
消息中间件JMS介绍、入门demo与spring整合
|
消息中间件 Java 大数据
JMS VS AMQP
《高性能》系列
102 0
JMS VS AMQP