写在前面话
关于这一节的程序,需要使用到《ActiveMQ系列之——安装、运行及事例代码(一)》中的示例代码。本文主要介绍关于消息持久化的配置。
ActiveMQ默认情况下是基于文件的存储,使用的是kahaDB,当然还有其它的持久化方式,例如LevelDB,这个是在5.8的版本中引入的,本文主要介绍数据库持久化,使用的数据库是MySQL,其它数据库类似。
介绍
ActiveMQ数据库持久化有两种类型,一是直接的JDBC操作,二是journal的jdbc操作,第二是优化过的,先快速的保存到文件中,然后在某个检查点与数据对比,之后再写数据库,也可以称之为批量操作数据库。持久化操作的主要配置在activemq.xml中,在此基础上,我做了其它两点改动,在conf目录下增加一个jdbc.properties的文件,将数据库有关配置信息保存在此文件中,activemq.xml通过加载外部属性文件的方式引入该文件。
配置
journal
activemq.xml(地址
https://github.com/wangdongsong/java/tree/master/src/main/resources/activemq/journaljdbc)
<!--
在5.9.0里的conf目录下,修改activemq.xml而成。
1、增加了一加载jdbc.properties的属性文件
2、在broker节点中增加了persistenceFactory的配置
3、增加了mySql的bean定义
-->
<!-- START SNIPPET: example -->
<beans
xmlns="http://www.springframework.org/schema/beans"
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.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<!-- Allows us to use system properties as variables in this configuration file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${activemq.conf}/credentials.properties</value>
<!--
1、增加了加载数据库的属性文件,目录为activemq目录下的conf文件中,
此文件定义了有关数据连接的信息,conf目录下无jdbc.properties文件,需要手动创建,
也可以不使用此属性文件,将数据库信息直接写到activemq.xml中
-->
<value>file:${activemq.conf}/jdbc.properties</value>
</list>
</property>
</bean>
<!-- Allows log searching in hawtio console -->
<bean id="logQuery" class="org.fusesource.insight.log.log4j.Log4jLogQuery"
lazy-init="false" scope="singleton"
init-method="start" destroy-method="stop">
</bean>
<!--
The <broker> element is used to configure the ActiveMQ broker.
-->
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry topic=">" >
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<managementContext>
<managementContext createConnector="false"/>
</managementContext>
<!--
2、增加此节点的配置,此种方式直接操作数据库
也可以通过persistenceAdapter这个节点的journalPersistenceAdapter的配置完成
-->
<persistenceFactory>
<!-- 指定数据文件的路径及数据源 -->
<journalPersistenceAdapterFactory dataDirectory="${activemq.data}/wdsdb" dataSource="#mySql"/>
</persistenceFactory>
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage percentOfJvmHeap="70" />
</memoryUsage>
<storeUsage>
<storeUsage limit="100 gb"/>
</storeUsage>
<tempUsage>
<tempUsage limit="50 gb"/>
</tempUsage>
</systemUsage>
</systemUsage>
<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
<shutdownHooks>
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
</shutdownHooks>
</broker>
<!--
3、增加关于数据库连接的配置,在增加此配置时,还需要将数据库驱动的jar包放到activemq安装目录下的lib目录中
-->
<bean id="mySql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<import resource="jetty.xml"/>
</beans>
<!-- END SNIPPET: example -->
非journal
<!--
在5.9.0里的conf目录下,修改activemq.xml而成。
1、增加了一加载jdbc.properties的属性文件
2、在broker节点中增加了persistenceAdapter的配置
3、增加了mySql的bean定义
-->
<!-- START SNIPPET: example -->
<beans
xmlns="http://www.springframework.org/schema/beans"
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.xsd
http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<!-- Allows us to use system properties as variables in this configuration file -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>file:${activemq.conf}/credentials.properties</value>
<!--
1、增加了加载数据库的属性文件,目录为activemq目录下的conf文件中,
此文件定义了有关数据连接的信息,conf目录下无jdbc.properties文件,需要手动创建,
也可以不使用此属性文件,将数据库信息直接写到activemq.xml中
-->
<value>file:${activemq.conf}/jdbc.properties</value>
</list>
</property>
</bean>
<!-- Allows log searching in hawtio console -->
<bean id="logQuery" class="org.fusesource.insight.log.log4j.Log4jLogQuery"
lazy-init="false" scope="singleton"
init-method="start" destroy-method="stop">
</bean>
<!--
The <broker> element is used to configure the ActiveMQ broker.
-->
<broker xmlns="http://activemq.apache.org/schema/core" brokerName="localhost" dataDirectory="${activemq.data}">
<destinationPolicy>
<policyMap>
<policyEntries>
<policyEntry topic=">" >
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<managementContext>
<managementContext createConnector="false"/>
</managementContext>
<!--
2、增加此节点的配置,此种方式直接操作数据库
-->
<persistenceAdapter>
<jdbcPersistenceAdapter dataSource="#mySql"/>
</persistenceAdapter>
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage percentOfJvmHeap="70" />
</memoryUsage>
<storeUsage>
<storeUsage limit="100 gb"/>
</storeUsage>
<tempUsage>
<tempUsage limit="50 gb"/>
</tempUsage>
</systemUsage>
</systemUsage>
<transportConnectors>
<transportConnector name="openwire" uri="tcp://0.0.0.0:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="amqp" uri="amqp://0.0.0.0:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="stomp" uri="stomp://0.0.0.0:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="mqtt" uri="mqtt://0.0.0.0:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
<transportConnector name="ws" uri="ws://0.0.0.0:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600"/>
</transportConnectors>
<shutdownHooks>
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
</shutdownHooks>
</broker>
<!--
3、增加关于数据库连接的配置,在增加此配置时,还需要将数据库驱动的jar包放到activemq安装目录下的lib目录中
-->
<bean id="mySql" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<import resource="jetty.xml"/>
</beans>
<!-- END SNIPPET: example -->
##jdbc.driver
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/activemq?autoReconnect=true&useUnicode=true&characterEncoding=utf8
jdbc.username=root
jdbc.password=root