RabbitMQ-TTL队列/消息

RabbitMQ之TTL队列/消息

  • TTL是Time To Live的缩写,也就是生存时间.

  • RabbitMQ支持消息的过期时间,在消息发送时可以进行制定.

  • RabbitMQ支持队列的过期时间,从消息入队列开始计算,只要超过了队列的超时时间配置,那么消息会自动的清
    除.

TTL队列

在设置队列的超时时间有两种方式,一是:通过Java代码声明队列时设置其超时时间设置;二是:通过RabbitMQ
管控台的可视化操作下声明队列并进行设置其超时时间参数配置.

TTL队列声明方式一:

声明队列并设置其TTL


import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;

import java.util.HashMap;
import java.util.Map;

/**
 * @Classname Consumer
 * @Description TTL队列/消息:消费者
 * @Date 2019/9/10 14:36
 * @Created by Jiavg
 */
public class Consumer {

    public static void main(String[] args) throws Exception{

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        String exchangeName = "test_TTL_Exchange";
        String routingKey = "TTL.#";
        String queueName = "test_TTL_Queue";

        // 创建声明队列所用到的参数集合,并设置其超时时间x-message-ttl为10000(10秒)
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-message-ttl", 10000);

        // 声明交换机、声明队列、绑定队列到交换机
        channel.exchangeDeclare(exchangeName, "topic", true, false, null);
        channel.queueDeclare(queueName, true, false, false, arguments);
        channel.queueBind(queueName, exchangeName, routingKey);

        QueueingConsumer queueingConsumer = new QueueingConsumer(channel);
        channel.basicConsume(queueName, true, queueingConsumer);

        while (true) {
            QueueingConsumer.Delivery delivery = queueingConsumer.nextDelivery();
            byte[] body = delivery.getBody();

            System.out.printf("消费端:" + new String(body));
        }        
    }   
}

管控台变化-超时队列信息

超时队列相关信息

验证超时队列

在管控台发送一条消息

信息处理过程


由上图可知,超时队列test_TTL_Queue中的消息在经过指定时间后会自动清除

TTL队列声明方式二:

声明队列并设置其TTL

TTL消息

在设置消息的超时时间有两种方式,一是:通过Java代码发送消息时设置其超时时间设置;二是:通过RabbitMQ
管控台的可视化操作下发送消息并进行设置其超时时间参数配置(了解).

TTL消息声明方式一:

声明消息并设置其TTL


import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Classname Producer
 * @Description TTL队列/消息:生产者
 * @Date 2019/9/10 14:38
 * @Created by Jiavg
 */
public class Producer {

    public static void main(String[] args) throws Exception{

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/");
        connectionFactory.setUsername("guest");
        connectionFactory.setPassword("guest");

        Connection connection = connectionFactory.newConnection();

        Channel channel = connection.createChannel();

        String exchangeName = "test_Topic_Exchange";
        String routingKey = "test.ttl";
        String msg = "Hello World send TTL Message";

        AMQP.BasicProperties properties = new AMQP.BasicProperties.Builder()
                .deliveryMode(2)
                .contentEncoding("UTF-8")
                // 设置消息的超时时间
                .expiration("10000")
                .build();

        channel.basicPublish(exchangeName, routingKey, properties, msg.getBytes());   
    } 
}

验证超时消息

信息处理过程

由上图可知,设置了超时时间的消息在经过指定时间后会被自动清除.

TTL消息声明方式二:

声明消息并设置其TTL


   转载规则


《RabbitMQ-TTL队列/消息》 Jiavg 采用 知识共享署名 4.0 国际许可协议 进行许可。
  目录