Spring整合ActiveMQ及多个Queue消息监听的配置
一、项目的搭建
(1)项目结构图

(2)pom.xml
- spring-jms
- activemq-all
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zxy</groupId>
<artifactId>spring-activemq</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-activemq</name>
<dependencies>
<!-- spring-jms 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- activemq依赖 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.1</version>
</dependency>
</dependencies>
</project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zxy</groupId>
<artifactId>spring-activemq</artifactId>
<version>1.0-SNAPSHOT</version>
<name>spring-activemq</name>
<dependencies>
<!-- spring-jms 依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>4.3.10.RELEASE</version>
</dependency>
<!-- activemq依赖 -->
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.9.1</version>
</dependency>
</dependencies>
</project>
二、整合+写代码
(1)配置applicationContext.xml
- 注册ActiveMQ连接工厂—— 配置与ActiveMQ相关的一些基本配置
- 注册Spring Cache连接工厂—— 类似于数据库连接池一样的东西,用于提高效率。后续Connection和Session都是通过它来获取,不直接和ActiveMQ发生关系
- 注册JmsTemplate —— 主要用来发送MQ消息
- 注册Queue监听 —— 主要用来配置MQ消息的消费者
<?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:jms="http://www.springframework.org/schema/jms"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
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-4.3.xsd">
<!-- 开启包扫描 (减少在xml中注册bean)-->
<context:component-scan base-package="com.zxy.mq" />
<!-- #### ActiveMq配置 start ####-->
<!-- 1. ActiveMq连接工厂 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<!--2. Spring Caching 连接工厂(类似数据库线程池的东西,减少连接的创建。) -->
<!-- 由于jmsTemplate每次发送消息都需要创建连接和创建session了,所以引入这个类似连接池的连接工厂,优化Mq的性能 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标连接工厂 指向 ActiveMq工厂 -->
<property name="targetConnectionFactory" ref="activeMQConnectionFactory" />
<!-- session缓存的最大个数-->
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 3.配置jmsTemplate,用于发送发送mq消息 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 设置 jmsTemplate 不支持订阅模式,即:只支持queue模式。
如果项目需要同时支持queue和topic,那么就需要另外注册一个jmsTemplate(把pubSubDomain设为true)-->
<property name="pubSubDomain" value="false"></property>
</bean>
<!-- 4.定义Queue监听器 -->
<jms:listener-container destination-type="queue" connection-factory="connectionFactory">
<!-- TODO 每添加一个queue的监听,都需要在这里添加一个配置 -->
<!-- 这样配置就可以方便的对多个队列监听 , 每增加一个队列只需要添加一个 jms:listener -->
<!-- destination:队列名称, ref:指向对应的监听器对象 -->
<!-- 示例: <jms:listener destination="queueName" ref="consumerBean" /> -->
</jms:listener-container>
<!-- #### ActiveMq配置 end ####-->
</beans>
<?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:jms="http://www.springframework.org/schema/jms"
xmlns:c="http://www.springframework.org/schema/c"
xsi:schemaLocation="http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-4.3.xsd
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-4.3.xsd">
<!-- 开启包扫描 (减少在xml中注册bean)-->
<context:component-scan base-package="com.zxy.mq" />
<!-- #### ActiveMq配置 start ####-->
<!-- 1. ActiveMq连接工厂 -->
<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
<property name="brokerURL" value="tcp://localhost:61616" />
</bean>
<!--2. Spring Caching 连接工厂(类似数据库线程池的东西,减少连接的创建。) -->
<!-- 由于jmsTemplate每次发送消息都需要创建连接和创建session了,所以引入这个类似连接池的连接工厂,优化Mq的性能 -->
<bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory">
<!-- 目标连接工厂 指向 ActiveMq工厂 -->
<property name="targetConnectionFactory" ref="activeMQConnectionFactory" />
<!-- session缓存的最大个数-->
<property name="sessionCacheSize" value="100" />
</bean>
<!-- 3.配置jmsTemplate,用于发送发送mq消息 -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="connectionFactory" />
<!-- 设置 jmsTemplate 不支持订阅模式,即:只支持queue模式。
如果项目需要同时支持queue和topic,那么就需要另外注册一个jmsTemplate(把pubSubDomain设为true)-->
<property name="pubSubDomain" value="false"></property>
</bean>
<!-- 4.定义Queue监听器 -->
<jms:listener-container destination-type="queue" connection-factory="connectionFactory">
<!-- TODO 每添加一个queue的监听,都需要在这里添加一个配置 -->
<!-- 这样配置就可以方便的对多个队列监听 , 每增加一个队列只需要添加一个 jms:listener -->
<!-- destination:队列名称, ref:指向对应的监听器对象 -->
<!-- 示例: <jms:listener destination="queueName" ref="consumerBean" /> -->
</jms:listener-container>
<!-- #### ActiveMq配置 end ####-->
</beans>
(2)写一个通用的MQ消息生产者
package com.zxy.mq.producer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
/**
* 通用的ActiveMQ queue消息生产者
* @author ZENG.XIAO.YAN
* @time 2018-11-13 10:48:20
* @version v1.0
*/
@Component
public class CommonMqQueueProducer {
@Autowired
private JmsTemplate jmsTemplate;
/**
* 发送点对点的文本类型的Mq消息
* @param queue 队列的名字
* @param message 文本消息(一般直接传输json字符串,所以可以认为文本消息是最通用的)
*/
public void send(String queue, String message) {
jmsTemplate.send(queue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
package com.zxy.mq.producer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Component;
/**
* 通用的ActiveMQ queue消息生产者
* @author ZENG.XIAO.YAN
* @time 2018-11-13 10:48:20
* @version v1.0
*/
@Component
public class CommonMqQueueProducer {
@Autowired
private JmsTemplate jmsTemplate;
/**
* 发送点对点的文本类型的Mq消息
* @param queue 队列的名字
* @param message 文本消息(一般直接传输json字符串,所以可以认为文本消息是最通用的)
*/
public void send(String queue, String message) {
jmsTemplate.send(queue, new MessageCreator() {
@Override
public Message createMessage(Session session) throws JMSException {
return session.createTextMessage(message);
}
});
}
}
(3)写2个消费者
package com.zxy.mq.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
@Component
public class TestAConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
// myQueueA的消费者
try {
String text = ((TextMessage) message).getText();
System.out.println(this.getClass().getSimpleName() + "接受到消息---->" + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.zxy.mq.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
@Component
public class TestAConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
// myQueueA的消费者
try {
String text = ((TextMessage) message).getText();
System.out.println(this.getClass().getSimpleName() + "接受到消息---->" + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.zxy.mq.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
@Component
public class TestBConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
// myQueueB的消费者
try {
String text = ((TextMessage) message).getText();
System.out.println(this.getClass().getSimpleName() + "接受到消息---->" + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
package com.zxy.mq.consumer;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;
import org.springframework.stereotype.Component;
@Component
public class TestBConsumer implements MessageListener {
@Override
public void onMessage(Message message) {
// myQueueB的消费者
try {
String text = ((TextMessage) message).getText();
System.out.println(this.getClass().getSimpleName() + "接受到消息---->" + text);
} catch (JMSException e) {
e.printStackTrace();
}
}
}
(4)queue监听器中配置Listener并指向消费者

三、测试
(1)测试代码
package com.zxy.mq.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zxy.mq.producer.CommonMqQueueProducer;
/**
* MQ消息测试类
* @author ZENG.XIAO.YAN
* @time 2018-11-15 14:04:35
* @version v1.0
*/
public class MqTestDemo {
private static ApplicationContext applicationContext;
// 静态代码块加载Spring容器
static {
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("spring 容器已启动。。。");
}
public static void main(String[] args) {
CommonMqQueueProducer mqQueueProducer = applicationContext.getBean(CommonMqQueueProducer.class);
for (int i = 1; i < 11; i++) {
// 奇数给myQueueA发,偶数给myQueueB发
if (i % 2 == 1) {
mqQueueProducer.send("myQueueA", "Mq消息A" + i);
} else {
mqQueueProducer.send("myQueueB", "Mq消息B" + i);
}
}
}
}
package com.zxy.mq.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zxy.mq.producer.CommonMqQueueProducer;
/**
* MQ消息测试类
* @author ZENG.XIAO.YAN
* @time 2018-11-15 14:04:35
* @version v1.0
*/
public class MqTestDemo {
private static ApplicationContext applicationContext;
// 静态代码块加载Spring容器
static {
applicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
System.out.println("spring 容器已启动。。。");
}
public static void main(String[] args) {
CommonMqQueueProducer mqQueueProducer = applicationContext.getBean(CommonMqQueueProducer.class);
for (int i = 1; i < 11; i++) {
// 奇数给myQueueA发,偶数给myQueueB发
if (i % 2 == 1) {
mqQueueProducer.send("myQueueA", "Mq消息A" + i);
} else {
mqQueueProducer.send("myQueueB", "Mq消息B" + i);
}
}
}
}
(2)测试结果

四、小结
Spring整合ActiveMQ及多个Queue消息监听的配置的更多相关文章
- 多线程消息监听容器配置[ 消费者spring-kafka配置文件]
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- Spring整合ActiveMQ,实现队列主题消息生产消费
1.引入依赖 pom.xml 1 <!-- activemq --> 2 <dependency> 3 <groupId>org.springframework&l ...
- Spring整合ActiveMQ实现消息延迟投递和定时投递
linux(centos)系统安装activemq参考:https://www.cnblogs.com/pxblog/p/12222231.html 首先在ActiveMQ的安装路径 /conf/ac ...
- spring 整合 ActiveMQ
1.1 JMS简介 JMS的全称是Java Message Service,即Java消息服务.它主要用于在生产者和消费者之间进行消息传递,生产者负责产生消息,而消费者负责接收消息.把它应用到 ...
- spring整合activemq发送MQ消息[queue模式]实例
queue类型消息 pom依赖 <dependency> <groupId>junit</groupId> <artifactId>junit</ ...
- Java消息队列-Spring整合ActiveMq
1.概述 首先和大家一起回顾一下Java 消息服务,在我之前的博客<Java消息队列-JMS概述>中,我为大家分析了: 消息服务:一个中间件,用于解决两个活多个程序之间的耦合,底层由Jav ...
- Java ActiveMQ 讲解(二)Spring ActiveMQ整合+注解消息监听
对于ActiveMQ消息的发送,原声的api操作繁琐,而且如果不进行二次封装,打开关闭会话以及各种创建操作也是够够的了.那么,Spring提供了一个很方便的去收发消息的框架,spring jms.整合 ...
- Spring整合ActiveMq消息队列
ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线.ActiveMQ 是一个完全支持JMS1.1和J2EE 1.4规范的 JMS Provider实现,尽管JMS规范出台已经是很久 ...
- spring整合ActiveMq
spring整合ActiveMq: 1:依赖的jar包: 2:spring-activemq.xml 的配置: 代码: <?xml version="1.0" enco ...
随机推荐
- Python 利用Python操作excel表格之openyxl介绍Part2
利用Python操作excel表格之openyxl介绍 by:授客 QQ:1033553122 欢迎加入全国软件测试交流qq群(群号:7156436) ## 绘图 c = LineChart() ...
- Elasticsearch Elasticsearch入门指导
Elasticsearch入门指导 By:授客 QQ:1033553122 1. 开启elasticsearch服务器 1 2. 基本概念 2 <1> 集群(Cluster) 2 < ...
- Android广播机制的基本使用
一提到广播我们第一感觉就会联想到小时候村里面的广播,安卓的广播机制也是类似于大喇叭.有发送广播的地方,也有接收广播的地方.但是具体怎么操作呢,我们来一步一步的看下去~ 安卓的广播种类 系统发送的广播: ...
- [随时更新][Android]小问题记录
此文随时更新,旨在记录平时遇到的不值得单独写博客记录的细节问题,当然如果问题有拓展将会另外写博客. 原文地址请保留http://www.cnblogs.com/rossoneri/p/4040314. ...
- 前后端分离djangorestframework——分页组件
Pagination 为什么要分页也不用多说了,大家都懂,DRF也自带了分页组件 这次用 前后端分离djangorestframework——序列化与反序列化数据 文章里用到的数据,数据库用的my ...
- 暂别SQL Server,转战MySQL和Redis
机缘巧合下找到一个愿意提供学习MySQL和Redis机会的岗位,于是要暂别SQL Server了. 后续一段时间会陆续总结三年来SQL Server相关的工作经验,当做是暂别前的总结.
- kali系统固化到固态硬盘小记(赠送给广大折腾党的笔记)
1.首先你需要一个移动硬盘和一个移动硬盘盒子(一根数据转换线,一般买盒子商家会赠送的) SSD硬盘要事先格式化一下格式,不然识别不出来 2.准备好Kali镜像,传送门在这里https://www.ka ...
- ACDSee清除旧版本残余
新建记事本,输入以下内容: reg delete "HKEY_LOCAL_MACHINE\SOFTWARE\ACD Systems" /f reg delete "HKE ...
- LeetCode算法题-Sum of Left Leaves(Java实现)
这是悦乐书的第217次更新,第230篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第85题(顺位题号是404).找到给定二叉树中所有左叶的总和.例如: 二叉树中有两个左叶 ...
- vuex最简单的
https://segmentfault.com/a/1190000009404727 "dependencies": { "axios": " ...