概述:
在rabbitmq中我們可以給消息設定過期時間LLT(Time to Live),在消息發送后過期時間段內未被消費,則系統會將其刪除,被刪除的消息將會進入死信隊列。關於設置消息的過期時間有兩種設置方式。1,可以設置在消息隊列上,則經過該消息隊列的消息都會使用該消息對列的過期時間;2,也可以將過期時間設置在消息體上,對消息進行單獨的消息過期時間設置。如果在經過一個設置了過期時間的隊列的並且自身也設置了過期時間的消息,則其過期時間取決於兩者時間較小的一個。
接下來我們本章將在上一章《RabbitMq(八) SpringBoot整合RabbitMQ 生產者代碼實現》的基礎上進行調整,對消息進行設置不同場景的過期時間。大致實現步驟如下:
- 創建rabbitmq的spring 配置資源文件
- 在配置文件中聲明消息隊列信息,包含隊列名稱,過期時間等
- 將配置資源文件在啟動類上進行導入
- 在發送消息時,指定2步驟中聲明的隊列名稱
一、配置文件信息
在SpringBoot工程的資源目錄下創建spring目錄,並創建rabbitmq.xml配置文件,引入rabbit命名空間。xml文件代碼如下:
-
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
-
http://www.springframework.org/schema/rabbit http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">
-
-
<!-- 定義過期隊列及其屬性,不存在則自動創建-->
-
<rabbit:queue id="my_ttl_queue" name="my_ttl_queue" auto-declare="true" >
-
<rabbit:queue-arguments>
-
<!-- 投遞該消息的隊列 ,中消息如果在6秒之內沒有被消費則進行刪除 -->
-
<entry key="x-message-ttl" value-type="long" value="6000" />
-
</rabbit:queue-arguments>
-
</rabbit:queue>
-
-
</beans>
二、在springBoot工程的啟動類上導入一種的配置資源文件:
通過在啟動類上使用@ImportResource 注解導入配置信息,代碼如下:
-
package com.xiaohui.rabbitmq;
-
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.context.annotation.ImportResource;
-
-
-
/**
-
* 生產者啟動類
-
*/
-
-
-
public class ProducerApplication {
-
-
public static void main(String[] args) {
-
SpringApplication.run(ProducerApplication.class,args);
-
}
-
}
三、編寫調用請求方法測試消息隊列過期時間
我們在之前篇章的web層Controller中添加一個方法sendTTLMsg。並在內部固定消息隊列為我們資源文件中配置的消息隊列“my_ttl_queue”。Controller代碼如下:
-
package com.xiaohui.rabbitmq.controller;
-
-
import com.xiaohui.rabbitmq.config.RabbitMqConfig;
-
import org.springframework.amqp.core.Message;
-
import org.springframework.amqp.core.MessageProperties;
-
import org.springframework.amqp.rabbit.core.RabbitTemplate;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.GetMapping;
-
import org.springframework.web.bind.annotation.RequestParam;
-
import org.springframework.web.bind.annotation.RestController;
-
-
-
public class MsgController {
-
-
-
-
RabbitTemplate rabbitTemplate;
-
-
-
public String sendTTLMsg(
-
MessageProperties properties = new MessageProperties();
-
properties.setExpiration( "3000");
-
Message msgobj = new Message(msg.getBytes(),properties);
-
rabbitTemplate.convertAndSend( "my_ttl_queue",msgobj);
-
return "消息發送成功!";
-
}
-
}
我們訪問url進行條用Controller進行調用發送消息代碼,並可以通過rabbitmq控制台查看隊列信息情況。
消息發送之后rabbit的控制台我們可以看到會新增一個消息隊列,並且我們可以看到隊列中消息從0變成1后,在過期時間之后又從1變為0。
四、編寫調用請求方法測試消息過期時間
我們在web層的Controller中編寫請求方法並通過代碼實現單條消息的創建以及通過配置設置消息過期時間。controller代碼如下:
-
package com.xiaohui.rabbitmq.controller;
-
-
import com.xiaohui.rabbitmq.config.RabbitMqConfig;
-
import org.springframework.amqp.core.Message;
-
import org.springframework.amqp.core.MessageProperties;
-
import org.springframework.amqp.rabbit.core.RabbitTemplate;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.web.bind.annotation.GetMapping;
-
import org.springframework.web.bind.annotation.RequestParam;
-
import org.springframework.web.bind.annotation.RestController;
-
-
-
public class MsgController {
-
-
-
-
RabbitTemplate rabbitTemplate;
-
-
-
-
public String sendTTLMsg(
-
MessageProperties properties = new MessageProperties();
-
properties.setExpiration( "3000");
-
Message msgobj = new Message(msg.getBytes(),properties);
-
rabbitTemplate.convertAndSend( "my_ttl_queue",msgobj);
-
return "消息發送成功!";
-
}
-
}
我們訪問url進行條用Controller進行調用發送消息代碼,並可以通過rabbitmq控制台查看隊列信息情況。同樣我們可以在rabbit控制台中查看消息處理情況,我們可以通過隔秒刷新看到此時消息會在3秒時間內從0變1后又從1變0。同時也可以看出在既有設置失效時間的隊列上以及設置時間的消息上,以時間小的為最終失效時間。