In this paper, the reference

The official document: activemq.apache.org/persistence

Blog: blog.csdn.net/weixin_4279…

Persistence mode support

The latest version supports the following message persistence modes. For details about the persistence modes of other versions, see the official documents

  • KahaDB file persistence
  • JDBC persistence
  • LevelDB storage
  • LevelDB Primary/secondary replication
  • AMQ persistence; (Not recommended, kahaDB can be used instead)
  • Memory Memory persistence; (Not recommended, easy to lose data)

Persistent configuration and features

If there are no special circumstances, activeMQ’s persistent configuration file is /config/ Activemq.xml

KahaDB file persistence

KahaDB is officially recommended from activeMQ version 5.3, which offers much higher scalability and recoverability than AMQ message storage. The AMQ message store, while faster than KahaDB, is not as scalable as KahaDB and takes longer to recover.

Kaha is the default persistence policy, where all messages are sequentially added to a log file, with an index file that points to the address where these logs are stored, and a transaction log for message reply operations. Is a solution specifically for message persistence that optimizes typical message usage patterns.

In the data/kahadb directory, four files are generated for message persistence

  1. Db.data is an index file for messages, which is essentially a B-tree, using the B-tree as an index to point to messages stored in db-*.log
  2. Db.redo is used for message recovery
  3. Db -*.log stores message content. New data is appended to the end of the log file as an APPEND. It is sequential writing, so message storage is faster. The default value is 32 MB. The value increases automatically when the threshold is reached
  4. Lock File lock that writes to the broker that currently has access to kahaDB for contention processing in a clustered environment
<persistenceAdapter>
    <kahaDB directory="${activemq.data}/kahadb" journalMaxFileLength="16mb"/>
</persistenceAdapter>
Copy the code

More parameters configuration reference website: activemq.apache.org/kahadb

JDBC persistence

JDBC persistence requires the configuration of a persistence adapter that points to a bean for a data source. Note, in particular, that the root element should be configured in, not with, the configuration. The official recommendation is to use a combination of JDBC and High Performance Journal in long-term persistence scenarios. Using JDBC alone is acceptable but slow.

Journal files can greatly reduce the number of messages that need to be written to the DB while message consumers can keep up with producers. For example, the producer produces 10,000 messages, which are saved to the Journal file, but the consumer is fast, consuming 9900 messages before the Journal file is synchronized to the DB. Then you only need to write 100 messages to DB. If the consumer cannot keep up with the producer, the Journal file enables messages to be written to the DB in bulk, which is optimized by the JDBC driver. This improves performance. In addition, journal files support JMS transaction consistency.

Apache Derby is officially supported as the default database, which is easy to embed, as well as other SQL databases such as MySQL and Oracle, simply by reconfiguring the JDBC Configuration in Xml Configuration.

  • JDBC+ High-performance log configuration example

The following configuration is from the official website

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:amq="http://activemq.apache.org/schema/core" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd"> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"/> <broker useJmx="true" xmlns="http://activemq.apache.org/schema/core"> <networkConnectors> <! -- <networkConnector uri="multicast://default? initialReconnectDelay=100" /> <networkConnector uri="static://(tcp://localhost:61616)" /> --> </networkConnectors> <persistenceFactory> <journalPersistenceAdapterFactory journalLogFiles="5" dataDirectory="${basedir}/target" /> <! -- To use a different dataSource, use the following syntax : --> <! -- <journalPersistenceAdapterFactory journalLogFiles="5" dataDirectory="${basedir}/activemq-data" dataSource="#mysql-ds"/> --> </persistenceFactory> <transportConnectors> <transportConnector uri="tcp://localhost:61636"  /> </transportConnectors> </broker> <! -- MySql DataSource Sample Setup --> <! -- <bean id="mysql-ds" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/> <property name="username" value="activemq"/> <property name="password" value="activemq"/> <property name="poolPreparedStatements" value="true"/> </bean> --> </beans>Copy the code
  • Example of JDBC configuration
<! - configure a data source under the beans elements - > < bean id = "mysqlDataSource" class = "org.apache.com mons. Dbcp2. BasicDataSource." " destroy-method="close"> <property name="driverClassName" value="com.mysql.jdbc.Driver"/> <property name="url" value="jdbc:mysql://localhost:3306/test"/> <property name="username" value="root"/> <property name="password" value="1234"/> </bean> <! JdbcPersistenceAdapter dataSource="#mysqlDataSource" createTablesOnStartup="true" /> </persistenceAdapter>Copy the code

LevelDB Storage and levelDB storage for replication

KahaDB is recommended for use. The detailed configuration will not be explained here. It is worth mentioning that the company has used LevelDB replication storage before

Memory message store

So memory-based message storage, messages are stored in memory, so basically they’re not persisted, they’re stored directly in memory, and the configuration is persistent=”false” in the tag