Expiration time TTL and dead letter queue

Expiration time TTL and dead letter queue

1. Expiration time TTL

The expiration time TTL indicates that the expected time can be set for the message, within which time it can be received by consumers; the message will be automatically deleted after this time.
RabbitMQ can set TTL for messages and queues. There are currently two ways to set it.

  • The first method is to set the queue properties, all messages in the queue have the same expiration time.
  • The second method is to set the message individually, and the TTL of each message can be different.

If the above two methods are used at the same time, the expiration time of the message is based on the smaller value of the TTL between the two. Once the lifetime of a message in the queue exceeds the set
TTL value, it is called a dead message and is delivered to the dead letter queue, and consumers will no longer receive the message.

Set up

 @Bean
    public Queue queue5(){
    
    
        Map<String, Object> args = new HashMap<>();

        args.put("x-message-ttl",5000);
        return  new Queue("queue5",true,false,false,args);

    }
@Test
	void contextLoads2() throws Exception {
    
    
		MessagePostProcessor messagePostProcessor=new MessagePostProcessor() {
    
    
			@Override
			public Message postProcessMessage(Message message) throws AmqpException {
    
    

				message.getMessageProperties().setExpiration("5000");
				message.getMessageProperties().setContentEncoding("UTF-8");

				return message;
			}
		};

		rabbitTemplate.convertAndSend("dead","","hello springboot-dead",messagePostProcessor);
	}

Second, the dead letter queue

DLX, the full name is Dead-Letter-Exchange, can be called a dead letter exchange, and some people call it a dead letter mailbox. When a message becomes a
dead message (dead message) in a queue , it can be re-sent to another exchange. This exchange is DLX, and the queue bound to DLX is called a dead letter queue.
The message becomes a dead letter, which may be due to the following reasons:

  • Message rejected
  • Message expired
  • The queue reaches the maximum length

DLX is also a normal switch, which is no different from a general switch. It can be specified on any queue, in fact it is to set the attributes of a certain queue
. When there is a dead letter in this queue, Rabbitmq will automatically republish the message to the set DLX, and then be routed to another
queue, the dead letter queue.
To use the dead letter queue, the queue only need to set the time parameter defines the queue x-dead- letter-exchangespecified switch to.

Insert picture description here

   @Bean
    public Queue queue7(){
    
    
        Map<String, Object> args = new HashMap<>();

        args.put("x-message-ttl",5000);
        args.put("x-dead-letter-exchange","dead");
        return  new Queue("queue7",true,false,false,args);
    }
@Test
	void contextLoads() throws Exception {
    
    
		MessagePostProcessor messagePostProcessor=new MessagePostProcessor() {
    
    
			@Override
			public Message postProcessMessage(Message message) throws AmqpException {
    
    

				message.getMessageProperties().setExpiration("5000");
				message.getMessageProperties().setContentEncoding("UTF-8");

				return message;
			}
		};


		rabbitTemplate.convertAndSend("latte3.fanout","","hello springboot-fanout",messagePostProcessor);


	}

Insert picture description here
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_43803285/article/details/115035652