activeMQ学习笔记 -- day03 整合spirng

来源:互联网 发布:java定位内存泄露 编辑:程序博客网 时间:2024/06/05 02:07

一、配置生产者

1、生产者整合spring时的配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/task   http://www.springframework.org/schema/task/spring-task-4.0.xsd"><!-- connectionFactory --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><constructor-arg name="brokerURL" value="tcp://192.168.200.134:61616" /></bean><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标连接工厂 --><property name="targetConnectionFactory" ref="targetConnectionFactory"></property></bean><!-- JMSTemplate --><bean class="org.springframework.jms.core.JmsTemplate"><!-- connectionFactory对应的是我们自己定义的spring提供的ide为connectionFactory的对象 --><property name="connectionFactory" ref="connectionFactory"></property></bean><!-- Destination --><bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="test-spring-queue"></constructor-arg></bean></beans>

2、生产者测试代码

@Testpublic void testSpring4QueueProducer(){ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");JmsTemplate jmsTemplate = app.getBean(JmsTemplate.class);Destination destination = app.getBean(Destination.class);jmsTemplate.send(destination,new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {return session.createTextMessage("测试spring与activemq整合");}});}

二、配置消费者

1、消费者整合spirng的配置文件

<?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:mvc="http://www.springframework.org/schema/mvc"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"xmlns:task="http://www.springframework.org/schema/task"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/task   http://www.springframework.org/schema/task/spring-task-4.0.xsd"><!-- connectionFactory --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><constructor-arg name="brokerURL" value="tcp://192.168.200.134:61616" /></bean><bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"><!-- 目标连接工厂 --><property name="targetConnectionFactory" ref="targetConnectionFactory"></property></bean><!-- Destination --><bean id="testQueue" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg value="test-spring-queue"></constructor-arg></bean><!-- 自定义的MessageListener --><bean id="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener"></bean><!-- Message容器 MessageListenerContainer --><bean id="myMessageContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory"></property><property name="destination" ref="testQueue"></property><property name="messageListener" ref="myMessageListener"></property></bean></beans>

2、消费者中消息监听器

package cn.e3mall.search.listener;import javax.jms.Message;import javax.jms.MessageListener;import javax.jms.TextMessage;public class MyMessageListener implements MessageListener {/** * 消息处理方法 */public void onMessage(Message message) {try {//接收消息if(message instanceof TextMessage){TextMessage textMessage = (TextMessage) message;System.out.println("接收到的消息是:" + textMessage.getText());}} catch (Exception e) {e.printStackTrace();}}}

3、消费者测试代码

@Testpublic void testSpring4QueueConsumer() throws Exception{ApplicationContext app = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");System.in.read();}