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文件代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<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;


/**
 * 生产者启动类
 */
@SpringBootApplication
@ImportResource("classpath:spring/rabbitmq.xml")
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;

@RestController
public class MsgController {


    @Autowired
    RabbitTemplate rabbitTemplate;

    @GetMapping("/sendTTLMsg")
    public String sendTTLMsg(@RequestParam String msg){
        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;

@RestController
public class MsgController {


    @Autowired
    RabbitTemplate rabbitTemplate;


    @GetMapping("/sendTTLMsg")
    public String sendTTLMsg(@RequestParam String msg){
        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。同时也可以看出在既有设置失效时间的队列上以及设置时间的消息上,以时间小的为最终失效时间。

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值