<?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:amq="http://activemq.apache.org/schema/core"
xmlns:jms="http://www.springframework.org/schema/jms"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd
http://www.springframework.org/schema/jms
http://www.springframework.org/schema/jms/spring-jms-4.1.xsd
http://activemq.apache.org/schema/core
http://activemq.apache.org/schema/core/activemq-core-5.12.1.xsd">
<!-- 自动扫描包路径下名字-->
<context:component-scan base-package="sa.Stu"/>
<mvc:annotation-driven/>
<!-- 配置mq的连接-->
<amq:connectionFactory id="amqConnectionFactory"
brokerURL="tcp://localhost:61616"
userName="admin"
password="admin"/>
<!-- 配置JMS连接工厂-->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory"/>
<property name="sessionCacheSize" value="100"/>
</bean>
<!-- 定义消息队列(Queue),默认的 目标地址 -->
<bean id="demoQueueDestination" class="org.apache.activemq.command.ActiveMQQueue">
<!-- 设置消息队列的名字 -->
<constructor-arg>
<value>Jaycekon</value>
</constructor-arg>
</bean>
<!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<property name="defaultDestination" ref="demoQueueDestination" />
<property name="receiveTimeout" value="10000" />
<!-- true是topic,false是queue,默认是false,此处显示写出false -->
<property name="pubSubDomain" value="false" />
</bean>
<!--配置消息队列监听者 ,自己定义的一个监听器类-->
<bean id="queueMessageListener" class="sa.Stu.Filter.QueueMessageListener">
</bean>
<!-- 显示注入消息监听容器(Queue),配置连接工厂,监听的目标是demoQueueDestination,监听器是上面定义的监听器 -->
<bean id="queueListenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="demoQueueDestination" />
<property name="messageListener" ref="queueMessageListener" />
</bean>
</beans>
网上的例子。
利用了 spring提供的spring-jms模块完成。
另一种写法
<?xml version="1.0" encoding="UTF-8"?>
<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">
<bean id="jmsTargetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL"><value>tcp://localhost:61616</value></property>
<property name="userName"><value>admin</value></property>
<property name="password"><value>admin</value>
</property>
</bean>
<!-- 建立连接池,activemq提供的 -->
<bean id="jmsPooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory" ref="jmsTargetConnectionFactory" />
<property name="maxConnections" value="5" />
</bean>
<!-- 这是spring 内在集成的的连接,可以实现对事物的管理,这样MQ的事物交给 spring来进行管理,CachingConnectionFactory
和SingleConnectionFactory 两种方式
有两种这里是融合spring与activeMQ的关键-->
<bean id="jmsConnectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<property name="targetConnectionFactory" ref="jmsPooledConnectionFactory" />
</bean>
<!--利用spring 的 jmsTemplate 的模版实现 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="jmsConnectionFactory" />
</bean>
<!--自定义一个 监听器 -->
<bean id="messageReceiver"
class="com.hkrt.app.offlinepos.listener.MercQuickApplyListener">
<property name="testService" ref="testService" />
/>
</bean>
<!--建立一个默认的 destination 地址 -->
<bean id="syncRechargeDestination" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>mercapply-queue</value>
</constructor-arg>
<!-- 消息监听容器 ,DefaultMessageListenerContainer 默认的spring容器监听器 -->
<bean id="listenerContainer"
class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsPooledConnectionFactory" />
<property name="destination" ref="syncRechargeDestination" />
<property name="messageListener" ref="messageReceiver" />
</bean>
<bean/>