之前的几篇博文中,我们实现的ActiveMQ消息未实现消息的持久化,一旦ActiveMQ服务重启则未发送的消息全部丢失,所以实现ActiveMQ消息的持久化也是很重要的。那么,这篇文章,我就带着大家一起来实现ActiveMQ消息的持久化。本博文主要是将ActiveMQ消息持久化到MySQL数据库。实现ActiveMQ消息的持久化主要是修改ActiveMQ conf目录下的activemq.xml文件。那么,我们就打开activemq.xml一起修改配置来实现ActiveMQ消息的持久化吧。
1、配置activeMQ需要的mySql数据源
<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/activemq?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
在</broker>结点之后,增加数据源的配置
2.改变ActiveMQ默认的持久化方式
在activemq.xml中注释掉默认的kahadb,使用jdbc持久化
<!--
<persistenceAdapter>
<kahaDB directory="${activemq.data}/kahadb"/>
</persistenceAdapter>
-->
<persistenceAdapter>
<jdbcPersistenceAdapter dataDirectory="activemq-data" useDatabaseLock="false" transactionIsolation="4" dataSource="#mysql-ds"/>
</persistenceAdapter>
3.设置JMS访问连接协议类型
修改原来的协议连接为如下方式:
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="default" uri="tcp://0.0.0.0:61616"/>
<!--
<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>
4.提供mysql的驱动程序
由于activeMQ消息服务器,没有自带mysql数据库的驱动程序。我们需要手动将mysql驱动添加到消息服务器。
方法是将驱动拷贝到ActiveMQ的lib\目录下。主要有以下几个Jar包:commons-collections-3.2.1.jar、commons-dbcp-1.4.jar、commons-pool-1.3.jar、mysql-connector-java-5.1.30-bin.jar
经过上面的配置,我们重新启动消息服务器,就可以发现activeMQ在数据库中新建了3张表activemq_acks ,activemq_lock ,activemq_msgs 。
至此数据库持久化完成。ActiveMQ持久化的中表结构是什么,表需要人工创建吗?其实不需要,ActiveMQ会帮助我们生成的。只需要制定采用的数据库名称并,创建数据库即可。ActiveMQ采用MySQL5.6持久化产生的SQL语句:
持久化mysql数据库的3张表;
activemq_acks:ActiveMQ的签收信息。
activemq_lock:ActiveMQ的锁信息。
activemq_msgs:ActiveMQ的消息的信息
5、验证
启动ActiveMQ后,我们运行博文《JMS之——基于ActiveMQ实现简单的消息收发案例》 中的消息提供者类JMSProducer,控制台打印如下:
此时我们打开activemq_msgs表,如下:
说明消息提供者类JMSProducer生产的消息全部持久化到了MySQL中。
此时,我们重启ActiveMQ服务,再次打开activemq_msgs表如下:
说明MySQL数据表未被清空,此时,我们运行博文《JMS之——基于ActiveMQ实现简单的消息收发案例》 中的消息消费者类JMSConsumer,控制台打印如下:
此时,我们再次打开activemq_msgs表如下:
说明消息被消费了。
6.activemq.xml全部配置信息
<!--
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!-- 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">
<value>file:${activemq.conf}/credentials.properties</value>
</property>
</bean>
<!-- Allows accessing the server log -->
<bean id="logQuery" class="io.fabric8.insight.log.log4j.Log4jLogQuery"
lazy-init="false" scope="singleton"
init-method="start" destroy-method="stop">
</bean>
<bean id="mysql-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/activemq?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="poolPreparedStatements" value="true"/>
</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=">" >
<!-- The constantPendingMessageLimitStrategy is used to prevent
slow topic consumers to block producers and affect other consumers
by limiting the number of messages that are retained
For more information, see:
http://activemq.apache.org/slow-consumer-handling.html
-->
<pendingMessageLimitStrategy>
<constantPendingMessageLimitStrategy limit="1000"/>
</pendingMessageLimitStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
<!--
The managementContext is used to configure how ActiveMQ is exposed in
JMX. By default, ActiveMQ uses the MBean server that is started by
the JVM. For more information, see:
http://activemq.apache.org/jmx.html
-->
<managementContext>
<managementContext createConnector="false"/>
</managementContext>
<!--
Configure message persistence for the broker. The default persistence
mechanism is the KahaDB store (identified by the kahaDB tag).
For more information, see:
http://activemq.apache.org/persistence.html
-->
<!--
<persistenceAdapter>
<kahaDB directory="${activemq.data}/kahadb"/>
</persistenceAdapter>
-->
<persistenceAdapter>
<jdbcPersistenceAdapter dataDirectory="activemq-data" useDatabaseLock="false" transactionIsolation="4" dataSource="#mysql-ds"/>
</persistenceAdapter>
<!--
The systemUsage controls the maximum amount of space the broker will
use before disabling caching and/or slowing down producers. For more information, see:
http://activemq.apache.org/producer-flow-control.html
-->
<systemUsage>
<systemUsage>
<memoryUsage>
<memoryUsage percentOfJvmHeap="70" />
</memoryUsage>
<storeUsage>
<storeUsage limit="100 gb"/>
</storeUsage>
<tempUsage>
<tempUsage limit="50 gb"/>
</tempUsage>
</systemUsage>
</systemUsage>
<!--
The transport connectors expose ActiveMQ over a given protocol to
clients and other brokers. For more information, see:
http://activemq.apache.org/configuring-transports.html
-->
<transportConnectors>
<!-- DOS protection, limit concurrent connections to 1000 and frame size to 100MB -->
<transportConnector name="default" uri="tcp://0.0.0.0:61616"/>
<!--
<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>
<!-- destroy the spring context on shutdown to stop jetty -->
<shutdownHooks>
<bean xmlns="http://www.springframework.org/schema/beans" class="org.apache.activemq.hooks.SpringContextHook" />
</shutdownHooks>
</broker>
<!--
Enable web consoles, REST and Ajax APIs and demos
The web consoles requires by default login, you can disable this in the jetty.xml file
Take a look at ${ACTIVEMQ_HOME}/conf/jetty.xml for more details
-->
<import resource="jetty.xml"/>
</beans>
<!-- END SNIPPET: example -->
7、参考
http://activemq.apache.org/jdbc-master-slave.html
8、温馨提示
大家可以到链接下载ActiveMQ消息持久化所需要的Jar包