创业的那些事儿-spring与activeMQ集成-queue消费者

<?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:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">

	<!-- failover 自动重连机制 -->
	<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL" value="failover:(tcp://localhost:61616)" />
	</bean>

	<!-- 使用连接池 -->
	<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
		<property name="connectionFactory" ref="targetConnectionFactory" />
		<property name="maxConnections" value="10" />
	</bean>
	<!-- 使用single connect (对于已经连接到MQ的线程始终使用同一条连接) -->
	<bean id="connectionFactory"
		class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="pooledConnectionFactory" />
	</bean>

	<bean id="destination" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg >
			<value>feed</value>
		</constructor-arg>
	</bean>
	
	<!-- 消息监听器 -->
	<bean id="messageListener" class="com.yysns.feed.FeedMessageListener"/>

	<!-- 消息监听容器 -->
	<bean id="jmsContainer"
		class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="connectionFactory" />
		<property name="destination" ref="destination" />
		<property name="messageListener" ref="messageListener" />
	</bean>
</beans>


对应实现的listener

package com.yysns.feed;

import javax.jms.Message;
import javax.jms.MessageListener;

public class FeedMessageListener implements MessageListener {

	@Override
	public void onMessage(Message feedMessage) {
	      //TODO
	}

}



已测试在activeMQ断开后,可以自动重连
ps:此代码在 渔歌开发中, 由于保密的问题故不能贴出太多代码,请见谅

你可能感兴趣的:(spring,activemq,jms)