linux下 消息中间件ActiveMQ整合spring笔记二 接收消息
2017-08-20 13:03
686 查看
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.2.xsd"> <!-- JMS厂商提供的ConnectionFactory --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg name="brokerURL" value="tcp://192.168.25.127:61616"></constructor-arg> </bean> <!-- spring对ConnectionFactory的封装 --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="targetConnectionFactory"></property> </bean> <!--配置消息的Destination对象 queue方式 --> <bean id="test-queue" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg name="name" value="test-queue"></constructor-arg> </bean> <!--配置消息的Destination对象 topic方式 --> <bean id="test-topic" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg name="name" value="test-topic"></constructor-arg> </bean> <!-- 配置消息的接收者 --> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="test-queue" /> <property name="messageListener" ref="myMessageListener" /> </bean> <bean id="myMessageListener" class="com.taotao.search.listener.MyMessageListener"/> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="test-queue" /> <property name="messageListener" ref="myMessageListener" /> </bean> </beans>
创建一个实现MessageListener接口的类 用于接收消息
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage
4000
;
public class MyMessageListener implements MessageListener {
@Override
public void onMessage(Message message) {
TextMessage messages = (TextMessage) message;
try {
System.out.println("xioaxi"+messages.getText());
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
测试代码
import static org.junit.Assert.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class TestSpringActiveMQ {
@Test
public void test() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
System.in.read();
}
}
相关文章推荐
- linux下 消息中间件ActiveMQ整合spring笔记二 发消息
- 利用Spring与ActiveMQ整合发送、接收消息实例(Queue与Topic模式)
- Spring与ActiveMQ整合(多线程并发发送与接收消息)
- Spring整合ActiveMQ消息中间件
- 消息中间件-activemq实战之整合Spring(四)
- 消息中间件之ActiveMQ整合Spring实现邮箱发送(四)
- ActiveMQ消息中间件简明笔记(3)——Spring集成JMS接口
- 消息中间件-activemq实战整合Spring之Topic模式(五)
- 利用activeMQ消息中间件整合spring mail发邮件
- spring整合消息中间件ActiveMQ简单使用
- 消息中间件activemq-5.13.0整合spring
- 消息中间件-activemq实战之整合Spring(四)
- ActiveMq与spring的整合--发送和接收消息
- ActiveMQ 整合 Spring 发送和接收消息
- 学习淘淘商城第六十一课(Activemq与Spring整合接收消息)
- linux下消息中间件ActiveMQ的入门笔记一
- activeMQ消息中间件的与spring的整合
- 淘淘商城系列——ActiveMQ与Spring整合接收消息
- Java中间件JMS(四)之ActiveMQ整合spring之类转换(三)
- spring整合activemq发布订阅消息模式