Road rabbitmq learning (eight)

Today it is about rabbitmq in the dead letter queue

 

First, start with what is a dead letter queue

 

Three situations in which messages become dead letter

 

1. The message is rejected (basic.reject or basic.nack) and requeue = false.

2. Message TTL expired

It can be achieved in two ways: there are two ways Per-Message TTL and Queue TTL, a first expiration time can be used in most scenarios for each message is provided, the second set the expiration time for the Queue for disposable delay tasks scene

If the above two methods simultaneously, the expiration time of the message that is subject to a smaller value between TTL.

Queue attributes: x-message-ttl 

Message Properties: expiration

3. The maximum length of the queue (queue is full, not to add the data in mq)

 

We can give a queue configuration two properties

x-dead-letter-exchange: sending to the switch after the dead letter

x-dead-letter-routing-key: to set the dead letter routingKey

    @Bean
    public Queue mailQueue(){
        Map<String,Object> args = new HashMap<>();
        args.put(DEAD_LETTER_QUEUE_KEY,deadExchangeName);
        args.put(DEAD_LETTER_ROUTING_KEY,deadRoutingKey);
        Queue queue = new Queue(FANOUT_EMAIL_QUEUE,true,false,false, args);
        return queue;
    }

 

After the above configuration, once the queue becomes a dead letter message will retransmit the message to a dead letter switch, the switch then sent to the queue, the message may be used to check the reason for the failure, and the like

 

    @RabbitHandler
    @RabbitListener(queues = "fanout_email_queue")
    public void process(@Payload User user, Channel channel, @Headers Map<String,Object> map){



       /* rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        User user1 = rabbitTemplate.receiveAndConvert(new ParameterizedTypeReference<User>() {
        });*/
        System.out.println(user.getName());
        if (map.get("error")!= null){
            System.out.println("错误的消息");
            try {
                channel.basicNack ((Long) as map.get (AmqpHeaders.DELIVERY_TAG),to false , to false );       // denied message requeue = false does not re-queue the 
                return ; 
            } the catch (IOException E) { 
                e.printStackTrace (); 
            } 
        } 
        the try { 
            channel.basicAck ((Long) as map.get (AmqpHeaders.DELIVERY_TAG) , to false );             // acknowledgment message 
        } the catch (IOException E) { 
            e.printStackTrace (); 
        } 
    } 

    @RabbitListener (Queues = "dead_queue" )
     public  void processTwo(@Payload User user, Channel channel, @Headers Map<String,Object> map){
        System.out.println(user.getName()+":"+user.getAge());
    }

Tested successfully entered into the dead letter queue!

 

Guess you like

Origin www.cnblogs.com/changeCode/p/11333014.html