activemq是一款JMS消息队列的服务产品。其出品公司是大家所知的Apache。具体介绍大家自行百度吧,本文着重讲解spring+activemq的整合。
1、安装activemq##
1)windows环境安装####
**从http://activemq.apache.org/官网处下载apache-activemq-5.14.0.zip。****解压之后,目录结构如下:
然后打开bin目录下,如果属于32位的电脑,运行win32/activemq.bat,如果属于64位的电脑,运行win64/activemq.bat即可启动(前提是需要安装JDK。没有JDK环境的同学请自行安装JDK)。
运行成功结果如下:
运行成功之后不要关闭。(mq运行期间不能关闭此页面)
打开浏览器地址栏输入:http://localhost:8161/admin/
默认用户名密码都是admin.
进入管理页面如下:
2)、CentOS(Linux)安装####
从http://activemq.apache.org/官网处下载apache-activemq-5.14.4-bin.tar.gz,rz至/home/(路径可自行选择)
tar -zxvf apache-activemq-5.14.4-bin.tar.gz
cd apache-activemq-5.14.4/bin
./activemq start
即可启动mq。
2、整合Spring##
首先创建activemq.properties
mq.url=tcp://localhost:61616
mq.uername=admin
mq.password=admin
shopQueue=shop.topic
apQueue=ap.topic
userQueue=user.topic
然后创建spring-activemq.xml
<?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" xmlns:amq="http://activemq.apache.org/schema/core"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.1.xsd
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-3.1.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core.xsd">
<!--配置文件-->
<context:property-placeholder location="classpath:activemq.properties" ignore-unresolvable="true" />
<!--创建连接工厂-->
<bean id="connectionFactory" class="org.apache.activemq.spring.ActiveMQConnectionFactory">
<property name="brokerURL" value="${mq.url}" />
<property name="userName" value="${mq.username}"/>
<property name="password" value="${mq.password}"/>
<property name="useAsyncSend" value="true" />
</bean>
<!--配置监听模板,pubSubDomain=true代表用Topic主题订阅模式,默认是false(队列模式)-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="pubSubDomain" value="true" />
</bean>
<!--定义topic1-->
<bean id="apTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="${apQueue}" />
</bean>
<!--定义topic2-->
<bean id="shopTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="${shopQueue}" />
</bean>
<!--定义topic3-->
<bean id="userTopic" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg value="${userQueue}" />
</bean>
<!--分别定义topic的监听类-->
<bean id="shopMessageListener" class="com.riil.its.bysy.attendance.jms.ShopMessageListener"</bean>
<bean id="apMessageListener" class="com.riil.its.bysy.attendance.jms.ApMessageListener"></bean>
<bean id="userMessageListener" class="com.riil.its.bysy.attendance.jms.UserMessageListener"></bean>
<jms:listener-container>
<jms:listener destination="apTopic" ref="apMessageListener" />
<jms:listener destination="shopTopic" ref="shopMessageListener" />
<jms:listener destination="userTopic" ref="userMessageListener" />
</jms:listener-container>
<!--监听容器1-->
<bean id="jmsContainer1"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<!--指定连接工厂-->
<property name="connectionFactory" ref="connectionFactory" />
<!--指定Topic-->
<property name="destination" ref="apTopic" />
<!--客户端ID,配置持久订阅者必须指定一个clientId的值-->
<property name="clientId" value="clientId_001"/>
<!--指定监听类-->
<property name="messageListener" ref="apMessageListener" />
<!--订阅topic模式-->
<property name="pubSubDomain" value="true"/><!-- default is false -->
<!--表示是一个持久订阅者,-->
<property name="subscriptionDurable" value="true"/>
</bean>
<!--监听容器2-->
<bean id="jmsContainer2"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="shopTopic" />
<property name="clientId" value="clientId_002"/>
<property name="messageListener" ref="shopMessageListener" />
<property name="pubSubDomain" value="true"/><!-- default is false -->
<property name="subscriptionDurable" value="true"/>
</bean>
<!--监听容器3-->
<bean id="jmsContainer3"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="userTopic" />
<property name="clientId" value="clientId_003"/>
<property name="messageListener" ref="userMessageListener" />
<property name="pubSubDomain" value="true"/><!-- default is false -->
<property name="subscriptionDurable" value="true"/>
</bean>
</beans>
此时,spring+mvc整合基本完成。