2019独角兽企业重金招聘Python工程师标准>>>

1 生产者

第一步:引用相关的jar包。

<dependency>
<groupId>org.springframework</groupId><artifactId>spring-jms</artifactId>
</dependency>
<dependency><groupId>org.springframework</groupId><artifactId>spring-context-support</artifactId>
</dependency>
<!-- 消息队列 -->
<dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId>
</dependency>

第二步:配置Activemq整合spring。配置ConnectionFactory applicationContext-activemq.xml

第三步:配置生产者。

使用JMSTemplate对象。发送消息。

第四步:在spring容器中配置Destination。

<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://192.168.36.40: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.command.ActiveMQQueue"><constructor-arg><value>spring-queue</value></constructor-arg></bean><!--这个是主题目的地,一对多的 --><bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"><constructor-arg value="topic" /></bean></beans>

第五步:代码测试

package com.shi.page;import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;/*** * @author: SHF* @date: 2018年3月16日 下午1:39:20* @Description: spring 整合activemq测试*/
public class springActiveMqTest {@Testpublic void testpublichMQ()throws Exception{//1 初始化spring容器ApplicationContext application=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");//2 从spring容器中获得jsmTemplate对象JmsTemplate jmsTemplate=application.getBean(JmsTemplate.class);//3 从spring容器中获取Distination对象Destination distination=(Destination) application.getBean("queueDestination");//4 使用jmsTempate对象发送消息jmsTemplate.send(distination,new MessageCreator() {@Overridepublic Message createMessage(Session session) throws JMSException {// 创建一个消息对象并返回return session.createTextMessage("spring active queue22222");}});}
}

2 消费者

2.2.2. 接收消息

e3-search-Service中接收消息。

第一步:把Activemq相关的jar包添加到工程中

第二步:创建一个MessageListener的实现类。

package com.shi.search.activemq;import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;/*** * @author: SHF* @date: 2018年3月16日 下午2:33:05* @Description: 消息队列监听对象,消费者。用来接受消息*/
public class MyMessageListener implements MessageListener {@Overridepublic void onMessage(Message paramMessage) {// 监听中 处理接收到的消息TextMessage textMessage=(TextMessage) paramMessage;try {String text = textMessage.getText();System.out.println(text);} catch (JMSException e) {e.printStackTrace();}}}

第三步:配置spring和Activemq整合。

<?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:context="http://www.springframework.org/schema/context"xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:jdbc="http://www.springframework.org/schema/jdbc"xmlns:jee="http://www.springframework.org/schema/jee" xmlns:aop="http://www.springframework.org/schema/aop"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/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://192.168.36.40: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="topic" /></bean><!-- 自己配置的监听器 实现了MessageListener接口 --><bean id="myMessageListener" class="com.shi.search.activemq.MyMessageListener"></bean><!-- 消息监听容器 --><bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="queueDestination" /><property name="messageListener" ref="myMessageListener" /></bean></beans>

第四步:测试代码。

package com.shi.solr;import java.io.IOException;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;/*** * @author: SHF* @date: 2018年3月16日 下午2:45:16* @Description: ActiveMQ消费者测试*/
public class ActiveMQCusomerTest {@Testpublic void customerTest() throws IOException{//只需要初始化spring容器,系统会帮我们监听的ApplicationContext application=new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");//等待System.in.read();}
}

转载于:https://my.oschina.net/u/3677987/blog/1635889

ActiveMQ与spring整合相关推荐

  1. java监控activemq,ActiveMQ与Spring整合-监听消息

    本课程全程使用目前比较流行的开发工具idea进行开发,涉及到目前互联网项目中常用的高并发解决方案技术, 如  dubbo,redis,solr,freemarker,activeMQ,springBo ...

  2. Spring整合ActiveMQ完成消息队列MQ编程

    <–start–> 第一步:新建一个maven,将工程命名为activeMQ_spring.在pom.xml文件中导入相关jar包. ①spring开发和测试相关的jar包: spring ...

  3. Spring整合JMS——基于ActiveMQ实现(一)

    Spring整合JMS--基于ActiveMQ实现(一) 1.1     JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息 ...

  4. spring整合使用activemq

    activemq简单的demo这里就不做演示了,今天介绍一下如何利用spring整合activemq,也是实际工作中涉及到的,希望对各位伙伴有所帮助, 1.安装activemq,为演示方便,我已经提前 ...

  5. ActiveMQ —— Spring 整合 ActiveMQ

    前文 消息中间件 -- 简介 ActiveMQ 下载.安装 ActiveMQ -- Java 连接 ActiveMQ(点对点) ActiveMQ -- Java 连接 ActiveMQ(发布订阅 To ...

  6. Spring整合Activemq

    目录 一.Pom.xml 二.Spring配置文件 三.队列 四.主题 一.Pom.xml <dependencies><!-- activemq核心依赖包 --><de ...

  7. java 消息队列详解_Java消息队列-Spring整合ActiveMq的详解

    本篇文章主要介绍了详解Java消息队列-Spring整合ActiveMq ,小编觉得挺不错的,现在分享给大家,也给大家做个参考.一起跟随小编过来看看吧 1.概述 首先和大家一起回顾一下Java 消息服 ...

  8. ActiveMQ学习总结(3)——spring整合ActiveMQ

    2019独角兽企业重金招聘Python工程师标准>>> 1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇 ...

  9. JMS--ActiveMq与spring整合(二)

    原文地址:http://blog.csdn.net/haoxingfeng/article/details/9167895 在我们和Spring 整合的时候,对于消费者而言我们有三种不同类型的监听器可 ...

最新文章

  1. UI基础篇-iOS中简单图片浏览器的实现
  2. C#.NET 比较好用的tcp通信模板(服务器端篇)
  3. 企业联合体的形式_母公司是否可以用子公司资质进行投标,且不以联合体的形式?...
  4. 普诺飞思公布发明者社区,启发基于事件视觉技术的创新
  5. 机器学习笔记(二)---- 线性回归
  6. 用户控件与自定义控件
  7. codeIgniter3 学习笔记五(表单验证)
  8. 日常投票评分:大多都是拉票
  9. WPF基础(八)bitmapImage.EndInit()引发异常 未找到适用于完成此操作的图像处理组件:可能是收发图片格式不一致导致的。
  10. 使用 tinypng 进行批量压缩
  11. 5步操作,解决SOLIDWORKS处理复杂零件时卡顿的问题
  12. SBB:替代固氮酶对非共生固氮可能的贡献
  13. Android中Home键的监听和拦截
  14. Unity做360度的全景照片
  15. 蓝牙BR/EDR和Bluetooth Smart的十大重要区别
  16. 同一个用户异地登陆踢人操作
  17. 用计算机弹歌万有引力,2011秋11计算机班物理单元测试题1.doc
  18. Keystone 认证服务
  19. Android踩坑之 couldnt find libClingSDK.so
  20. ELKStack实时分析Haproxy访问日志配置

热门文章

  1. Java 之HashSet、LinkedHashSet、TreeSet比较
  2. mysql下载哪一代版本好_潮一代更好的设计
  3. 概念验证_设置成功的UX概念验证
  4. (原+译)使用numpy.savez保存字典后读取的问题
  5. 这些故事说的都是你——译者带你读《硅谷革命》
  6. myeclipse+git pull项目报错
  7. PingingLab传世经典系列《CCNA完全配置宝典》-3.4 Trunk进阶配置
  8. 让你一周变聪明的大脑保健操
  9. Smarty目录结构和子目录路径问题
  10. Python的日志记录-logging模块的使用