SpringBoot集成ActiveMQ
前面提到了原生API访问ActiveMQ和Spring集成ActiveMQ。今天讲一下SpringBoot集成ActiveMQ.
SpringBoot就是为了解决我们的Maven配置烦恼而生,因此使用SpringBoot无疑就会让我们减少很多繁琐的xml配置。具体操作如下:
1.pom.xml
2.application.properties
3.启动类ActiveBootApplication
4.生产者Producer
5.消费者Consumer
6.测试代码
1.pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
2.application.properties
# 用户名和密码都是空
spring.activemq.broker-url=tcp://localhost:
spring.activemq.user=
spring.activemq.password=
spring.activemq.in-memory=true
# 不使用连接池
spring.activemq.pool.enabled=false
3.启动类ActiveBootApplication
@SpringBootApplication
@EnableJms //必须要启动
public class ActiveBootApplication { public static void main(String[] args) {
SpringApplication.run(ActiveBootApplication.class, args);
}
}
4.生产者Producer
@Service
public class Producer {
@Autowired // 也可以注入JmsTemplate,JmsMessagingTemplate对JmsTemplate进行了封装 ,JmsTemplate可以进行更为细微的操作
private JmsMessagingTemplate jmsTemplate; public void sendMessage(Destination destination, final String message){
jmsTemplate.convertAndSend(destination, message);
}
}
5.消费者Consumer
@Component
public class Consumer {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest.queue")
public void receiveQueue(String text) {
System.out.println(this.getClass().getName() + " 收到的报文为:" + text);
}
}
6.测试代码
@RunWith(SpringRunner.class)
@SpringBootTest //springboot测试注解
public class ActiveBootApplicationTests { @Autowired
private Producer producer; @Test
public void contextLoads() {
//定义队列模式,而非Topic模式
Destination destination = new ActiveMQQueue("mytest.queue"); for (int i = 0; i < 5; i++) {
String msg = "Hello World!" + i;
producer.sendMessage(destination, msg);
System.out.println("send:" + msg);
}
}
}
注意:
1.必须要启动@EnableJms
2.次示例没有池化
3.测试代码里面定义点对点模式还是订阅模式
下面再对池化的代码进行说明:
1.application.properties修改如下:
spring.activemq.broker-url=tcp://localhost:
spring.activemq.user=
spring.activemq.password=
spring.activemq.in-memory=true # 启动池化
spring.activemq.pool.enabled=true
# 最大连接数
spring.activemq.pool.max-connections=
#表示空闲多少时间将被清理,而expiry-timeout则表示创建之后多少时间后就会被清理(0表示永不过期)
spring.activemq.pool.idle-timeout=
spring.activemq.pool.expiry-timeout=
2.增加配置类ActiveMqConfiguration
@Configuration
public class ActiveMqConfiguration { @Value("${spring.activemq.user}")
private String usrName; @Value("${spring.activemq.password}")
private String password; @Value("${spring.activemq.broker-url}")
private String brokerUrl; //池化必须配置,否者会出现找不到org.springframework.jms.core.JmsMessagingTemplate的异常
@Bean
public ConnectionFactory connectionFactory(){
ActiveMQConnectionFactory connectionFactory
= new ActiveMQConnectionFactory();
connectionFactory.setBrokerURL(brokerUrl);
connectionFactory.setUserName(usrName);
connectionFactory.setPassword(password);
return connectionFactory;
} }
其它代码保持一致即可。
下面对Topic的监听进行说明:
1.需要指定消费者的监听类型为Topic,即订阅模式,在ActiveMqConfiguration增加代码:
@Bean("jmsTopicListenerContainerFactory")
public JmsListenerContainerFactory<?> jmsTopicListenerContainerFactory(
ConnectionFactory connectionFactory
){
DefaultJmsListenerContainerFactory factory
= new DefaultJmsListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setPubSubDomain(true);
return factory;
}
2.消费者注解增加containerFactory
@Component
public class Consumer {
// 使用JmsListener配置消费者监听的队列,其中text是接收到的消息
@JmsListener(destination = "mytest.queue",containerFactory="jmsTopicListenerContainerFactory")
public void receiveQueue(String text) {
System.out.println(this.getClass().getName() + " 收到的报文为:" + text);
}
}
注意containerFactory的值和前面定义Bean的名称保持一直。不写containerFactory就表示队列模式。
SpringBoot集成ActiveMQ的更多相关文章
- 使用SpringBoot集成ActiveMQ
SpringBoot是个好东西,好多java常用的东西都被集成进去了 JMS 在 Spring Boot 中的使用 使用Spring/Spring Boot集成JMS的陷阱 Spring-boot J ...
- SpringBoot集成ActiveMq消息队列实现即时和延迟处理
原文链接:https://blog.csdn.net/My_harbor/article/details/81328727 一.安装ActiveMq 具体安装步骤:自己谷歌去 二.新建springbo ...
- Web项目容器集成ActiveMQ & SpringBoot整合ActiveMQ
集成tomcat就是随项目启动而启动tomcat,最简单的方法就是监听器监听容器创建之后以Broker的方式启动ActiveMQ. 1.web项目中Broker启动的方式进行集成 在这里采用Liste ...
- Springboot简单集成ActiveMQ
Springboot简单集成ActiveMQ 消息发送者的实现 pom.xml添加依赖 <dependency> <groupId>org.springframework.bo ...
- SpringBoot集成Zipkin实现分布式全链路监控
目录 Zipkin 简介 Springboot 集成 Zipkin 安装启动 zipkin 版本说明 项目结构 工程端口分配 引入 Maven 依赖 配置文件.收集器的设置 编写 Controller ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(一)
你家小区下面有没有快递柜 近两年来,我们收取快递的方式好像变了,变得我们其实并不需要见到快递小哥也能拿到自己的快递了.对,我说的就是类似快递柜.菜鸟驿站这类的代收点的出现,把我们原来快递小哥必须拿着快 ...
- 从零开始学 Java - Spring 集成 ActiveMQ 配置(二)
从上一篇开始说起 上一篇从零开始学 Java - Spring 集成 ActiveMQ 配置(一)文章中讲了我关于消息队列的思考过程,现在这一篇会讲到 ActivMQ 与 Spring 框架的整合配置 ...
- 在Spring下集成ActiveMQ
1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...
- 在spring环境下集成ActiveMQ
1.参考文献 Spring集成ActiveMQ配置 Spring JMS异步发收消息 ActiveMQ 2.环境 在前面的一篇ActiveMQ入门实例中我们实现了消息的异步传送,这篇博文将如何在spr ...
随机推荐
- Vue 旅游网首页开发1-工具安装及码云使用
Vue 旅游网首页开发-工具安装及码云使用 环境安装 安装 node.js node.js 官网:https://nodejs.org/en/ 注册码云,创建私密仓库存储项目 码云:https://g ...
- cJSON库的简单介绍及使用
转载:http://www.cnblogs.com/liunianshiwei/p/6087596.html JSON 语法是 JavaScript 对象表示法语法的子集.数据在键/值对中:数据由逗号 ...
- linux 子shell subshell和函数
关于子shell, subshell 参考:http://blog.csdn.net/sosodream/article/details/5683515 系统引导时的进程为 "原始进程&qu ...
- CF1131D tarjan,拓扑
题目链接 541div2 http://codeforces.com/contest/1131/problem/D 思路 给出n序列和m序列的相对大小关系 构造出最大值最小的序列 缩点+拓扑 小的向大 ...
- 三星固态硬盘ssd产品线收集
目录 三星ssd按时间展示: 三星ssd后缀带a与不带a的区别,举其中一例: 下面是从三星中国的官网截图的产品线: 产品线列表: 总结: 参考: 最近在淘宝看到了很多拆机ssd,三星作为世界上唯一一家 ...
- HDU 3506 Monkey Party(区间DP)题解
题意:有n个石堆排成环,每次能合并相邻的两堆石头变成新石堆,代价为新石堆石子数,问最少的总代价是多少 思路:先看没排成环之前怎么做:用dp[i][j]表示合并i到j所需的最小代价,那么dp[i][j] ...
- 【做题】codechefCOUNTARI——分块FFT
记本题数组长度为\(n\),权值大小为\(m\). 首先,暴力显然是\(O(n^2)\)的. 先瞄一眼tag,然后发现这是FFT. 显然,问题的关键在于要满足i,j,k之间的位置关系.于是考虑分治FF ...
- hihoCoder week10 后序遍历
题目链接 https://hihocoder.com/contest/hiho10/problem/1 给出先序 中序 求 后序 #include <bits/stdc++.h> usi ...
- Vue.extend提供自定义组件的构造器
Vue.extend 返回的是一个“扩展实例构造器”,也就是预设了部分选项的Vue实例构造器.经常服务于Vue.component用来生成组件,可以简单理解为当在模板中遇到该组件名称作为标签的自定义元 ...
- 《C语言程序设计》指针篇<一>
指针 指针是C语言的精华,同时也是其中的难点和重点,我在近日对这一部分内容进行了重新的研读,把其中的一些例子自己重新编写和理解了一遍.此篇博客的内容即是我自己对此书例子的一些理解和总结. 一.大问题: ...