activeMq-3 Spring整合activeMq
与jdbcTemplate相似的是,Spring也提供了JmsTemplate
生产者使用JmsTemplate生产消息,消费者实现一个监听器用于获取消息
项目用maven构建,jdk1.8,
文末提供免费源码下载
项目工程截图如下

activemq.properties 主要配置连接activemq相关连接参数
spring-activemq.xml 主要用于配置jmsTemplate
spring-context.xml 主要加载bean
我们重点关注spring-activemq.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
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.xsd"> <!-- activeMQ工厂: ConnectionFactory -->
<bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="${activemq.brokerURL}" />
<!-- <property name="userName" value="${activemq.userName}"></property>
<property name="password" value="${activemq.password}"></property> -->
</bean> <!--
ActiveMQ为我们提供了一个PooledConnectionFactory,通过往里面注入一个ActiveMQConnectionFactory
可以用来将Connection、Session和MessageProducer池化,这样可以大大的减少我们的资源消耗,要依赖于 activemq-pool包
-->
<bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory">
<property name="connectionFactory" ref="targetConnectionFactory" />
<property name="maxConnections" value="${activemq.pool.maxConnections}" />
</bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory -->
<bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <property name="targetConnectionFactory" ref="pooledConnectionFactory" />
</bean> <!--目的地-->
<bean id="msgQueue" class="org.apache.activemq.command.ActiveMQQueue">
<constructor-arg>
<value>${activemq.queueName}</value>
</constructor-arg>
</bean> <!-- Spring提供的JMS模板类,它可以进行消息发送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory"/>
<property name="pubSubDomain" value="false"></property> <!-- 设置p2p 还是主题订阅模式 默认是false 即p2p模式 -->
<property name="defaultDestinationName" value="${activemq.queueName}"></property>
</bean> <!-- 配置自定义监听:MessageListener -->
<bean id="messageListener" class="com.activemq.listener.ConsumerListener"></bean> <!-- 将连接工厂、目标对象、自定义监听注入jms模板 -->
<bean id="sessionAwareListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="connectionFactory" />
<property name="destination" ref="msgQueue" />
<property name="messageListener" ref="messageListener" />
</bean> </beans>
上面只是一些常见的配置 ,jmsTemplate还有很多配置

生产者代码
@Service
public class Producer {
@Autowired
private JmsTemplate jmsTemplate;
public void sendTextMessage(String text) {
jmsTemplate.send(new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
Message mes = session.createTextMessage(text);
return mes;
}
});
}
}
消费者代码(异步监听)
public class ConsumerListener implements SessionAwareMessageListener<Message>{
@Override
public void onMessage(Message message, Session session) throws JMSException {
if(message instanceof TextMessage)
{
System.out.println("Text "+((TextMessage)message).getText());
}
}
}
测试代码
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:spring-context.xml" })
public class TestProducer {
@Autowired
private Producer producer; @Test
public void send() {
producer.sendTextMessage("activemq-Spring!"); }
}
@RunWith 指明采用SpringJunit4进行测试
@ContextConfiguration 配置Spring核心配置文件位置
结果如下 :

注意:消息中间件适用于生产者能力强 而消费者能力弱的场景,就是大量写的情况。
源码下载:http://pan.baidu.com/s/1dFpPHw1
activeMq-3 Spring整合activeMq的更多相关文章
- Spring整合ActiveMQ实现消息延迟投递和定时投递
linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...
- Spring整合ActiveMQ及多个Queue消息监听的配置
消息队列(MQ)越来越火,在java开发的项目也属于比较常见的技术,MQ的相关使用也成java开发人员必备的技能.笔者公司采用的MQ是ActiveMQ,且消息都是用的点对点的模式.本文记录了实 ...
- spring整合ActiveMq
spring整合ActiveMq: 1:依赖的jar包: 2:spring-activemq.xml 的配置: 代码: <?xml version="1.0" enco ...
- 【报错】spring整合activeMQ,pom.xml文件缺架包,启动报错:Caused by: java.lang.ClassNotFoundException: org.apache.xbean.spring.context.v2.XBeanNamespaceHandler
spring版本:4.3.13 ActiveMq版本:5.15 ======================================================== spring整合act ...
- ActiveMQ学习总结------Spring整合ActiveMQ 04
通过前几篇的学习,相信大家已经对我们的ActiveMQ的原生操作已经有了个深刻的概念, 那么这篇文章就来带领大家一步一步学习下ActiveMQ结合Spring的实战操作 注:本文将省略一部分与Acti ...
- spring 整合 ActiveMQ
1.1 JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...
- Java消息队列-Spring整合ActiveMq
1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 消息服务:一个中间件,用于解决两个活多个程序之间的耦合,底层由Jav ...
- ActiveMQ (三) Spring整合JMS入门
Spring整合JMS入门 前提:安装好了ActiveMQ ActiveMQ安装 Demo结构: 生产者项目springjms_producer: pom.xml <?xml versio ...
- 消息中间件ActiveMQ及Spring整合JMS
一 .消息中间件的基本介绍 1.1 消息中间件 1.1.1 什么是消息中间件 消息中间件利用高效可靠的消息传递机制进行平台无关的数据交流,并基于数据通信来进行分布式系统的集成.通过提供消息传递和消息排 ...
随机推荐
- js源生ajax
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 对于使用JDBC连接mysql数据时The server time zone value '¤¤°ê¼Ð·Ç®É¶¡'...的异常问题解决。
相信很多小伙伴和我一样遇到了这类问题,在使用JDBC连接mysql数据库的时候发生SQLException如下所示的异常情况! java.sql.SQLException: The server ti ...
- java泛型的作用及实现原理
一.泛型的介绍 泛型是Java 1.5的新特性,泛型的本质是参数化类型,也就是说所操作的数据类型被指定为一个参数.这种参数类型可以用在类.接口和方法的创建中,分别称为泛型类.泛型接口.泛型方法. Ja ...
- android 开发 View _5_ Paint详解
转载:http://blog.csdn.net/abcdef314159 //Paint的setStyle,Style共有3种 setStyle(Style style) Paint.Style.FI ...
- 63.1拓展之纯 CSS 创作一个摇摇晃晃的 loader
效果地址:https://scrimba.com/c/cqKv4VCR HTML code: <div class="loader"> <span>Load ...
- leetcode96
class Solution { public: int numTrees(int n) { vector<,); f[]=; f[]=; ;i<=n;i++){ ;j<=i;j++ ...
- error: ‘kEmptyString’ is not a member of ‘google::protobuf::internal’
最近安装caffe,突然报这个错: .build_release/src/caffe/proto/caffe.pb.h: In member function ‘void caffe::ImageDa ...
- English-商务英文邮件例句100句
最常用最专业的商务英文邮件例句100句——塞依SAP培训 字体大小:大 | 中 | 小2013-08-27 17:24 阅读(74) 评论(0) 分类:sap职场 1. I am writing t ...
- jsp页面中比较“接收数据”与“页面循环数据”是否相等
页面中关系运算符: -lt 小于 -le 小于或者等于 -gt 大于 -ge 大于或者等于 -eq 等于 -ne 不等于 判空:<c:if test="${empty count ...
- [mybatis]Example的用法
Example类是什么? Example类指定如何构建一个动态的where子句. 表中的每个non-BLOB列可以被包括在where子句中. 例子是展示此类用法的最好方式. Example类可以用来生 ...