淘淘商城系列——ActiveMQ与Spring整合接收消息
来源:互联网 发布:c语言int数组有多大 编辑:程序博客网 时间:2024/06/11 15:58
我们测试接收消息时换个工程来测试,就用taotao-search-service工程,要用这个工程来接收ActiveMQ消息就要先添加对activemq-all的依赖,如下图所示。
下面我们新建一个接收ActiveMQ队列消息的监听器类,该类继承自MessageListener,如下图所示。
为方便大家复制,现把MyMessageListener类的代码贴出。
/** * 接收ActiveMQ队列消息的监听器 * <p>Title: MyMessageListener</p> * <p>Description: </p> * <p>Company: www.itcast.cn</p> * @version 1.0 */public class MyMessageListener implements MessageListener { // onMessage是一个事件,当消息到达的时候,会调用这个方法 @Override public void onMessage(Message message) { // 取消息的内容 try { TextMessage textMessage = (TextMessage) message; // 取内容 String text = textMessage.getText(); System.out.println(text); // 其他业务逻辑... } catch (JMSException e) { e.printStackTrace(); } }}
下面就来整合ActiveMQ与Spring,我们需要新建一个ActiveMQ的配置文件,如下图所示。
同样为方便大家复制,现把applicationContext-activemq.xml配置文件的内容给出。
<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"> <!-- 真正可以产生Connection的ConnectionFactory,由对应的JMS服务厂商提供 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.25.129:61616" /> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory" /> </bean> <!--这个是队列目的地,点对点的 --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg> <value>spring-queue</value> </constructor-arg> </bean> <!--这个是主题目的地,一对多的 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="spring-topic" /> </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="queueDestination" /> <property name="messageListener" ref="myMessageListener" /> </bean></beans>
最后我们新建一个包和一个测试类,如下图所示。
同样为方便大家复制,现把ActiveMQTest单元测试类的代码给出。
public class ActiveMQTest { @Test public void testQueueConsumer() throws Exception { // 初始化Spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml"); // 一旦Spring容器初始化成功之后,程序就结束了,但我们不能让其结束,所以得让它在这等着。 // 你什么时候发消息,我就什么时候接收。 System.in.read(); }}
下面我们便来运行上面的测试方法,在Eclipse控制台会看到接收到了一条ActiveMQ队列消息,这是上文我们发送的ActiveMQ队列消息,由于当时没有消费者,所以这条消息一直留到现在。现在我们有了消费者,自然就消费了那条没有被消费的消息。我们还可以再发送几条ActiveMQ队列消息看看这个控制台是否都能接收的了。
我们再用JmsTemplate来发送一条消息试试,结果如下图所示,可以正常接收到。如果不想让测试类继续接收消息的话,可以在控制台直接敲回车即可。
阅读全文
0 0
- 淘淘商城系列——ActiveMQ与Spring整合接收消息
- 淘淘商城系列——Spring与ActiveMQ的整合及用JmsTemplate发送消息
- 学习淘淘商城第六十一课(Activemq与Spring整合接收消息)
- 淘淘商城系列——ActiveMQ发送topic消息和接收topic消息
- 淘淘商城系列——spring与freemarker的整合
- 淘淘商城系列——ActiveMQ发送queue消息
- 淘淘商城系列——接收Queue消息
- 学习淘淘商城第六十课(Spring与Activemq的整合及用JmsTemplate发送消息)
- 淘淘商城系列——ActiveMQ的介绍
- 淘淘商城系列——ActiveMQ的安装
- 学习淘淘商城第五十八课(ActiveMQ发送topic消息和接收topic消息)
- ActiveMQ 整合 Spring 发送和接收消息
- 淘淘商城系列——SSM框架整合之Dao层整合
- 淘淘商城系列——SSM框架整合之Service层整合
- 淘淘商城系列——SSM框架整合之表现层整合
- 淘淘商城系列——内容管理
- 淘淘商城系列——初始SolrCloud
- 淘淘商城系列——freemarker入门
- Saving loop 保存与循环
- 类的三大特征:封装,继承,多态
- 给GridView的单元格加上分割线
- Java的equal方法和==的一个坑
- RecyclerView添加footer实现上拉加载的自定义style
- 淘淘商城系列——ActiveMQ与Spring整合接收消息
- 欢迎使用CSDN-markdown编辑器
- datatables常用配置
- 左旋转字符串和翻转单词序列
- spring配置文件详解--真的蛮详细
- Android NDK编译Openssl-1.1.0f静态库
- Struts2
- 自定义View(二)橡皮擦效果
- Android 微信只能调起一次,问题集锦