ActiveMQ深入淺出(五)——ActiveMQ將訊息持久化到資料庫
環境描述:目前最新版本是ActiveMQ5.13.2,本文講述的例項是ActiveMQ5.9.0。
使用預設的持久化機制,我們不容易直接看到訊息究竟是如何持久的。ActiveMQ提供的JDBC持久化機制,能夠將持久化資訊儲存到資料庫。通過檢視資料庫中ActiveMQ生成的表結構和儲存的資料,能夠幫助我們更好的瞭解訊息的持久化機制。現在介紹如何配置activemq,將資料持久化到MySQL中。
1.配置activeMQ需要的mySql資料來源
為了能夠使用JDBC訪問mysql資料庫,顯然必須要配置訊息伺服器的資料庫源。在activemq\apache-activemq-5.9.0\conf\activemq.xml進行配置
- <!-- MySQL DataSource -->
- <beanid="mysql-ds"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
-
<propertyname="url"value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"
- <propertyname="username"value="root"/>
- <propertyname="password"value="root"/>
- <propertyname="poolPreparedStatements"value="true"/>
- </bean>
2.改變activeMQ預設的持久化方式
在activemq.xml中註釋掉預設的kahadb,使用jdbc持久化
-
<!--
- <persistenceAdapter>
- <kahaDB directory="${activemq.data}/kahadb"/>
- </persistenceAdapter>
- -->
- <persistenceAdapter>
- <jdbcPersistenceAdapter dataSource="#mysql-ds"/>
- </persistenceAdapter>
3.設定JMS訪問連線協議型別
修改原來的協議連線為如下方式:
- <transportConnectors>
- <transportConnectorname="default"uri="tcp://localhost:61616"/>
- </transportConnectors>
4.提供mysql的驅動程式
由於activeMQ訊息伺服器,沒有自帶mysql資料庫的驅動程式。我們需要手動將mysql驅動新增到訊息伺服器。
方法是將驅動拷貝到apache-activemq-5.9.0\lib\目錄下。
經過上面的三步配置,我們重新啟動訊息伺服器,就可以發現activeMQ在資料庫中新建了3張表activemq_acks ,activemq_lock ,activemq_msgs 。
至此資料庫持久化完成。ActiveMQ持久化的中表結構是什麼,表需要人工建立嗎?其實不需要,ActiveMQ會幫助我們生成的。只需要制定採用的資料庫名稱並,建立資料庫即可。以為為ActiveMQ採用MySQL5.7持久化產生的SQL語句:
持久化mysql資料庫的3張表;
activemq_acks:ActiveMQ的簽收資訊。
activemq_lock:ActiveMQ的鎖資訊。
activemq_msgs:ActiveMQ的訊息的資訊
5.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 -->
- <beanclass="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <propertyname="locations">
- <value>file:${activemq.conf}/credentials.properties</value>
- </property>
- </bean>
- <!-- Allows log searching in hawtio console -->
- <beanid="logQuery"class="org.fusesource.insight.log.log4j.Log4jLogQuery"
- lazy-init="false"scope="singleton"
- init-method="start"destroy-method="stop">
- </bean>
- <!-- MySql DataSource Sample Setup -->
- <beanid="mysql-ds"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertyname="driverClassName"value="com.mysql.jdbc.Driver"/>
- <propertyname="url"value="jdbc:mysql://localhost/activemq?relaxAutoCommit=true"/>
- <propertyname="username"value="root"/>
- <propertyname="password"value="root"/>
- <propertyname="poolPreparedStatements"value="true"/>
- </bean>
- <!-- Oracle DataSource Sample Setup -->
- <!--
- <beanid="oracle-ds"class="org.apache.commons.dbcp.BasicDataSource"destroy-method="close">
- <propertyname="driverClassName"value="oracle.jdbc.driver.OracleDriver"/>
- <propertyname="url"value="jdbc:oracle:thin:@localhost:1521:AMQDB"/>
- <propertyname="username"value="scott"/>
- <propertyname="password"value="tiger"/>
- <propertyname="poolPreparedStatements"