实现思路:利用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实现消息延时发送的更多相关文章

  1. SpringBoot 整合 RabbitMQ 实现消息可靠传输

    消息的可靠传输是面试必问的问题之一,保证消息的可靠传输主要在生产端开启 comfirm 模式,RabbitMQ 开启持久化,消费端关闭自动 ack 模式. 环境配置 SpringBoot 整合 Rab ...

  2. springboot项目整合rabbitMq涉及消息的发送确认,消息的消费确认机制,延时队列的实现

    1.引入maven依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactI ...

  3. springboot整合rabbitmq,支持消息确认机制

    安装 推荐一篇博客https://blog.csdn.net/zhuzhezhuzhe1/article/details/80464291 项目结构 POM.XML <?xml version= ...

  4. SpringBoot整合Rabbitmq设置消息请求头

    String str = "{\"origin\":\"BBC\",\"origin_coupon_id\":51,\" ...

  5. SpringBoot系列八:SpringBoot整合消息服务(SpringBoot 整合 ActiveMQ、SpringBoot 整合 RabbitMQ、SpringBoot 整合 Kafka)

    声明:本文来源于MLDN培训视频的课堂笔记,写在这里只是为了方便查阅. 1.概念:SpringBoot 整合消息服务 2.具体内容 对于异步消息组件在实际的应用之中会有两类: · JMS:代表作就是 ...

  6. springboot整合rabbitmq实现生产者消息确认、死信交换器、未路由到队列的消息

    在上篇文章  springboot 整合 rabbitmq 中,我们实现了springboot 和rabbitmq的简单整合,这篇文章主要是对上篇文章功能的增强,主要完成如下功能. 需求: 生产者在启 ...

  7. springboot学习笔记-6 springboot整合RabbitMQ

    一 RabbitMQ的介绍 RabbitMQ是消息中间件的一种,消息中间件即分布式系统中完成消息的发送和接收的基础软件.这些软件有很多,包括ActiveMQ(apache公司的),RocketMQ(阿 ...

  8. 一篇学习完rabbitmq基础知识,springboot整合rabbitmq

    一   rabbitmq 介绍 MQ全称为Message Queue,即消息队列, RabbitMQ是由erlang语言开发,基于AMQP(Advanced MessageQueue 高级消息队列协议 ...

  9. 【MQ中间件】RabbitMQ -- SpringBoot整合RabbitMQ(3)

    1.前言说明 前面一篇博客中提到了使用原生java代码进行测试RabbitMQ实现多种交换机类型的队列场景.但是在项目中我们一般使用SpringBoot项目,而且RabbitMQ天生对于Spring的 ...

随机推荐

  1. linux 内存泄露检测工具

    Valgrind Memcheck 一个强大开源的程序检测工具 下载地址:http://valgrind.org/downloads/current.html Valgrind快速入门指南:http: ...

  2. mycat 全局表

    全局表的作用 在分片的情况下,当业务表因为规模而进行分片以后,业务表与这些附属的字典表之间的关联,就成了比较棘手的问题,考虑到字典表具有以下几个特性: 变动不频繁 数据量总体变化不大 数据规模不大,很 ...

  3. C++ 虚函数简介!程序员必学知识,掌握编程从对象开始!

    本文将简单探究一下 c++ 中的虚函数实现机制.主要基于 vs2013 生成的 32 位代码进行研究,相信其它编译器(比如, gcc )的实现大同小异. 先从对象大小开始 假设我们有如下代码,假设 i ...

  4. zabbix安装中文语言包及中文乱码的解决(zabbix5.0)

    一,zabbix不能配置中文界面的问题: 1, zabbix5.0 系统安装后,web界面不能选择使用中文 系统提示: You are not able to choose some of the l ...

  5. centos8平台安装ansible2.9

    一,ansible的用途: ansible是基于python开发的自动化运维工具, 它基于SSH远程连接服务, 可以实现批量系统配置.批量软件部署.批量文件拷贝.批量运行命令等多个运维功能 因为基于s ...

  6. Linux系统及第三方应用官方文档

    通过在线文档获取帮助 http://www.github.com https://www.kernel.org/doc/html/latest/ http://httpd.apache.org htt ...

  7. springboot入门系列(一):简单搭建springboot项目

    Spring Boot 简单介绍 Spring Boot 本身并不提供Spring框架的核心特性以及扩展功能,只是用于快速.敏捷地开发新一代基于Spring框架的应用程序.也就是说,它并不是用来替代S ...

  8. Jquery中$("").事件()和$("").on("事件","指定的元素",function(){});的区别(jQuery动态绑定事件)

    这个是在学习时不懂的问题,记录下来方便查看 转至https://www.cnblogs.com/mr-wuxiansheng/p/7136864.html //绑定 下一页 的点击事件 $(" ...

  9. webpack5文档解析(下)

    声明:所有的文章demo都在我的仓库里 代码分离 代码分离的有点在于: 切割代码,生成不同的打包文件,按需加载这些文件. 每个bundle的体积更小 控制外部资源的加载顺序 常用的方法有: 入口起点: ...

  10. CSS常见标签类型

      块级(block): div,ul,li,h1~h6,p 独自占据一行 可设置宽高 行内(inline): span,a 占据所设置字体大小 不可设置宽高 上下margin值不会占用空间 行内块( ...