ActiveMQ整合Spring
2017-11-12 16:53
344 查看
前言:
在前面的文章中对
ActiveMQ的简单介绍和安装 、JMS两种模型的介绍和ActiveMQ的简单实例做了记录,这篇文章中对ActiveMQ与spring的整合做知识分享和记录,方便以后查看
整合代码下载:
csdn下载:ActiveMQ整合Spring实例
gitHub地址:ActiveMQ整合Spring实例
一、引入maven相关依赖
二、配置spring-activeMq.xml文件
为了方便测试,我这里配置的activeMQ整合spring的配置文件,只是单纯的在一个项目中进行了配置和测试。
如果是在分布式项目中:
1.将该配置文件复制到消费者项目(模块)中,配置对应的监听class
2.将复制过来的配置文件中的生产者配置删除
3.将原配置文件(生产者模块)的监听器配置删除
三、MessageListener代码实现
四、完成测试代码
生产者:
使用的是Queue的方式进行测试
因为我们在配置文件中已经配置了消息监听器,运行这个测试类的时候,就加载了整个spring-activemq容器,这样我们在运行生产者的方法时,他会一直监听并且接受消息
五、测试
1.运行testQueueConsumer()方法,加载spring-activemq容器,一直监听消息的发送
2.运行sendMessage(),发送消息
我们会发现控制台一直有消息输出:
![](https://img-blog.csdn.net/20171112163814138?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGxlaV95dWU=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
以上就是对ActiveMQ与Spring进行整合的实例,如果以上有什么问题或者错误,请大家及时指出,感激不尽,共同进步!
在前面的文章中对
ActiveMQ的简单介绍和安装 、JMS两种模型的介绍和ActiveMQ的简单实例做了记录,这篇文章中对ActiveMQ与spring的整合做知识分享和记录,方便以后查看
整合代码下载:
csdn下载:ActiveMQ整合Spring实例
gitHub地址:ActiveMQ整合Spring实例
一、引入maven相关依赖
<!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.2.4.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.2.4.RELEASE</version> </dependency> <!-- activemq --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.11.2</version> </dependency>
二、配置spring-activeMq.xml文件
为了方便测试,我这里配置的activeMQ整合spring的配置文件,只是单纯的在一个项目中进行了配置和测试。
如果是在分布式项目中:
1.将该配置文件复制到消费者项目(模块)中,配置对应的监听class
2.将复制过来的配置文件中的生产者配置删除
3.将原配置文件(生产者模块)的监听器配置删除
<?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"> <!--第一步:配置ConnectionFactory --> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://127.0.0.1:61616" /> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory" /> </bean> <!-- 第二步:配置生产者 --> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory" /> </bean> <!--这个是队列目的地,点对点的 --> <bean id="queueDestination" class="org.apache.activemq. b97b command.ActiveMQQueue"> <constructor-arg> <value>spring-queue</value> </constructor-arg> </bean> <!--这个是主题目的地,一对多的 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="itemAddTopic" /> </bean> <!-- 接收消息 --> <!-- 配置监听器 --> <bean id="myMessageListener" class="org.lpl.myDemo.message.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>
三、MessageListener代码实现
package org.lpl.myDemo.message; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class MyMessageListener implements MessageListener{ @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; //取消息内容 String text = textMessage.getText(); System.out.println("接受者:"+text); } catch (JMSException e) { e.printStackTrace(); } } }
四、完成测试代码
生产者:
使用的是Queue的方式进行测试
@Test public void sendMessage() throws Exception{ // 初始化spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/applicationContext-activemq.xml"); // 从spring容器中获得JmsTemplate对象 JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class); // 从spring容器中取Destination对象 Destination destination = (Destination) applicationContext.getBean("queueDestination"); // 使用JmsTemplate对象发送消息。 jmsTemplate.send(destination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { // 创建一个消息对象并返回 TextMessage textMessage = session.createTextMessage("spring activemq queue message"); return textMessage; } }); }消费者:
因为我们在配置文件中已经配置了消息监听器,运行这个测试类的时候,就加载了整个spring-activemq容器,这样我们在运行生产者的方法时,他会一直监听并且接受消息
@Test public void testQueueConsumer() throws Exception { // 初始化spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext( "classpath:spring/applicationContext-activemq.xml"); // 等待 System.in.read(); }
五、测试
1.运行testQueueConsumer()方法,加载spring-activemq容器,一直监听消息的发送
2.运行sendMessage(),发送消息
我们会发现控制台一直有消息输出:
以上就是对ActiveMQ与Spring进行整合的实例,如果以上有什么问题或者错误,请大家及时指出,感激不尽,共同进步!
相关文章推荐
- Java中间件JMS(二)之ActiveMQ整合spring(一)
- Spring整合ActiveMQ
- 5、ActiveMQ入门教程(五)--Spring和ActiveMQ整合
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- springboot整合activemq
- 学习淘淘商城第六十一课(Activemq与Spring整合接收消息)
- Spring整合JMS——基于ActiveMQ实现
- 利用activeMQ消息中间件整合spring mail发邮件
- JAVA学习笔记26——ActiveMQ讲解之整合Spring(二)
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- Spring整合activemq,实现单消费者消费
- ActiveMQ实战(五)--Spring整合ActiveMQ实现点对点与主题发布订阅通信
- Spring整合JMS(一)-基于ActiveMQ实现
- Spring整合JMS——基于ActiveMQ实现(一)
- 详解Java消息队列-Spring整合ActiveMq
- 深入浅出JMS(四)--Spring和ActiveMQ整合的完整实例
- spring整合消息中间件ActiveMQ简单使用
- JMS-Spring和ActiveMQ整合的完整实例--ActiveMQ入门之二
- SpringBoot 整合ActiveMQ 小Demo