ActiveMQ与Spring集成

参考资料

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>


转载于:https://my.oschina.net/ksss/blog/663246

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值