参考资料
http://blog.csdn.net/jiuqiyuliang/article/details/48758203
该博文中有一到四章节对ActiveMQj进行介绍使用适合学习。
参考博文中的源码下载地址:http://download.csdn.net/detail/jiuqiyuliang/9141139
思考改进
基于配置实现
第一种配置
<!-- 定义Topic监听器 -->
<jms:listener-container client-id="111" destination-type="topic" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
<jms:listener subscription="11111" destination="test.topic" ref="topicReceiver"/>
</jms:listener-container>
第二种配置
<!-- 定义Topic监听器 -->
<bean id="receive" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destinationName" value="test.topic"></property>
<property name="messageListener" ref="topicReceiver"></property>
<property name="sessionAcknowledgeMode" value="1"></property>
<property name="subscriptionDurable" value="true"></property>
<property name="pubSubDomain" value="true"></property>
<property name="clientId" value="111"></property>
<property name="durableSubscriptionName" value="111a"></property>
</bean>
实现监听类为topicReceiver
基于注解实现
<bean id="jmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
<jms:annotation-driven/>
@Component("springQueueReciverTest")
public class SpringQueueReciverTest extends MessageListenerAdapter {
@JmsListener(destination = "test.queue", concurrency = "5-10")
public void onMessagehehe(Message message, Session session) throws JMSException {
try {
System.out.println(message);
} catch (MessageConversionException e) {
e.printStackTrace();
}
}
}
如何持续化订阅模式消息
第一种配置里修改属性
destination-type="durableTopic"
第二种配置里修改属性
<property name="subscriptionDurable" value="true"></property>
完整ActiveMQ.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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"
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/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.10.0.xsd">
<amq:connectionFactory id="amqConnectionFactory" clientID="111"
brokerURL="tcp://localhost:61616" userName="admin" password="admin" />
<bean id="connectionFactory"
class="org.springframework.jms.connection.CachingConnectionFactory">
<constructor-arg ref="amqConnectionFactory" />
<property name="sessionCacheSize" value="100" />
</bean>
<!-- ====Producer side start====-->
<!-- 定义JmsTemplate的Queue类型 -->
<bean id="jmsQueueTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="connectionFactory" />
<!-- 非pub/sub模型(发布/订阅),即队列模式 -->
<property name="pubSubDomain" value="false" />
<!-- 持续化 -->
<property name="deliveryPersistent" value="true" />
</bean>
<!-- 定义JmsTemplate的Topic类型 -->
<bean id="jmsTopicTemplate" class="org.springframework.jms.core.JmsTemplate">
<constructor-arg ref="connectionFactory" />
<!-- pub/sub模型(发布/订阅) -->
<property name="pubSubDomain" value="true" />
<!-- 持续化 -->
<property name="deliveryPersistent" value="true" />
</bean>
<!-- ====Producer side end====-->
<!-- ====Consumer side start====-->
<!-- 定义Queue监听器 -->
<jms:listener-container destination-type="queue" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
<jms:listener destination="test.queue" ref="queueReceiver" concurrency="1-1" />
</jms:listener-container>
<!-- 定义Topic监听器 -->
<jms:listener-container client-id="111" destination-type="durableTopic" container-type="default" connection-factory="connectionFactory" acknowledge="auto">
<jms:listener subscription="111a" destination="test.topic" ref="topicReceiver"/>
</jms:listener-container>
<!-- 监听器配置方式二
<bean id="receive" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory"></property>
<property name="destinationName" value="test.topic"></property>
<property name="messageListener" ref="topicReceiver"></property>
<property name="sessionAcknowledgeMode" value="1"></property>
<property name="subscriptionDurable" value="true"></property>
<property name="pubSubDomain" value="true"></property>
<property name="clientId" value="111"></property>
<property name="durableSubscriptionName" value="111a"></property>
</bean>
-->
<!-- ====Consumer side end==== -->
<!-- 监听&注解支持 -->
<!--
<bean id="jmsListenerContainerFactory" class="org.springframework.jms.config.DefaultJmsListenerContainerFactory">
<property name="connectionFactory" ref="connectionFactory"/>
</bean>
-->
<!--
<jms:annotation-driven/>
-->
</beans>