Rabbitmq advanced – expiration time TTL

Time:2021-6-24

summary

The expiration time TTL indicates that the expected time can be set for the message, which can be received by the consumer within this time; Automatically deleted after expiration; Rabbitmq can set TTL for messages and queues. At present, there are two ways to set it

The first method is through the setting of queue properties, all messages in the queue have the same expiration time
The second method is to set the message separately, and the TTL of each message can be different
The difference between the two methods is that the message set by the queue attribute is moved to the dead letter queue, while the message set by TTL is automatically disappeared.

If the above two methods are used at the same time, the message expiration time can be the smaller TTL between them. Once the message's lifetime in the queue exceeds the set TTL value, it is called dead message, which is delivered to the dead letter queue, and consumers will no longer be able to receive the message.

Set queue TTL

Configuration class configuration is as follows

@Configuration
public class TTLRabbitMqConfiguration {
    @Bean
    public DirectExchange ttlDirectExchange() {
        return new DirectExchange("ttl_direct_order_exchange",true,false);
    }

    @Bean
    public Queue directTtlQueue() {
        //Set expiration time
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",5000); //  This must be an int type
        return new Queue("ttl.direct.queue",true,false,false,args);
    }

    @Bean
    public Binding ttlBinding() {
        return BindingBuilder.bind(directTtlQueue()).to(ttlDirectExchange()).with("ttl");
    }
}


public void makeOrderTtl(String userId,String productId,int num) {
  //1: check whether the goods are sufficient according to the ID
  //2: save order
  String orderId = UUID.randomUUID().toString();
  System. Out. Println ("order generated successfully) + OrderID];
  //3: complete message distribution through MQ
  //Switch, route, key / queue, queue name, message content
  String exchangeName = "ttl_direct_order_exchange";
  String routingKey = "ttl";
  rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
}

Test code

@Test
void testOrderTtl() {
    orderService.makeOrderTtl("1","1",12);
}

Rabbitmq advanced - expiration time TTL

After five seconds, the data disappears automatically

Rabbitmq advanced - expiration time TTL

Rabbitmq advanced - expiration time TTL

Message set expiration time

Config configuration code

@Configuration
public class TTLRabbitMqConfiguration {
    @Bean
    public DirectExchange ttlDirectExchange() {
        return new DirectExchange("ttl_direct_order_exchange",true,false);
    }

    @Bean
    public Queue directTtlQueue() {
        //Set expiration time
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",5000); //  This must be an int type
        return new Queue("ttl.direct.queue",true,false,false,args);
    }

    @Bean
    public Queue directTtlMessageQueue() {
        return new Queue("ttl.message.direct.queue",true);
    }

    @Bean
    public Binding ttlBinding() {
        return BindingBuilder.bind(directTtlMessageQueue()).to(ttlDirectExchange()).with("ttlMessage");
    }

}

Test code

public void makeOrderTtlMessage(String userId,String productId,int num) {
        //1: check whether the goods are sufficient according to the ID
        //2: save order
        String orderId = UUID.randomUUID().toString();
        System. Out. Println ("order generated successfully) + OrderID];
        //3: complete message distribution through MQ
        //Switch, route, key / queue, queue name, message content
        String exchangeName = "ttl_direct_order_exchange";
        String routingKey = "ttlMessage";
        //Set expiration time for messages
        MessagePostProcessor messagePostProcessor = new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                //Set expiration time
                message.getMessageProperties().setExpiration("5000");
                message.getMessageProperties().setContentEncoding("UTF-8");
                return message;
            }
        };
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId,messagePostProcessor);
    }

@Test
void testOrderTtlMessage() {
  orderService.makeOrderTtlMessage("1","1",12);
}

After five seconds, the message automatically expired and disappeared

Rabbitmq advanced - expiration time TTL

Rabbitmq advanced - expiration time TTL

This work adoptsCC agreementReprint must indicate the author and the link of this article

Recommended Today

Five simple steps to master tensorflow tensor

Author | Orhan g. YAL ç ı nCompile VKSource: towards Data Science If you are reading this article, I believe we have similar interests and will engage in similar industries now / in the future. In this article, we will delve into the details of tensorflow tensor. We will cover all topics related to tensorflow’s […]