Rabbitmq advanced – dead letter queue

Time:2021-6-23

summary

Dlx, the full name of dead letter exchange, can be called dead letter switch or dead letter mailbox. When a message becomes dead message in a queue, it can be re sent to another switch, which is called Dlx, and the queue bound to Dlx is called dead message queue. The reason why a message becomes a dead letter may be as follows

Message rejected
Message expired
The queue has reached the maximum length

Dlx is also a normal switch. It is no different from ordinary switches. It can be specified on any queue. In fact, it is to set the properties of a queue. When there is a dead letter in the queue, rabbitmq will automatically re publish the message to the set Dlx, and then it will be routed to another queue, that is, the dead letter queue.
To use the dead letter queue, you only need to set the queue parameter 'x-dead-letter-exchange' and specify the switch when defining the queue.

You can see the parameter configuration on the page

Rabbitmq advanced - dead letter queue

Dead letter queue configuration

This is a normal direct message queue

@Configuration
public class DeadRabbitMqConfiguration {
    @Bean
    public DirectExchange deadDirectExchange() {
        return new DirectExchange("dead_direct_exchange",true,false);
    }

    @Bean
    public Queue directDeadQueue() {
        return new Queue("dead.direct.queue",true);
    }

    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(directDeadQueue()).to(deadDirectExchange()).with("dead");
    }

}

Expired message queue configuration

Compared with the previous message queue, the difference is that not only the expiration time is specified, but also the dead letter queue switch and dead letter queue name are specified.

@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 directTtlQueue() {
        //Set expiration time
        HashMap<String, Object> args = new HashMap<>();
        args.put("x-message-ttl",5000); //  This must be an int type
        args.put("x-dead-letter-exchange","dead_direct_exchange");
        args.put("x-dead-letter-routing-key","dead"); //  In fanout mode, there is no routing key
        return new Queue("ttl2.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(directTtlQueue()).to(ttlDirectExchange()).with("ttl2");
    }


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

}

test

/**
     *Queue set expiration time
     * @param userId
     * @param productId
     * @param num
     */
    public void makeOrderTtl2(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 = "ttl2";
        rabbitTemplate.convertAndSend(exchangeName,routingKey,orderId);
    }

@Test
void testOrderTtl2() {
  orderService.makeOrderTtl2("1","1",12);
}

Rabbitmq advanced - dead letter queue

Rabbitmq advanced - dead letter queue

You can see that the queue has two more parametersDLXandDLK

Expired messages are delivered tottl2.direct.queueDead letter queue

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

Recommended Today

What is “hybrid cloud”?

In this paper, we define the concept of “hybrid cloud”, explain four different cloud deployment models of hybrid cloud, and deeply analyze the industrial trend of hybrid cloud through a series of data and charts. 01 introduction Hybrid cloud is a computing environment that integrates multiple platforms and data centers. Generally speaking, hybrid cloud is […]