springboot整合rabbitMq实现消息延时发送
实现思路:利用mq的ttl设置消息失效时间 当达到设置时间后通过交换机到达死信队列中,消费者端绑定读取死信队列中信息来达到延时发送消息的功能。
demo 如下:
(1)在pom.xml 中引入rabbitMq相关包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
(2)创建rabbitMq连接的工具类
public class MqConnectionUtil { public static Connection getConnection() throws Exception {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setVirtualHost("virtualHost_wl");
factory.setUsername("admin");
factory.setPassword("admin");
Connection connection = factory.newConnection();
return connection;
}
}
(3)创建生产者发送消息
public class Send { private final static String QUEUE_NAME = "msg_ttl_queue"; public static void main(String[] argv) throws Exception {
//获取连接
Connection connection = MqConnectionUtil.getConnection();
//创建通道
Channel channel = connection.createChannel();
//设置延时队列
HashMap<String, Object> args = new HashMap<>();
args.put("x-dead-letter-exchange", "delay-exchange");
args.put("x-dead-letter-routing-key", "msg_ttl_routingKey");
channel.queueDeclare("delay_queue", true, false, false, args);
//声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
//绑定路由(delay-queue队列消息失效之后转发到msg_ttl_queue中)
channel.queueBind(QUEUE_NAME, "delay-exchange", "msg_ttl_routingKey");
//设置延时属性
AMQP.BasicProperties.Builder builder = new AMQP.BasicProperties.Builder();
//设置延时时间 1分钟
AMQP.BasicProperties properties = builder.expiration("60000").deliveryMode(2).build();
//消息内容
SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String message = "生产者创建时间: " + sft.format(new Date());
channel.basicPublish("", "delay_queue", properties, message.getBytes());
System.out.println("Sent: '" + message + "'");
//关闭通道和连接
channel.close();
connection.close();
}
}
(4)创建对应的消费者接受消息
public class Receive { private final static String QUEUE_NAME = "msg_ttl_queue"; public static void main(String[] argv) throws Exception {
//获取连接
Connection connection = MqConnectionUtil.getConnection();
//创建通道
Channel channel = connection.createChannel();
// 声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null);
// 定义队列的消费者
MyConsumer myConsumer = new MyConsumer(channel);
// 监听队列
channel.basicConsume(QUEUE_NAME, true, myConsumer);
}
}
自定义消费者类为:
public class MyConsumer extends DefaultConsumer {
public MyConsumer(Channel channel) {
super(channel);
} @Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
SimpleDateFormat sft = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println("消费者接受时间:" + sft.format(new Date()));
System.out.println("consumerTag: " + consumerTag);
System.out.println("envelope: " + envelope);
System.out.println("properties: " + properties);
System.out.println("body: " + new String(body));
}
}
生产者发送消息到延时队列:
到达设定的失效时间1分钟后到达指定的队列中:
设置的延时交换机为:
控制台打印信息为:
springboot整合rabbitMq实现消息延时发送的更多相关文章
- SpringBoot 整合 RabbitMQ 实现消息可靠传输
消息的可靠传输是面试必问的问题之一,保证消息的可靠传输主要在生产端开启 comfirm 模式,RabbitMQ 开启持久化,消费端关闭自动 ack 模式. 环境配置 SpringBoot 整合 Rab ...
- springboot项目整合rabbitMq涉及消息的发送确认,消息的消费确认机制,延时队列的实现
1.引入maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...
- springboot整合rabbitmq,支持消息确认机制
安装 推荐一篇博客https://blog.csdn.net/zhuzhezhuzhe1/article/details/80464291 项目结构 POM.XML <?xml version= ...
- SpringBoot整合Rabbitmq设置消息请求头
String str = "{\"origin\":\"BBC\",\"origin_coupon_id\":51,\" ...
- SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)
声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...
- springboot整合rabbitmq实现生产者消息确认、死信交换器、未路由到队列的消息
在上篇文章 springboot 整合 rabbitmq 中,我们实现了springboot 和rabbitmq的简单整合,这篇文章主要是对上篇文章功能的增强,主要完成如下功能. 需求: 生产者在启 ...
- springboot学习笔记-6 springboot整合RabbitMQ
一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...
- 一篇学习完rabbitmq基础知识,springboot整合rabbitmq
一 rabbitmq 介绍 MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced MessageQueue 高级消息队列协议 ...
- 【MQ中间件】RabbitMQ -- SpringBoot整合RabbitMQ(3)
1.前言说明 前面一篇博客中提到了使用原生java代码进行测试RabbitMQ实现多种交换机类型的队列场景.但是在项目中我们一般使用SpringBoot项目,而且RabbitMQ天生对于Spring的 ...
随机推荐
- 多测师讲解selenium ——切换窗口——打印句柄_高级讲师肖sir
(一)同一个窗口打开两个浏览器 from selenium import webdriverfrom time import sleepdrvier=webdriver.Chrome()url='ht ...
- CSS常见反爬技术
目录 利用字体 反爬原理 应对措施 难点: 利用背景 反爬原理 应对措施 利用伪类 反爬原理 应对措施 利用元素定位 反爬原理 应对措施 利用字符切割 反爬原理 应对措施 利用字体 反爬原理 反爬原理 ...
- lora网关
lora网关 lora物联网网关ZLAN9743可以实现RS232/485/422/以太网转 LoRa功能 是一款高性价比远距离无线通讯网关.LoRa和GPRS.4G方案相比它无需入网月租费,和Wif ...
- pytest文档53-命令行实时输出错误信息(pytest-instafail)
前言 pytest 运行全部用例的时候,在控制台会先显示用例的运行结果(.或F), 用例全部运行完成后最后把报错信息全部一起抛出到控制台. 这样我们每次都需要等用例运行结束,才知道为什么报错,不方便实 ...
- linux时间校准 设置时间为上海时区
[root@localhost log]# rm -f /etc/localtime [root@localhost log]# cp /usr/share/zoneinfo/Asia/Shang ...
- 理解 PHP 依赖注入 和 控制反转
理解 PHP 依赖注入 和 控制反转 要想理解 PHP 依赖注入 和 控制反转 两个概念,就必须搞清楚如下的两个问题: DI -- Dependency Injection 依赖注入 IoC -- ...
- wifi - 无线相关命令
1.Linux环境下的无线相关操作命令 interface 指代当前网卡 一般是 wifi0 , eth0 , ath1等 ifconfig - 常用查看网络设定及控制网卡(Windows下是ip ...
- Kettle学习笔记(一)
Kettle简介 Kettle是一款国外开源的ETL工具,纯java编写,可以在Windows.Linux.Unix上运行,s数据抽取高效稳定.Kettle 中文名称叫水壶,该项目的主程序员MATT ...
- 【应用服务 App Service】当使用EntityFrameWorkCore访问Sql Server数据库时,在Azure App Service会出现Cannot create a DbSet for ** because this type is not included in the model for the context的错误
问题情形 使用EF Core访问数据库,在本地运行正常,发布到App Service后,偶尔出现了Cannot create a DbSet for ** because this type is n ...
- 如何快速在vscode配置C/C++环境
目录 1.卸载重装vscode 2.下载vscode 3.下载MinGW 4.配置环境变量 5.配置c/c++环境 6.超完整的配置文件 7.常用扩展推荐 8.注意 9.后记 相信许多刚开始使用vsc ...