版本说明
Spring 4.3.16.RELEASE
activemq-all 5.15.3
activemq.properties 文件
在 activemq.properties
文件中配置ActiveMQ服务器的url, userName, password等. 还可以指定queue和topic的队列名.
# ActiveMQ Config
com.tao.activemq.brokerURL = tcp\://127.0.0.1\:61616
com.tao.activemq.userName = admin
com.tao.activemq.password = admin
com.tao.activemq.pool.maxConnection = 10
# queueName
com.tao.activemq.queueName = activemq_queue_01
#topicName
com.tao.activemq.topicName = activemq_topic_01
spring-activemq.xml 配置文件说明
在 spring-activemq.xml
配置文件中, 我们需要配置 ConnectionFactory
, JmsTemplate
, Destination
, Consumer的监听器
.
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jms="http://www.springframework.org/schema/jms"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms.xsd">
<!-- 1: 读入配置文件 -->
<context:property-placeholder location="classpath*:properties/activemq.properties" ignore-unresolvable="true"/>
<!-- 2: 扫描service包下的所有使用注解的类型 -->
<context:component-scan base-package="com.tao.smp.mq"/>
<!-- 3: 配置ConnectionFactory -->
<!-- 真正的由JMS服务器厂商提供的ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<!-- ActiveMQ Config -->
<property name="brokerURL" value="${com.tao.activemq.brokerURL}"/>
<property name="userName" value="${com.tao.activemq.userName}"/>
<property name="password" value="${com.tao.activemq.password}"/>
</bean>
<!--
ActiveMQ为我们提供了一个PooledConnectionFactory, 通过往里面注入一个ActiveMQConnectionFactory
可以用来将Connection、Session和MessageProducer池化, 这样可以大大减少资源的消耗.
-->
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory" ref="targetConnectionFactory"/>
<property name="maxConnections" value="${com.tao.activemq.pool.maxConnection}"/>
</bean>
<!-- Spring用于管理connectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 注入真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="pooledConnectionFactory"/>
</bean>
<!-- 4: Spring JmsTemplate 的消息生产者Producer -->
<!-- 生产者bean -->
<bean id="queueProducer" class="com.tao.smp.mq.producer.queue.QueueProducer"</