activemq 结合 数据库的使用配置 ,此处以mysql为例子
版本 5.9.0 jdk 1.7 单机版
关于数据库的配置 参考 :
在配置文件中 增加 配置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 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=">" >
<!-- 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"/>-->
<jdbcPersistenceAdapter dataDirectory="${activemq.data}/activemq-data" 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="10 gb"/>
</storeUsage>
<tempUsage>
<tempUsage limit="1 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="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>
<!-- This xbean configuration file supports all the standard spring xml configuration options 数据库配置 -->
<!-- Postgres DataSource Sample Setup -->
<!--
<bean id="postgres-ds" class="org.postgresql.ds.PGPoolingDataSource">
<property name="serverName" value="localhost"/>
<property name="databaseName" value="activemq"/>
<property name="portNumber" value="0"/>
<property name="user" value="activemq"/>
<property name="password" value="activemq"/>
<property name="dataSourceName" value="postgres"/>
<property name="initialConnections" value="1"/>
<property name="maxConnections" value="10"/>
</bean>
-->
<!-- MySql DataSource Sample Setup -->
<!-- -->
<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://192.168.10.222:3306/activemq?relaxAutoCommit=true"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
<!-- Oracle DataSource Sample Setup -->
<!--
<bean id="oracle-ds" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="jdbc:oracle:thin:@localhost:1521:AMQDB"/>
<property name="username" value="scott"/>
<property name="password" value="tiger"/>
<property name="poolPreparedStatements" value="true"/>
</bean>
-->
<!-- Embedded Derby DataSource Sample Setup -->
<!--
<bean id="derby-ds" class="org.apache.derby.jdbc.EmbeddedDataSource">
<property name="databaseName" value="derbydb"/>
<property name="createDatabase" value="create"/>
</bean>
-->
<!--
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 -->
然后还需要引入 mysql 的相关数据库连接jar 此处使用的 dbcp 数据库连接
在 mysql 中 创建 activemq 数据库 这个名字可以根据自己的来进行修改 ,当然 如果自定义的话 对应的 数据库连接处的名字也要跟着修改 大家都懂的。
geronimo-jta_1.1_spec-1.1.1.jar
commons-dbcp-1.4.jar
commons-pool-1.5.4.jar
mysql-connector-java-5.1.44.jar
下载这几个jar 然后 放入 activemq 的 lib 文件夹中
[julong@localhost lib]$ pwd
/home/julong/apache-activemq-5.9.0/lib
[julong@localhost lib]$ ll
total 5428
-rw-r--r--. 1 julong julong 1002495 Oct 15 2013 activemq-broker-5.9.0.jar
-rw-r--r--. 1 julong julong 1165912 Oct 15 2013 activemq-client-5.9.0.jar
-rw-r--r--. 1 julong julong 182243 Oct 15 2013 activemq-console-5.9.0.jar
-rw-r--r--. 1 julong julong 31527 Oct 15 2013 activemq-jaas-5.9.0.jar
-rw-r--r--. 1 julong julong 539427 Oct 15 2013 activemq-kahadb-store-5.9.0.jar
-rw-r--r--. 1 julong julong 685801 Oct 15 2013 activemq-openwire-legacy-5.9.0.jar
-rw-r--r--. 1 julong julong 147874 Aug 9 2010 activemq-protobuf-1.1.jar
-rw-r--r--. 1 julong julong 961 Oct 15 2013 activemq-rar.txt
-rw-r--r--. 1 julong julong 160352 Oct 15 2013 activemq-spring-5.9.0.jar
-rw-r--r--. 1 julong julong 109748 Oct 15 2013 activemq-web-5.9.0.jar
drwxrwxr-x. 2 julong julong 4096 Sep 16 21:59 camel
-rw-rw-r--. 1 julong julong 160519 Nov 12 19:23 commons-dbcp-1.4.jar
-rw-rw-r--. 1 julong julong 96221 Nov 12 19:23 commons-pool-1.5.4.jar
drwxrwxr-x. 2 julong julong 32 Sep 16 21:59 extra
-rw-r--r--. 1 julong julong 20220 Jul 29 2009 geronimo-j2ee-management_1.1_spec-1.0.1.jar
-rw-r--r--. 1 julong julong 32359 Jun 17 2009 geronimo-jms_1.1_spec-1.1.1.jar
-rw-r--r--. 1 julong julong 14637 Jun 17 2009 geronimo-jta_1.0.1B_spec-1.0.1.jar
-rw-rw-r--. 1 julong julong 16030 Nov 12 19:23 geronimo-jta_1.1_spec-1.1.1.jar
-rw-r--r--. 1 julong julong 50139 Mar 23 2012 hawtbuf-1.9.jar
-rwxr-xr-x. 1 julong julong 20044 Oct 15 2013 insight-log4j-7.2.0.redhat-024-patched.jar
-rw-r--r--. 1 julong julong 21902 Sep 20 2013 insight-log-core-7.2.0.redhat-024.jar
-rw-r--r--. 1 julong julong 16517 Sep 16 2013 jcl-over-slf4j-1.7.5.jar
-rw-rw-r--. 1 julong julong 999808 Nov 12 19:28 mysql-connector-java-5.1.45.jar
drwxrwxr-x. 2 julong julong 4096 Sep 16 21:59 optional
-rw-r--r--. 1 julong julong 26084 Sep 16 2013 slf4j-api-1.7.5.jar
drwxrwxr-x. 2 julong julong 4096 Sep 16 21:59 web
[julong@localhost lib]$
然后 重新启动 activemq start
启动之后 可以在数据库看到三张表
activemq_acks
activemq_lock
activemq_msgs
至此 可以进行实验操作了 ,尝试使用代码创建消息 并且 查看数据库 是否存在数据 。