rabbitmq dead letter, delay queue

Dead letter queue (delay queue)

Dead letter, the corresponding word in the official website is "Dead Letter", which is a message mechanism of RabbitMQ

Generally speaking, the producer delivers the message to the broker or directly to the queue, and the consumer takes out the message from the queue for consumption. If it has been unable to consume a certain piece of data, it can put the message into the dead letter queue. Wait for the condition to be met before taking it out of the dead letter queue for consumption again, thereby avoiding message loss.

Dead letter source:

Message TTL expired

The queue is full and data cannot be added again

The message was rejected (reject or nack), and requeue = false

dead letter exchange (producer

DeadConfig.java

package com.hmf.producer.mq; 

import org.springframework.amqp.core.Binding; 
import org.springframework.amqp.core.BindingBuilder; 
import org.springframework.amqp.core.DirectExchange; 
import org.springframework.amqp.core .Queue; 
import org.springframework.context.annotation.Bean; 
import org.springframework.context.annotation.Configuration; 

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

@Configuration 
@SuppressWarnings("all") 
public class DeadConfig { 
    //1. Need a normal switch 
    //2. Normal queue to send messages (with configuration) 
    //3. Have a dead letter switch, queue 

    @Bean 
    public Queue normalQueue(){  
        Map<String,Object> config=new HashMap <>();
        //Expiration time 
        config.put("x -message-ttl", 10000);
        //死信交换机
        config.put("x-dead-letter-exchange", "deadExchange");
        //死信routing key
        config.put("x-dead-letter-routing-key", "DD");
        return new Queue("normalQueue",true,false,false,config);
    }

    @Bean
    public Queue deadQueue(){
        return new Queue("deadQueue",true);
    }

    @Bean
    public DirectExchange normalExchange() {
        return new DirectExchange("normalExchange");
    }

    @Bean
    public DirectExchange deadExchange() {
        return new DirectExchange("deadExchange");
    }

    @Bean
    public Binding normalBinding() {
        return BindingBuilder.bind(normalQueue()).to(normalExchange()).with("CC");
    }

    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(deadQueue()).to(deadExchange()).with("DD");
    }
}

 

ProducerController.java

package com.hmf.producer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SuppressWarnings("all")
@Slf4j
public class ProducerController {

    @Autowired
    private RabbitTemplate template;

    @RequestMapping("/directSend")
    public String directSend(String routingKey){
        template.convertAndSend("directExchange",routingKey,"Hello");
        return "okok";
    }

    @RequestMapping("/topicSend") 
    public String topicSend(String routingKey){ 
        template.convertAndSend("topicExchange",routingKey,"Hello"); 
        return "okok"; 
    } 

    @RequestMapping("/fanoutSend") 
    public String fanoutSend() { 
        //The key is written as null, otherwise it will directly enter the second parameter, otherwise an error will be reported 
        template.convertAndSend("fanoutExchange",null,"Hello"); 
        return "okok"; 
    } @RequestMapping("/deadSend") 
    public String deadSend(){ 
        log.warn("The order has been saved"); 
        //an order has been saved 
        template.convertAndSend("normalExchange","CC","order-1902");
        return "yes";
    }
}

   

Access successful

 

dead letter exchange (consumer

DeadReceiver

package com.hmf.cunsumer.mq;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@SuppressWarnings("all")
@RabbitListener(queues = "deadQueue")
@Slf4j
public class DeadReceiver {

    @RabbitHandler
    public void process(String message){
        log.warn(message+":该订单已经过期");
    }

}

Guess you like

Origin blog.csdn.net/m0_60375943/article/details/123154755