Spring读书笔记-------使用activeMq

在spring中使用activeMq

版本:spring 2.5.6 activeMq:5.1.2

配置信息

1.包含主文件:/src/main/resources/applicationContext-jms.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:tx="http://www.springframework.org/schema/tx"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
	<import resource="jms/applicationContext-common.xml" />
	<import resource="jms/applicationContext-consumer.xml" />
	<import resource="jms/applicationContext-producer.xml" />
</beans>

 2.主要配置文件:src/main/resources/jms/activemq.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans>
	<broker useJmx="false" persistent="false" xmlns="http://activemq.apache.org/schema/core">
		<transportConnectors>
			<transportConnector uri="tcp://localhost:61616" />
		</transportConnectors>
	</broker>
</beans>

 3.配置queue和topic:src/main/resources/jms/applicationContext-common.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<bean id="broker" class="org.apache.activemq.xbean.BrokerFactoryBean">
		<property name="config" value="classpath:jms/activemq.xml" />
	</bean>
	
	<bean id="jmsFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<property name="brokerURL">
			<value>tcp://localhost:61616</value>
		</property>
	</bean>

	<bean id="pooledJmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
		<property name="connectionFactory" ref="jmsFactory"/>
	</bean>

	<!-- Spring JMS Template -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="pooledJmsFactory" />
	</bean>

	<bean id="notifyQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg value="notifyQueue" />
	</bean>

	<bean id="notifyTopic" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg value="notifyTopic" />
	</bean>
</beans>

 4.信息生产者:src/main/resources/jms/applicationContext-producer.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<description>JMS生产者演示配置</description>

	<!-- a sample POJO which uses a Spring JmsTemplate	 -->
	<bean id="producer" class="com.tnt.check.jms.Producer">
		<property name="jmsTemplate" ref="jmsTemplate" />
		<property name="notifyQueue" ref="notifyQueue" />
		<property name="notifyTopic" ref="notifyTopic" />
	</bean>
</beans>

 5.消费者:src/main/resources/jms/applicationContext-consumer.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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

	<description>JMS消费者演示配置</description>
<!--  -->
	<bean id="queueConsumer" class="com.tnt.check.jms.QueueConsumer">
		<property name="jmsTemplate" ref="jmsTemplate" />
		<property name="notifyQueue" ref="notifyQueue" />
	</bean>

	<bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer" lazy-init="false">
		<property name="connectionFactory" ref="jmsFactory" />
		<property name="destination" ref="notifyTopic" />
		<property name="messageListener" ref="topicListener" />
	</bean>

	<bean id="topicListener" class="com.tnt.check.jms.TopicListener" />
	
</beans>

6。发送信息

    jmsTemplate.convertAndSend(notifyQueue, map);

以上内容主要参考spring side 3中的配置。

相关推荐