springboot整合ActiveMQ1(基本使用)
- 基本使用,https://www.tapme.top/blog/detail/2018-09-05-10-38
- 主备模式,https://www.tapme.top/blog/detail/2018-09-06-10-38
说明:acitveMQ 版本为:5.9.1,springboot 版本为 2.0.3
一. 下载安装(windows)
官方下载地址:点我跳转,选择 windows 安装包下载,然后解压,解压后运行 bin 目录下的activemq.bat启动服务,无报错即可启动成功。默认管理地址为:localhost:8161/admin,默认管理员账号密码为admin/admin。
二. springboot 整合
1. 创建 springboot 项目
创建 springboot web 项目,加入 spring-boot-starter-activemq 依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
然后编辑配合文件,加上一个配置:61616 为 activeMQ 的默认端口,暂时不做其他配置,使用默认值。
spring:
activemq:
broker-url: tcp://localhost:61616
2. 创建生产者消费者
springboot 中 activeMQ 的默认配置为生产-消费者模式,还有一种模式为发布-订阅模式后面再讲。项目目录如下:

首先编写配置类 Config.java,代码如下
@Configuration
public class Config {
@Bean(name = "queue2")
public Queue queue2(){
return new ActiveMQQueue("active.queue2");
}
@Bean(name = "queue1")
public Queue queue1(){
return new ActiveMQQueue("active.queue1");
}
}
上面的代码建立了两个消息队列 queue1,queue2,分别由 queue1 和 queue2 这两个 Bean 注入到 Spring 容器中。程序运行后会在 activeMQ 的管理页面->queue 中看到如下:

生产者 Producer.java 代码如下:
@RestController
public class Producer {
@Autowired
private JmsMessagingTemplate jmsMessagingTemplate;
@Autowired()
@Qualifier("queue2")
private Queue queue2;
@Autowired()
@Qualifier("queue1")
private Queue queue1;
@GetMapping("/queue2")
public void sendMessage1(String message){
jmsMessagingTemplate.convertAndSend(queue2,"I'm from queue2:"+message);
}
@GetMapping("/queue1")
public void sendMessage2(String message){
jmsMessagingTemplate.convertAndSend(queue1,"I'm from queue1:"+message);
}
}
上面的类创建了两个 GET 接口,访问这两个接口分别向 queue1 和 queue2 中发送消息。
消费者 Comsumer.java 代码如下:
@Component //将该类注解到Spring 容器中
public class Comsumer {
//接受消息队列1消息
@JmsListener(destination = "active.queue1") //监听active.queue1消息队列
public void readActiveQueue11(String message){
System.out.println(1+message);
}
//接受消息队列1消息
@JmsListener(destination = "active.queue1")
public void readActiveQueue12(String message){
System.out.println(2+message);
}
//接受消息队列2消息
@JmsListener(destination = "active.queue2")
public void readActiveQueue21(String message){
System.out.println(1+message);
}
//接受消息队列2消息
@JmsListener(destination = "active.queue2")
public void readActiveQueue22(String message){
System.out.println(2+message);
}
}
上面的代码定义了 4 个消费者,每两个消费一个消息队列。
3. 运行
启动项目后分别向/queue1?message=niihao,/queue2?message=nihaoa 发送 http 请求,然后我们可以在控制台中看到如下输出:
2I'm from queue2:nihaoa
1I'm from queue2:nihaoa
2I'm from queue1:nihao
1I'm from queue1:nihao
消息都成功被消费者消费,从打印结果也可看出生产者消费者的一个特点:一个消息只会被一个消费者消费。同时在管理页面中可以看到:

每个消息队列有两个消费者,队列进入了三个消息,出了三个消息,说明消息都被消费掉了,如果注释掉消费者代码,再次运行,然后发送消息就会发现 MessagesEnqueued 数量大于 MessagesDequeued,然后再让消费者上线会立即消费掉队列中的消息。
本文原创发布于:https://www.tapme.top/blog/detail/2018-09-05-10-38
源码:https://github.com/FleyX/demo-project/tree/master/jms_demo
springboot整合ActiveMQ1(基本使用)的更多相关文章
- spring-boot整合mybatis(1)
sprig-boot是一个微服务架构,加快了spring工程快速开发,以及简便了配置.接下来开始spring-boot与mybatis的整合. 1.创建一个maven工程命名为spring-boot- ...
- SpringBoot整合Mybatis之项目结构、数据源
已经有好些日子没有总结了,不是变懒了,而是我一直在奋力学习springboot的路上,现在也算是完成了第一阶段的学习,今天给各位总结总结. 之前在网上找过不少关于springboot的教程,都是一些比 ...
- springboot整合mq接收消息队列
继上篇springboot整合mq发送消息队列 本篇主要在上篇基础上进行activiemq消息队列的接收springboot整合mq发送消息队列 第一步:新建marven项目,配置pom文件 < ...
- springboot整合mybaits注解开发
springboot整合mybaits注解开发时,返回json或者map对象时,如果一个字段的value为空,需要更改springboot的配置文件 mybatis: configuration: c ...
- SpringBoot整合Redis、ApachSolr和SpringSession
SpringBoot整合Redis.ApachSolr和SpringSession 一.简介 SpringBoot自从问世以来,以其方便的配置受到了广大开发者的青睐.它提供了各种starter简化很多 ...
- SpringBoot整合ElasticSearch实现多版本的兼容
前言 在上一篇学习SpringBoot中,整合了Mybatis.Druid和PageHelper并实现了多数据源的操作.本篇主要是介绍和使用目前最火的搜索引擎ElastiSearch,并和Spring ...
- SpringBoot整合Kafka和Storm
前言 本篇文章主要介绍的是SpringBoot整合kafka和storm以及在这过程遇到的一些问题和解决方案. kafka和storm的相关知识 如果你对kafka和storm熟悉的话,这一段可以直接 ...
- SpringBoot整合SpringCloud搭建分布式应用
什么是SpringCloud? SpringCloud是一个分布式的整体解决方案.SpringCloud为开发者提供了在分布式系统中快速构建的工具,使用SpringCloud可以快速的启动服务或构建应 ...
- SpringBoot整合RabbitMQ-整合演示
本系列是学习SpringBoot整合RabbitMQ的练手,包含服务安装,RabbitMQ整合SpringBoot2.x,消息可靠性投递实现等三篇博客. 学习路径:https://www.imooc. ...
随机推荐
- 20189220 余超《Linux内核原理与分析》第九周作业
理解进程调度时机跟踪分析进程调度与进程切换的过程 本章的基础知识总结 一般来说,进程调度分为三种类型:中断处理过程(包括时钟中断.I/O 中断.系统调用和异常)中,直接调用schedule,或者返回用 ...
- Java-JUC(十三):现在有两个线程同时操作一个整数I,做自增操作,如何实现I的线程安全性?
问题分析:正如i在多线程中如果想实现i的多线程操作,必须i要使用volitle来保证其内存可见性,但是i++自增操作不具备原子性操作,因此需要对i++这段代码确保其原子性操作即可. 方案1: 使用Re ...
- Flutter UI系统
我们可以看到,无论是Android SDK还是iOS的UIKit 的职责都是相同的,它们只是语言载体和底层的系统不同而已.那么可不可以实现这么一个UI系统:可以使用同一种编程语言开发,然后针对不同操作 ...
- NazoHell 攻略
http://hell.one-story.cn/hell-start.html Level 0: http://nazohell.one-story.cn/nazohell-start.html 跳 ...
- [LeetCode] 142. Linked List Cycle II 链表中的环 II
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 用浏览器控制台抓取shodan、搜索引擎、zone-h的结果
0x00 前言 大部分内容来自参考连接的内容,只是一种爬取内容的思路. 在很久以前自己会有爬取zone-h做目标测试的需求,但是总是有各种反爬限制.而且个别网址还有前端自动生成内容的功能,使用Java ...
- InfluxDB入门
InfluxDB是一个用于存储和分析时间序列数据的开源数据库 时序数据是基于时间的一系列的数据 时序数据库就是存放时序数据的数据库,并且需要支持时序数据的快速写入.持久化.多纬度的聚合查询等基本功能 ...
- 【视频开发】四大图像库:OpenCV/FreeImage/CImg/CxImage
1.对OpenCV 的印象:功能十分的强大,而且支持目前先进的图像处理技术,体系十分完善,操作手册很详细,手册首先给大家补计算机视觉的知识,几乎涵盖了近10年内的主流算法: 然后将图像格式和矩阵运算, ...
- 配置tomcat 加载指定的jar
# vi bin/catalina.sh
- vscode failed to excute git
将代码提交到github时 提示 “failed to excute git” 解决:配置git git config --global user.email "youremailid@ex ...