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