activeMQ和spring的整合
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的整合的更多相关文章
- activeMQ入门+spring boot整合activeMQ
最近想要学习MOM(消息中间件:Message Oriented Middleware),就从比较基础的activeMQ学起,rabbitMQ.zeroMQ.rocketMQ.Kafka等后续再去学习 ...
- ActiveMQ与Spring / SpringBoot 整合(四)
1. 对 Spring 的整合 1.1 所需jar 包 <!-- activeMQ jms 的支持 --> <dependency> <groupId>org.sp ...
- 04.ActiveMQ与Spring JMS整合
SpringJMS使用参考:http://docs.spring.io/spring/docs/current/spring-framework-reference/html/jms.html ...
- spring boot整合activemq消息中间件
spring boot整合activemq消息中间件 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi ...
- spring boot 整合activemq
1 Spring Boot与ActiveMQ整合 1.1使用内嵌服务 (1)在pom.xml中引入ActiveMQ起步依赖 <properties> <spring.version& ...
- ActiveMQ 笔记(四)Spring\SpringBoot 整合 Activemq
个人博客网:https://wushaopei.github.io/ (你想要这里多有) 一.Spring 整合Activemq 1.所需jar包 <dependencies> &l ...
- liunx 安装ActiveMQ 及 spring boot 初步整合 activemq
源码地址: https://gitee.com/kevin9401/microservice.git 一.安装 ActiveMQ: 1. 下载 ActiveMQ wget https://arch ...
- ActiveMQ和Tomcat的整合应用(转)
转自:http://topmanopensource.iteye.com/blog/1111321 ActiveMQ和Tomcat的整合应用 博客分类: ActiveMQ学习和研究 在Active ...
- ActiveMQ 与 Spring
1. ActiveMQ安装 1.1 下载(版本5.14.5) 点我官网下载 1.2 安装 解压下载的压缩文件到任意目录中(eg. C:\Program Files (x86)\apache-activ ...
随机推荐
- windows下有个目录名称中间有空格 java读目录空格变成%20 处理方法
String path=Parameter.class.getResource("").getPath();//得到路径//String path=Parameter.class. ...
- keil5 MDK warning:registered ARM compiler version not found in path
重装 打开keil5弹出窗口: warning:registered ARM compiler version not found in path... 解决: 增加系统环境变量 ARMCC5LIB ...
- Java IO浅析
1.File类 /** * * @author lenovo * * File类 * pathSeparator * separator * * File() * boolean createNewF ...
- Handling Touches - RN3
1. basic button format: <tag event caption /> <Button onPress={{}} title="I am button& ...
- 白话RPC
RPC,这个英文缩写在计算机专业里的意思是:Remote Procedure Call Protocol,远程过程调用协议,字面上的意思就是这个,不过还是有些懵逼. 下面就简单说明一下其内在原理,形象 ...
- Android 开发 VectorDrawable 矢量图 (一)了解Android矢量图与获取矢量图
VectorDrawable 矢量图 三部曲: Android 开发 VectorDrawable 矢量图 (一)了解Android矢量图与获取矢量图 Android 开发 VectorDrawabl ...
- Android Jetpack 概述
Android Jetpack Overview Android Jetpack Jetpack is a set of libraries, tools and architectural guid ...
- 关于Haclon使用GPU加速的代码实例
关于Haclon使用GPU加速的代码实例 read_image(Image, 'T20170902014819_58_2_1.bmp') *没有加加速并行处理 count_seconds(T1) to ...
- leetcode114
class Solution { public: void flatten(TreeNode* root) { while(root){ if(root->left){ TreeNode* pr ...
- component lists rendered with v-for should have explicit keys
错误:component lists rendered with v-for should have explicit keys 解析:使用vue 的v-for时,需要:key指定唯一key 文档:h ...