为了防止系统意外down机丢失消息,同时能在系统恢复后能重新发送原来未发送的消息。一般消息系统都会采用持久化机制。Activemq5.4提供了几种持久化机制:
- KahaDB message store
- Journaled JDBC adapter
- Non-journaled JDBC adapter
消息持久化,就是将消息进行一个存储。
如图所示,主题中的消息都是持久化后的,订阅同一topic的用户,都有自己的一个指针记录,消息处理的进展。
KahaDB存储
- 基于文件形式存储的。
- 基于文件存储,不需要第三方存储数据库。
使用KahaDB存储需要配置activemq.xml中<persistenceAdapter>
<persistenceAdapter>
<kahaDB directory="${activemq.data}/kahadb"/>
</persistenceAdapter>
同样可以以编程的方式,制定消息的存储
public class EmbeddedBrokerUsingAMQStoreExample {
BrokerService createEmbeddedBroker() throws Exception {
BrokerService broker = new BrokerService();
File dataFileDir = new File("target/amq-in-action/kahadb");
KahaDBStore kaha = new KahaDBStore();
kaha.setDirectory(dataFileDir);
// Using a bigger journal file
kaha.setJournalMaxFileLength(1024 * 100);
// small batch means more frequent and smaller writes
kaha.setIndexWriteBatchSize(100);
// do the index write in a separate thread
kaha.setEnableIndexWriteAsync(true);
broker.setPersistenceAdapter(kaha);
// create a transport connector
broker.addConnector("tcp://localhost:61616");
// start the broker
broker.start();
return broker;
}
}
默认情况下,ActiveMQ后台开启kahaDB,
我们查看目录:/mq/apache-activemq-5.13.3/data/kahadb
有4中类型格式的文件生成
db log files—KahaDB stores messages into data log files named db-.log of
a predefined size. When a data log is full, a new one will be created, and the log number incremented. When there are no more references to any of the messages in the data log file, it’ll be deleted or archiveddb.data —This file contains the persistent BTree indexes to the messages held in the message data logs.(用于存储消息索引)
db.redo —This is the redo file, used for recovering the BTree indexes if the KahaDB message store starts after a hard stop(用于数据恢复)
更多参数,详见下图
更多存储方式:
- The AMQ message store—A file-based message store designed for
performance - The JDBC message store—A message store based on JDBC
- The Memory message store—A memory-based message store