Springboot 整合 JMS
ActiveMQ
JMS 仅支持 Java 平台
。
由于 JMS 是一套标准,所以 SpringBoot 整合 JMS 必然是整合 JMS 的某一个实现。
Apache ActiveMQ 是一个开源的消息中间件,完全支持 JMS 1.1 规范,支持多种编程语言( C、C++、C#、Delphi、Erlang、AdobeFlash、Haskell,Java、Javascript、Perl、PHP、Pike、Python、Ruby )和协议 (OpenWire、REST、STOMP、WS-Notification、MQTT、XMPP、AMQP ),且其提供了集群支持。
我们使用 JMS 一般是使用 spring-JMS 和 ActiveMQ 相结合,通过 SpringBoot 为我们配置好的
JmsTemplate 发送消息到指定目的地 Destination。
一、安装 ActiveMQ 并启动
注:
JDK 版本需要 1.7 及以上才行
。
1、windows 版本
1、到 Apache官方网站下载最新的 ActiveMQ 的安装包,下载链接如下:ActiveMQ
下载。
(1)选择 ActiveMQ 5
(2)选择 windows,进行下载
2、下载后解压到本地目录下,解压目录如下:
3、打开 bin 目录,启动 activemq.bat 如果机器是 64 位,则打开 win64 目录,双击 activemq.bat 进行启动。 (注意:不要直接打开 bin 目录下的 activemq.bat,要选择 win64 或 win32 目录下的 activemq.bat
)
4、activemq.bat 运行结果如下:
5、ActiveMQ 启动成功!成功之后在浏览器输入ActiveMQ的管理页面地址( 本机地址是 http://127.0.0.1:8161 ),可以看到 ActiveMQ 的管理页面,用户名和密码默认都是 admin
,如下:
6、ActiveMQ 的相关配置
1)
用户名密码配置
在 apache-activemq-5.14.5\conf\jetty-realm.properties 文件,格式是用户名:密码 ,角色名。
2)用户名权限配置信息
记录在 apache-activemq-5.14.5\conf\jetty.xml 文件
2、Linux 版本
1、保证 Java 环境:JDK1.7 及以上
2、下载 ActiveMQ
wget http://mirrors.hust.edu.cn/apache//activemq/5.15.12/apache-activemq-5.15.12-bin.tar.gz
3、解压下载的文件
tar -zxvf apache-activemq-5.15.12-bin.tar.gz
4、启动 ActiveMQ,命令如下
cd apache-activemq-5.15.12 cd bin/ # 切换目录到 activemq 的 bin 目录下 ./activemq start # 启动 activemq
5、输入服务器 http://ip:port 进行访问 ActiveMQ 管理页面,ActiveMQ 默认端口号是 8161。
二、SpringBoot 整合 ActiveMQ
1、添加依赖
两个 springboot 项目分别作为消息提供者(provider)和消费者(consumer)
提供者和消费者分别添加如下依赖:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
<!--消息队列连接池-->
<!--springboot 2.0及以上-->
<dependency><groupId>org.messaginghub</groupId><artifactId>pooled-jms</artifactId><version>1.0.4</version>
</dependency>
2、配置消息队列
(1)在两个项目中的 application.properires 配置消息队列
# failover:(tcp://localhost:61616,tcp://localhost:61617)
# tcp://localhost:61616
spring.activemq.broker-url=tcp://localhost:61616
#true 表示使用内置的MQ,false则连接服务器
# 用户密码
spring.activemq.user=admin
spring.activemq.password=admin
spring.activemq.in-memory=false
#true表示使用连接池;false时,每发送一条数据创建一个连接
spring.activemq.pool.enabled=true
#连接池最大连接数
spring.activemq.pool.max-connections=10
#空闲的连接过期时间,默认为30秒
spring.activemq.pool.idle-timeout=30000
#强制的连接过期时间,与idleTimeout的区别在于:idleTimeout是在连接空闲一段时间失效,而expiryTimeout不管当前连接的情况,只要达到指定时间就失效。默认为0,never
spring.activemq.pool.expiry-timeout=0
#如果是点对点(queue),那么此处默认应该是false,如果发布订阅,那么一定设置为true
spring.jms.pub-sub-domain=true
(2)在提供者和消费者的启动类添加 @EnableJms 开启消息队列
// 1、提供者
@SpringBootApplication
@EnableJms //启动消息队列
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}
}// 2、消费者
@SpringBootApplication
@EnableJms //启动消息队列
public class ConsumerApplication {public static void main(String[] args) {SpringApplication.run(ConsumerApplication.class, args);}
}
@EnableJms 会启动 jms 的注解扫描即发现 @JmsListener 注释的方法创建消息监听容器,相当于 <jms:annotation-d riven/>
3、服务提供者 provider
(1)配置类定义消息队列
import javax.jms.Queue;
import org.apache.activemq.command.ActiveMQQueue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class JMSBean {//定义存放消息的队列@Beanpublic Queue queue() {return new ActiveMQQueue("activeMQQueue");}
}
(2)ProviderController 发送消息
import javax.jms.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class ProviderController {//注入存放消息的队列,用于下列方法一@Autowiredprivate Queue queue;//注入springboot封装的工具类@Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;@RequestMapping("send")public void send(String name) {//方法一:添加消息到消息队列jmsMessagingTemplate.convertAndSend(queue, name);//方法二:这种方式不需要手动创建queue,系统会自行创建名为test的队列//jmsMessagingTemplate.convertAndSend("test", name);}
}
这里的 jmsMessagingTemplate 是用 @Autowired 注解自动注入的,在整个工程里都没有配置实例化的地方。
jmsMessagingTemplate 的实例化是 springboot 的 autoconfigure 自动注入的。
实现可以参见 JmsAutoConfiguration.java 的代码。当我们引入了 jms 相关的包,比如 activemq 的包,又定义了 ConnectionFactory 的实例,那么他就会自动生成一个 JmsTemplate 实例。
(3)在 ActiveMQ 管理页面中查看发送的消息
1)启动 provider,向消息队列添加数据
2)查看 ActiveMQ 管理页面
Number Of Pending Messages:消息队列中待处理的消息
Number Of Consumers:消费者的数量
Messages Enqueued:累计进入过消息队列的总量
Messages Dequeued:累计消费过的消息总量
4、服务消费者 consumer
(1)定义消费者的消息接收器:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.stereotype.Component;@Component
public class ConsumerService {@Autowiredprivate JmsMessagingTemplate jmsMessagingTemplate;// 使用JmsListener配置消费者监听的队列@JmsListener(destination = "activeMQQueue")// SendTo 会将此方法返回的数据, 写入到 OutQueue 中去.@SendTo("SQueue")public String handleMessage(String name) {System.out.println("成功接受:" + name);return "成功接受:" + name;}
}
(2)启动消费者
Springboot 整合 JMS相关推荐
- SpringBoot整合JMS
SpringBoot整合JMS 文章目录 SpringBoot整合JMS 前言 一.JMS.AMQP概念以及区别 二.ActiveMQ安装 三.SpringBoot整合ActiveMQ:队列模式实例 ...
- ActiveMQ入门-ActiveMQ跟SpringBoot整合发送接收Queue
很多教程中,会介绍原生demo,原生demo在实际开发中不会用到,而且代码量比较大,而且不好理解,也没什么用,所以我们课程不介绍.我们的课程主打实战,所以只会介绍跟SpringBoot整合的方案. 新 ...
- SpringBoot整合 ActiveMQ、SpringBoot整合RabbitMQ、SpringBoot整合Kafka
1.概念:SpringBoot 整合消息服务2.具体内容对于异步消息组件在实际的应用之中会有两类:· JMS:代表作就是 ActiveMQ,但是其性能不高,因为其是用 java 程序实现的:· AMQ ...
- Springboot整合ActiveMQ发送邮件
虽然ActiveMQ以被其他MQ所替代,但仍有学习的意义,本文采用邮件发送的例子展示ActiveMQ 文章目录 1. 生产者 1.1 引入maven依赖 1.2 application.yml配置 1 ...
- activeMQ基础学习和SpringBoot整合activeMQ案例
昨天仔细研究了activeMQ消息队列,也遇到了些坑,昨天晚上也写了篇文章记录坑的内容,其实上篇文章(SpringBoot整合activeMQ消息队列手动签收(Session.CLIENT_ACKNO ...
- RabbitMq详解+SpringBoot整合RabbitMq快速入门
1概述: 1.1.什么是MQ 消息队列(Message Queue,简称MQ),从字面意思上看,本质是个队列,FIFO先入先出,只不过队列中存放的内容是message而已. 其主要用途:不同进程Pro ...
- springboot整合ActiveMQ(点对点和发布订阅)
springboot整合ActiveMQ(点对点和发布订阅) ActiveMQ是什么,为什么使用MQ 是基于 Java 中的 JMS 消息服务规范实现的一个消息中间件. 1.系统解耦 采用中间件之后, ...
- SpringBoot整合RabbitMq实战(一)
1 Spring AMQP 简介 Spring AMQP项目是一个引入Spring核心概念用于基于高级消息队列(AMQP)的解决方案的开发,它提供了一个模板用于发送和接受消息的高级抽象.它对基于消息驱 ...
- SpringBoot整合第三方技术学习笔记(自用)
SpringBoot整合第三方技术学习笔记 搬运黑马视频配套笔记 KF-4.数据层解决方案 KF-4-1.SQL 回忆一下之前做SSMP整合的时候数据层解决方案涉及到了哪些技术?MySQL数据库与My ...
最新文章
- 深度丨AI挑战人类情感!机器人写诗出书背后透露了什么?
- Python 3 的新特性zz
- window10运行python弹出商店_解决 win10 命令行下运行 python 弹出 Windows 应用商店
- Day02:requests请求库,selenium请求库
- NPAPI插件开发详细记录:插件开发入门
- GET 和 POST请求的本质区别是什么?看完觉得自己太无知了...
- mips 系统桥_系统功能调用实现简单输入输出(mips)
- 电源管理总线 (PMBus)
- 中国姓氏数据库 java_中国姓的数据库(sql版),下你妹CSDN,又不是你的资源要你妹积分!...
- 最新Axure谷歌浏览器Chrome扩展程序安装方法
- 解决lay out无法使用闪退问题,SketchUp 2022 MAC中文 (草图大师) 支持M1intel芯片,支持monterey最新系统
- 基于ssm的超市进销存管理系统
- 51单片机c语言的延时程序,51单片机延时程序
- 随机抽取一名同学回答问题,7/4更新一次
- 阿里CEO张勇:“亲亲节”预示未来年轻人生活方式
- 伸缩式工具刀行业调研报告 - 市场现状分析与发展前景预测
- RK3588 Android12开机log分析
- 影像的滑动窗口裁切与拼接(附代码)
- 幼儿抽象逻辑思维举例_熊孩子到底在想什么—幼儿思维干货速记!
- 绩效管理对 HR 的重要性