RabbitMq消息过期时间TTL介绍


概述:

        在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。同时也可以看出在既有设置失效时间的队列上以及设置时间的消息上,以时间小的为最终失效时间。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM