http://www.cnblogs.com/shuai-server/p/8966299.html  这篇博客中介绍了activemq传递消息的两种方式,今天分享的是activemq框架和spring框架的整合使用。

(1)导入activemq的核心jar包和整合spring需要的两个jar包  context-support和jms.jar(使用spring的版本是4.2.7 activemq的版本是5.11.2)

<!--锁定版本号-->
<properties>
  <spring.version>4.2.4.RELEASE</spring.version>
  <activemq.version>5.11.2</activemq.version>
</properties> <!--添加依赖-->
<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>

(2)activemq整合spring,首先需要配置connectionFactory

<?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"> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://192.168.25.168:61616" />
</bean>
<!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory"
class="org.springframework.jms.connection.SingleConnectionFactory">
<!-- 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory -->
<property name="targetConnectionFactory" ref="targetConnectionFactory" />
</bean>
</beans>

(3)配置生产者对象

使用jmsTemplate对象,发送消息

<!-- 配置生产者 -->
<!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 -->
<property name="connectionFactory" ref="connectionFactory" />
</bean>

(4)在spring中配置Destination(有两种方式:队列 queue和主题 topic)

<!--这个是队列目的地,点对点的 -->
<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>

(5)可以进行junit进行测试发送消息

@Test
public void testSpringActiveMq() 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;
}
});
}

(6)接收消息

第一步:需要自定义messagerListener的实现类

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();
}
} }

第二步:在spring容器中配置自定义的消息监听对象

    <!-- 接收消息 -->
<!-- 配置监听器 -->
<bean id="myMessageListener" class="cn.e3mall.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>

第三步:初始化一个spring容器,等待接收消息

@Test
public void testQueueConsumer() throws Exception {
//初始化spring容器
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml");
//等待
System.in.read();
}

至此,spring和activemq的整合过程完毕。需要注意的是activemq的新版本中核心jar包中集成了spring框架中的一些方法,但是不是太全,建议大家选择版本的时候注意。

activeMQ和spring的整合的更多相关文章

  1. activeMQ入门+spring boot整合activeMQ

    最近想要学习MOM(消息中间件:Message Oriented Middleware),就从比较基础的activeMQ学起,rabbitMQ.zeroMQ.rocketMQ.Kafka等后续再去学习 ...

  2. ActiveMQ与Spring / SpringBoot 整合(四)

    1. 对 Spring 的整合 1.1 所需jar 包 <!-- activeMQ jms 的支持 --> <dependency> <groupId>org.sp ...

  3. 04.ActiveMQ与Spring JMS整合

        SpringJMS使用参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html ...

  4. spring boot整合activemq消息中间件

    spring boot整合activemq消息中间件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...

  5. spring boot 整合activemq

    1 Spring Boot与ActiveMQ整合 1.1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <properties> <spring.version& ...

  6. ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq

    个人博客网:https://wushaopei.github.io/    (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...

  7. liunx 安装ActiveMQ 及 spring boot 初步整合 activemq

    源码地址:  https://gitee.com/kevin9401/microservice.git 一.安装 ActiveMQ: 1. 下载 ActiveMQ wget  https://arch ...

  8. ActiveMQ和Tomcat的整合应用(转)

    转自:http://topmanopensource.iteye.com/blog/1111321 ActiveMQ和Tomcat的整合应用 博客分类: ActiveMQ学习和研究   在Active ...

  9. ActiveMQ 与 Spring

    1. ActiveMQ安装 1.1 下载(版本5.14.5) 点我官网下载 1.2 安装 解压下载的压缩文件到任意目录中(eg. C:\Program Files (x86)\apache-activ ...

随机推荐

  1. SSB基准测试

    SSB(Star Schema Benchmark)是麻省州立大学波士顿校区的研究人员定义的基于现实商业应用的数据模型,业界公认用来模拟决策支持类应用,比较公正和中立.学术界和工业界普遍采用它来评价决 ...

  2. 如何修改MSSQL的用户名

    Alter LOGIN sa DISABLE Alter LOGIN sa WITH NAME = [systemAccount] "systemAccount" 为SA的新名称, ...

  3. phpize是什么

    安装php(fastcgi模式)的时候,常常有这样一句命令:/usr/local/webserver/php/bin/phpize一.phpize是干嘛的?phpize是什么东西呢?php官方的说明: ...

  4. 我是一个录像机(NVR)

    我是一个网络录像机,简称NVR.我的前辈是DVR,我们的区别很简单,DVR接的是模拟摄像机,我连接的是IP摄像机. 我的前辈DVR比我辛苦,因为模拟摄像机的模拟信号连过来之后,他要进行数字化.编码压缩 ...

  5. JAVA 每周一 每周日 时间

    Calendar cal = Calendar.getInstance(); SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd&q ...

  6. You Only Look Once: Unified, Real-Time Object Detection

    论文下载:http://arxiv.org/abs/1506.02640  代码下载:https://github.com/pjreddie/darknet Abstract 作者提出一种新的目标检测 ...

  7. Linux 查看内存使用情况

    1 . top  : 用于实时显示 process 的动态 PID:进程的ID USER:进程所有 PR:进程的优先级别,越小越优先被执 VIRT:进程占用的虚拟内 RES:进程占用的物理内 SHR: ...

  8. Android Room 学习(一)

    Room简介 Room persistence库为SQLite提供了一个抽象层,以便在利用SQLite的全部功能的同时实现更强大的数据库访问. 该库可帮助您在运行应用程序的设备上创建应用程序数据的缓存 ...

  9. V2Ray断流异常

    V2Ray断流异常   1. 问题描述 最近一段时间发现,代理十分不稳定,经常出现“断流”,具体表现为:打开需要代理的站点,需要访问两次,第一次访问失败,需要再刷新一次.查看错误日志内容: Proxy ...

  10. python 数据分析库介绍

    1 引言 高效处理数据的python工具: 与外界进行交互: 读写各种文件格式和数据库 准备: 对数据进行清理.修整.整合.规范化.重塑.切片切换.变形等处理以便进行分析 转换: 对数据集做一些数学和 ...