RabbitMQ 死信 延迟队列

目录

一、Dead Letter Exchange (死信交换机)

二、代码演示

1、provider

①、DeadConfig

②、ProviderController

 2、consumer

①、DeadReceiver 


一、Dead Letter Exchange (死信交换机)

RabbitMQ 作为一个高级消息中间件,提出了死信交换器的概念.
这种交换器专门处理死了的信息(被拒绝可以重新投递的信息不能算死的)。

消息变成死信一般是以下三种情况:

  • 消息被拒绝,并且设置 requeue 参数为 false 
  • 消息过期(默认情况下 Rabbit 中的消息不过期,但是可以设置队列的过期时间和消息的过期时间以达到消息过期的效果)
  • 队列达到最大长度(一般当设置了最大队列长度或大小并达到最大值时)

当满足上面三种情况时,消息会变成死信消息,并通过死死信交换机投递到相应的队列中。

我们只需要监听相应队列,就可以对死信消息进行最后的处理。

二、代码演示

1、provider

①、DeadConfig

package com.ysq.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+":该订单已经过期");
    }
 
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值