MQ 系列之 JMSTemplate
1.1 简介
1.1.1 概述
JMSTemplate 是由 Spring 提供的一个 JMS 消息发送模板(与 JDBCTmplate 类似),可以用来方便地进行消息的发送,消息发送方法 convertAndSend 的第一个参数是消息队列,第二个参数是消息内容,@JmsListener 注解则表示相应的方法是一个消息消费者。
1.1.2 整合 JMSTemplate
☞ Spring 整合 JMSTemplate
☞ SpringBoot 整合 JMSTemplate
1.2 JMSTemplate 的使用
1.2.1 常用方法
消息接收可以使用消息监听的方式替代模板方法,但是发送的时候是无法代替的,必须使用 Spring 提供的 JmsTemplate 中的方法来进行发送操作。浏览只是针对 Queue 的概念,Topic 没有浏览。浏览是指获取消息而消息依然保持在 broker 中,而消息的接收会把消息从 broker 中移除。
方法 | 描述 |
---|---|
void send(Destination destination,MessageCreator messageCreator) | 将消息发送到指定的 Destination |
void send(String destinationName,MessageCreator messageCreator) |
将消息发送到指定的 Destination 这个方法和send(Destination destination,MessageCreator messageCreator) 做同样的事情,只是这个方法依赖于解析器和类型 |
void send(MessageCreator messageCreator) |
将消息发送到默认的 Destination,这个方法要求提前设置 defaultDestination 可以调用 setDefaultDestination(Destination destination) 或者 setDefaultDestinationName(String destinationName) 来满足这个前提 |
void convertAndSend(Destination destination,Object message) | 将 message 转换成 JMS 的 Message,并发送到指定的 Destination |
void convertAndSend(String destinationName,Object message) | 将 message 转换成 JMS 的 Message,并发送到指定的 Destination |
void convertAndSend(Object message) | 将 message 转换成 JMS 的 Message,并发送到默认的 Destination |
Message receive(Destination destination) | 从指定的 Destination 接收消息并返回 |
Message receive(String destinationName) | 从指定的 Destination 接收消息并返回 |
Message receive() | 从默认的 Destination 接收消息并返回 |
Object receiveAndConvert() | 参考 Message receive(),在此基础上做了转换 |
Object receiveAndConvert(Destination destination) | 参考 Message receive(Destination destination),在此基础上做了转换 |
Object receiveAndConvert(String destinationName) | 参考 Message receive(String destinationName),在此基础上做了转换 |
T browse(BrowserCallback action) |
从默认的 Queue 中浏览消息,要求设置了默认的 Destination,且类型为 Queue action 是一个回调对象,它负责浏览消息,并返回浏览的结果 |
T browse(Queue queue,BrowserCallback action) | 从指定的 Queue 中浏览消息 |
T browse(String queueName,BrowserCallback action) | 从指定 name 的 Queue 中浏览消息 |
T doInJms(Session session, QueueBrowser browser) |
浏览队列中的消息,并返回浏览的结果 这是一个回调方法,Spring 会为我们提供 QueueBrowser 对象,我们可以据此获取消息,并自由的转换。最后以我们希望的类型返回 |
void setDeliveryMode(int deliveryMode) | 设置是否持久化要发送的消息:1-非持久化;2-持久化 |
int getDeliveryMode() | 获取持久化模式的设置:1-非持久化;2-持久化 |
void setDeliveryPersistent(boolean deliveryPersistent) | 设置是否持久化要发送的消息,true-持久化;false-非持久化 |
void setPriority(int priority) | 为将要发送的消息设置优先级 |
int getPriority() | 获取优先级 |
void setTimeToLive(long timeToLive) | 设置消息的存活时间,毫秒单位 |
long getTimeToLive() | 获取消息的存活时间,毫秒单位 |
void setReceiveTimeout(long receiveTimeout) | 设置接收等待时间,毫秒单位 |
long getReceiveTimeout() | 获取接收等待时间,毫秒单位 |
1.2.2 发送消息
/*** Created with IntelliJ IDEA.** @author Demo_Null* @date 2020/8/5* @description JMS 发送消息*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class MyMQTest {@Autowiredprivate JmsTemplate jmsTemplate;@Testpublic void jms() {jmsTemplate.convertAndSend(new ActiveMQQueue("myTest"), "测试消息");}
}
1.2.3 接收消息
☞ 普通方式
/*** Created with IntelliJ IDEA.** @author Demo_Null* @date 2020/8/5* @description AMQ 接收消息*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest()
public class MyMQTest {@Autowiredprivate JmsTemplate jmsTemplate;@Testpublic void recevie() {Object myTest = jmsTemplate.receiveAndConvert("myTest");if (!Objects.isNull(myTest)) {System.out.println(myTest);}}
}
☞ 监听消息
/*** Created with IntelliJ IDEA.** @author Demo_Null* @date 2020/8/5* @description AMQ 监听消息*/
@Component
public class MyMQListener {@Autowiredprivate JmsTemplate jmsTemplate;@JmsListener(destination = "myTest")// 可监听多个队列// @JmsListeners(value = {@JmsListener(destination = "T1"), @JmsListener(destination = "T2")})public void listener(String msg) {if (!Objects.isNull(msg)) {System.out.println(msg);}}
}
MQ 系列之 JMSTemplate相关推荐
- 【SpringBoot MQ 系列】RabbitListener 消费基本使用姿势介绍
[MQ 系列]RabbitListener 消费基本使用姿势介绍 之前介绍了 rabbitmq 的消息发送姿势,既然有发送,当然就得有消费者,在 SpringBoot 环境下,消费可以说比较简单了,借 ...
- active mq topic消费后删除_【SpringBoot MQ 系列】RabbitListener 消费基本使用姿势介绍
[MQ 系列]RabbitListener 消费基本使用姿势介绍 之前介绍了 rabbitmq 的消息发送姿势,既然有发送,当然就得有消费者,在 SpringBoot 环境下,消费可以说比较简单了,借 ...
- stm32c8t6+dht11+MQ系列环境检测模块+oled显示屏(基于物联网的家庭环境检测系统设计)
元件清单: stm32f103c8t6.mq2 检测烟雾浓度(模拟量输出).mq7 检测一氧化碳浓度.mq135 检测空气质量.OLED屏幕(四引脚仅支持iic协议通信).dht11检测温湿度(数字量 ...
- 《吃透 MQ 系列》之扒开 Kafka 的神秘面纱
大家好,我是武哥.这是<吃透 MQ 系列>的第二弹,有些珊珊来迟,后台被好几个读者催更了,实属抱歉! 这篇文章拖更了好几周,起初的想法是:围绕每一个具体的消息中间件,不仅要写透,而且要控制 ...
- 【消息中间件MQ系列】消息队列之ActiveMQ、RocketMQ、RabbitMQ、Kafka不得不说的秘密
热门系列: [消息中间件MQ系列]RabbitMQ安装与使用,并与SpringBoot整合 程序人生,精彩抢先看 目录 1.消息队列/消息中间件介绍 1.1 消息中间件是什么 1.1.1 消息中间件的 ...
- MQ系列传感器怎么用,mq2传感器引脚怎么接
由于买的时候买错了,买成了传感器,而不是模块,到手是6根引脚的, 在网上找了半天资料,才找明白怎么接线, 所以个人使用还是建议直接买模块,省事,贵不了几元钱 但是还得讲一下传感器如何接线, MQ系列传 ...
- 基于 STM32 的语音识别智能家居控制系统的设计(LD3320语音识别芯片+ESP8266 WIFI模块+DHT11温湿度采集+MQ系列 烟雾及可燃气体+蜂鸣器+步进电机模拟窗帘+OLED液晶显示+
## **基于 STM32 的语音识别智能家居控制系统的设计(LD3320语音识别芯片+ESP8266 WIFI模块(阿里云 或ONENET或局域网)+DHT11温湿度采集+MQ系列 烟雾及可燃气体+ ...
- 基于STM32的学习记录--MQ系列计算公式
目录 MQ-2 定义 计算 MQ-4 定义 公式 MQ-7 定义 公式 MQ-135 定义 公式 所有的MQ系列传感器都需要一定的预热时间进行一个精度的校准 MQ-2 定义 //R2-1K. #def ...
- MQ系列传感器电压与浓度转换关系
MQ系列传感器电压与浓度转换关系 在网上搜了大量资料都没有对mq系列传感器电压与浓度转换关系的一个很好说明,也知道mq适合做定性而不是定量分析,对于结果要求不是非常精确的情况下怎么取得一个接近结果的值 ...
最新文章
- 如何高效读论文?剑桥CS教授亲授“三遍论”:论文最多读三遍,有的放矢,步步深入...
- [PHP] 算法-数组重复数字统计的PHP实现
- 自学架构设计的一个好方法
- C++希尔密码Hill Cipher算法(附完整源码)
- subst命令镜像虚拟磁盘指南(原创)
- swift 3.0 中使用 xib
- ASP.NET MVC 随想录——探索ASP.NET Identity 身份验证和基于角色的授权,中级篇
- 笔记系列------sqlloader的使用
- 3DMAX 2018 界面简介
- Android仿微信源码下载
- html怎么获取手机mac地址,js获取mac地址_Js获取客户端IP地址与MAC地址
- 怎样用计算机制作漫画,用电脑绘制漫画需要些什么?步骤是什么?
- 【OS学习笔记】二 汇编语言和汇编软件
- android录音mediaRecord\AudioRecord\openSL\PCM tinyalsa总结和优缺点
- M1忘记mysql密码
- 深入理解 Java中的 流 (Stream)
- 最近在使用vue的时候,发现竟然不会引入图片,悲哀啊,于是乎。。。
- 普林斯顿大学形状基准
- Autojs 抖音自动养号脚本+抖音直播控场脚本
- 如何把手头上的apk安装到电视上