RabbitMQ. Dead letter, delay queue

Table of contents

1. Dead Letter Exchange (Dead Letter Exchange)

Switch properties:

2. Code demonstration

1.provider

①.DeadConfig

②.ProviderController

 2.consumer

①.DeadReceiver 


1. Dead Letter Exchange (Dead Letter Exchange)

 As an advanced message middleware, RabbitMQ proposes the concept of dead letter exchange.
This kind of exchange handles dead messages exclusively (messages that are rejected for redelivery are not considered dead). There are generally three situations when a message becomes a dead letter:
1. The message is rejected and the requeue parameter is set to false 
2. The message expires (messages in Rabbit do not expire by default, but the expiration time of the queue and the expiration of the message can be set time to achieve the effect of message expiration)

3. The queue reaches the maximum length (generally when the maximum queue length or size is set and reaches the maximum value) when the
above three conditions are met, the message will become a dead letter message and be delivered to the corresponding queue through the dead letter switch. .
We only need to listen to the corresponding queue to perform final processing on the dead letter message.


Order timeout processing


 


The producer produces an order message that times out after 1 minute and sends it to the normal exchange exchange - a. The message matches the queue queue - a but is not consumed after one minute. The message will be delivered to the dead letter exchange dlx-exchange and sent to the dead letter queue dlx-queue in the private message queue.
After receiving the message, the consumer of the dead letter queue dlx-queue will query the status of the order based on the message. If it is still unpaid, the order status will be updated. timeout status

Switch properties:

Name: Switch name

Type: Switch type, direct, topic, fanout, headers

Durabi lity: Whether persistence is required. If persistence is required, the switch will still exist after RabbitMQ restarts. Auto Delete: When the last one is bound to Exchange. After the queue is deleted, the exchange will be automatically deleted. ExchangeInternal: Whether the current Exchange is used internally by RabbitMQ, the default is False;

Arguments: Extended parameters, used to extend the customized use of the AMQP protocol
 

2. Code demonstration

1.provider

①.DeadConfig

package com.example.provider.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.需要正常的交换机
    //2.正常队列发出消息(具备配置)
    //3.具备死信交换机,队列

    @Bean
    public Queue normalQueue(){
        Map<String,Object> config=new HashMap<>();
        //过期时间
        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");
    }

}

②.ProviderController

 @RequestMapping("/deadSend")
    public String deadSend(){
        log.warn("订单已经保存");
        //保存了一个订单
        template.convertAndSend("normalExchange","CC","order11888");
        return "yes";
    }

 2.consumer

①.DeadReceiver 

package com.example.consumer.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_54546762/article/details/123154426
Recommended