概述:

在rabbitmq中我们可以给消息设定过期时间LLT(Time to Live),在消息发送后过期时间段内未被消费,则系统会将其删除,被删除的消息将会进入死信队列。关于设置消息的过期时间有两种设置方式。1,可以设置在消息队列上,则经过该消息队列的消息都会使用该消息对列的过期时间;2,也可以将过期时间设置在消息体上,对消息进行单独的消息过期时间设置。如果在经过一个设置了过期时间的队列的并且自身也设置了过期时间的消息,则其过期时间取决于两者时间较小的一个。

接下来我们本章将在上一章《RabbitMq(八) SpringBoot整合RabbitMQ 生产者代码实现》的基础上进行调整,对消息进行设置不同场景的过期时间。大致实现步骤如下:

  1. 创建rabbitmq的spring 配置资源文件
  2. 在配置文件中声明消息队列信息,包含队列名称,过期时间等
  3. 将配置资源文件在启动类上进行导入
  4. 在发送消息时,指定2步骤中声明的队列名称

一、配置文件信息

在SpringBoot工程的资源目录下创建spring目录,并创建rabbitmq.xml配置文件,引入rabbit命名空间。xml文件代码如下:

  1.  
    <?xml version="1.0" encoding="UTF-8"?>
  2.  
    <beans xmlns="http://www.springframework.org/schema/beans"
  3.  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.  
    xmlns:rabbit="http://www.springframework.org/schema/rabbit"
  5.  
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6.  
    http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
  7.  
     
  8.  
    <!-- 定义过期队列及其属性,不存在则自动创建-->
  9.  
    <rabbit:queue id="my_ttl_queue" name="my_ttl_queue" auto-declare="true" >
  10.  
    <rabbit:queue-arguments>
  11.  
    <!-- 投递该消息的队列 ,中消息如果在6秒之内没有被消费则进行删除 -->
  12.  
    <entry key="x-message-ttl" value-type="long" value="6000" />
  13.  
    </rabbit:queue-arguments>
  14.  
    </rabbit:queue>
  15.  
     
  16.  
    </beans>

二、在springBoot工程的启动类上导入一种的配置资源文件:

通过在启动类上使用@ImportResource 注解导入配置信息,代码如下:

  1.  
    package com.xiaohui.rabbitmq;
  2.  
     
  3.  
    import org.springframework.boot.SpringApplication;
  4.  
    import org.springframework.boot.autoconfigure.SpringBootApplication;
  5.  
    import org.springframework.context.annotation.ImportResource;
  6.  
     
  7.  
     
  8.  
    /**
  9.  
    * 生产者启动类
  10.  
    */
  11.  
    @SpringBootApplication
  12.  
    @ImportResource("classpath:spring/rabbitmq.xml")
  13.  
    public class ProducerApplication {
  14.  
     
  15.  
    public static void main(String[] args) {
  16.  
    SpringApplication.run(ProducerApplication.class,args);
  17.  
    }
  18.  
    }

三、编写调用请求方法测试消息队列过期时间

我们在之前篇章的web层Controller中添加一个方法sendTTLMsg。并在内部固定消息队列为我们资源文件中配置的消息队列“my_ttl_queue”。Controller代码如下:

  1.  
    package com.xiaohui.rabbitmq.controller;
  2.  
     
  3.  
    import com.xiaohui.rabbitmq.config.RabbitMqConfig;
  4.  
    import org.springframework.amqp.core.Message;
  5.  
    import org.springframework.amqp.core.MessageProperties;
  6.  
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
  7.  
    import org.springframework.beans.factory.annotation.Autowired;
  8.  
    import org.springframework.web.bind.annotation.GetMapping;
  9.  
    import org.springframework.web.bind.annotation.RequestParam;
  10.  
    import org.springframework.web.bind.annotation.RestController;
  11.  
     
  12.  
    @RestController
  13.  
    public class MsgController {
  14.  
     
  15.  
     
  16.  
    @Autowired
  17.  
    RabbitTemplate rabbitTemplate;
  18.  
     
  19.  
    @GetMapping("/sendTTLMsg")
  20.  
    public String sendTTLMsg(@RequestParam String msg){
  21.  
    MessageProperties properties = new MessageProperties();
  22.  
    properties.setExpiration("3000");
  23.  
    Message msgobj = new Message(msg.getBytes(),properties);
  24.  
    rabbitTemplate.convertAndSend("my_ttl_queue",msgobj);
  25.  
    return "消息发送成功!";
  26.  
    }
  27.  
    }

我们访问url进行条用Controller进行调用发送消息代码,并可以通过rabbitmq控制台查看队列信息情况。

消息发送之后rabbit的控制台我们可以看到会新增一个消息队列,并且我们可以看到队列中消息从0变成1后,在过期时间之后又从1变为0。

四、编写调用请求方法测试消息过期时间

我们在web层的Controller中编写请求方法并通过代码实现单条消息的创建以及通过配置设置消息过期时间。controller代码如下:

  1.  
    package com.xiaohui.rabbitmq.controller;
  2.  
     
  3.  
    import com.xiaohui.rabbitmq.config.RabbitMqConfig;
  4.  
    import org.springframework.amqp.core.Message;
  5.  
    import org.springframework.amqp.core.MessageProperties;
  6.  
    import org.springframework.amqp.rabbit.core.RabbitTemplate;
  7.  
    import org.springframework.beans.factory.annotation.Autowired;
  8.  
    import org.springframework.web.bind.annotation.GetMapping;
  9.  
    import org.springframework.web.bind.annotation.RequestParam;
  10.  
    import org.springframework.web.bind.annotation.RestController;
  11.  
     
  12.  
    @RestController
  13.  
    public class MsgController {
  14.  
     
  15.  
     
  16.  
    @Autowired
  17.  
    RabbitTemplate rabbitTemplate;
  18.  
     
  19.  
     
  20.  
    @GetMapping("/sendTTLMsg")
  21.  
    public String sendTTLMsg(@RequestParam String msg){
  22.  
    MessageProperties properties = new MessageProperties();
  23.  
    properties.setExpiration("3000");
  24.  
    Message msgobj = new Message(msg.getBytes(),properties);
  25.  
    rabbitTemplate.convertAndSend("my_ttl_queue",msgobj);
  26.  
    return "消息发送成功!";
  27.  
    }
  28.  
    }

我们访问url进行条用Controller进行调用发送消息代码,并可以通过rabbitmq控制台查看队列信息情况。同样我们可以在rabbit控制台中查看消息处理情况,我们可以通过隔秒刷新看到此时消息会在3秒时间内从0变1后又从1变0。同时也可以看出在既有设置失效时间的队列上以及设置时间的消息上,以时间小的为最终失效时间。

RabbitMq消息过期时间TTL介绍的更多相关文章

  1. RabbitMQ(三)RabbitMQ消息过期时间(TTL)

    在RabbitMQ(二)AMQP协议mandatory和immediate标志位区别中我们提到,在RabbitMQ3.0以后的版本里,去掉了immediate参数支持,要实现类似的确认功能要使用TTL ...

  2. 【RabbitMQ 实战指南】一 过期时间TTL

    RabbitMQ 可以对消息和队列设置过期时间(TTL) 1.设置消息的TTL 目前有两种方式可以设置消息的TTL 第一种方式是通过队列属性设置,队列中所有消息都有相同的过期时间 第二种方式是对消息本 ...

  3. ActiveMQ队列消息过期时间设置和自动清除解决方案

    版本 apache-activemq-5.15.3 1.消息过期设置 参数详情 1)message过期则客户端不能接收 2)ttlCeiling:表示过期时间上限(程序写的过期时间不能超过此时间,超过 ...

  4. RabitMq过期时间TTL

    第一种:给消息设置过期时间 启动一个插件 @Bean public DirectExchange DirectExchange() { return new DirectExchange(" ...

  5. rabbitmq - (消息队列) 的基本原理介绍

    介绍 MQ全称为Message Queue, 是一种分布式应用程序的的通信方法,它是消费-生产者模型的一个典型的代表,producer往消息队列中不断写入消息,而另一端consumer则可以读取或者订 ...

  6. C# .net 使用rabbitmq消息队列——EasyNetQ插件介绍

    EasyNetQ 是一个简洁而适用的RabbitMQ .NET类库,本质上是一个在RabbitMQ.Client之上提供服务的组件集合.

  7. RabbitMQ消息队列+安装+工具介绍

    1.MQ为Message Queue,消息队列是应用程序和应用程序之间的通信方法 2. 多种开发语言支持,其实就是一个驱动,如连接数据库的mysql驱动,oracle驱动等. 3. 4.采用以下语言开 ...

  8. RabbitMQ之TTL(Time-To-Live 过期时间)

    本文转载自RabbitMQ之TTL(Time-To-Live 过期时间) 概述 RabbitMQ可以对消息和队列设置TTL. 目前有两种方法可以设置.第一种方法是通过队列属性设置,队列中所有消息都有相 ...

  9. RabbitMQ 设置消息的TTL(过期时间)

    我们在RabbitMQ中发布消息时,在代码中有两种方法设置某个队列的消息过期时间: 1.针对队列来说,可以使用x-message-ttl参数设置当前队列中所有消息的过期时间,即当前队列中所有的消息过期 ...

  10. rabbitMq 学习笔记(二) 备份交换器,过期时间,死信队列,死信队列

    备份交换器 备份交换器,英文名称为 Altemate Exchange,简称庙,或者更直白地称之为"备胎交换器". 生产者在发送消息的时候如果不设置 mandatory 参数, 那 ...

随机推荐

  1. 静态分析工具及使用总结(二)CheckStyle

    这里主要介绍三种开源的工具,PMD.CheckStyle和FindBugs,着重是在Ant里的调用,据说商业软件JTest也是著名的代码分析工具,哈哈,要花钱的没有用过. Checkstyle (ht ...

  2. Elasticsearch之性能优化

    elasticsearch 使用有时候会出现CPU飙升,内存爆满情况,需要进行一些优化设置. 1.  一定要用es自带id生产策略 2. 设置副本数量为0,写入完可恢复 put  localhost: ...

  3. pg_index

    在pg11之后,引入了indnkeyatts字段,根据官方文档解释其作用:The number of key columns in the index, not counting any includ ...

  4. three.js 性能优化之模型转化与压缩

    模型转换 obj转gltf 安装插件 npm i -g obj2gltf 执行转换命令 obj2gltf -i 11-6.obj -o 11-6.gltf -u 模型压缩 安装gltf-pipelin ...

  5. 内存Fuzz和WinAFL

    文章一开始发表在微信公众号 https://mp.weixin.qq.com/s/XSPrmBb44J8BUpKsj0cwGQ 内存Fuzz和WinAFL FoxitReader 软件分析 目前Fuz ...

  6. Flutter TextField设置值后光标位置偏移

    Flutter TextField设置值后光标位置偏移 一般用controller设置值是这样设置的 TextEditingController controller = TextEditingCon ...

  7. CMake构建学习笔记19-OpenSSL库的构建

    1. 概述 OpenSSL是一个开源的加密工具包和库,主要实现了安全套接字层(SSL)和传输层安全(TLS)协议,以及各种加密算法.数字签名.消息摘要.加密证书等功能.这个库可以说是Web开发尤其是H ...

  8. 【前端】白天/黑夜主题切换:JS读取XML预设主题实现黑白主题切换

    上扩展实现多主题选择,切换主题) 先看一下XML文档 id值不能重复,一套主题的name要相同,亮色type1,暗色type0 <?xml version="1.0" enc ...

  9. 第 5 章 Debian 系统中可用的软件

    目录 5.1. Debian GNU/Linux 收录了哪些类型的应用程序和开发工具? 5.2. 谁编写了所有这些软件? 5.3. 我应该如何获得一份 Debian 打包的程序的最新列表? 5.4. ...

  10. 9.4java考试订正

    import java.util.Scanner; public class viovo { static int number = 5;//五个商品信息 static oppo[] s = new ...