RabbitMQ-死信队列

RabbitMQ的死信交换机(DLX)是处理消息变为死信后的重定向机制,消息可能由于被拒绝、过期或队列满等原因成为死信。DLX是一个普通交换机,可以将死信路由到特定的死信队列。在配置中,通过设置x-dead-letter-exchange和x-dead-letter-routing-key参数,可以指定死信队列和路由键。示例代码展示了如何在Spring Boot应用中声明并配置死信队列和相关参数,例如设置消息存活时间和队列最大长度。

概述

DLX,全称为Dead-Letter-Exchange , 可以称之为死信交换机,也有人称之为死信邮箱。当消息在一个队列中变成死信(dead message)之后,它能被重新发送到另一个交换机中,这个交换机就是DLX ,绑定DLX的队列就称之为死信队列。
消息变成死信,可能是由于以下的原因:

  • 消息被拒绝
  • 消息过期
  • 队列达到最大长度

DLX也是一个正常的交换机,和一般的交换机没有区别,它能在任何的队列上被指定,实际上就是设置某一个队列的属性。当这个队列中存在死信时,Rabbitmq就会自动地将这个消息重新发布到设置的DLX上去,进而被路由到另一个队列,即死信队列。
要想使用死信队列,只需要在定义队列的时候设置队列参数 x-dead-letter-exchange 指定交换机即可。
在这里插入图片描述

案例

声明一个死信队列(和声明一个普通队列方式一样)

package com.chif.rabbitmq.springbootorderrabbitmqproducer.config;

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;

@Configuration
public class DeadRabbitMqConfiguration {

    //1.声明注册direct模式的dead交换机
    @Bean
    public DirectExchange DeadExchange() {
        return new DirectExchange("dead_direct_exchange", true, false);
    }

    //2.声明队列 sms.direct.queue,email.direct.queue,duanxin.direct.queue
    @Bean
    public Queue deadQueue() {
        return new Queue("dead.direct.queue", true);
    }


    //3.完成绑定关系
    @Bean
    public Binding deadBinding() {
        return BindingBuilder.bind(deadQueue()).to(DeadExchange()).with("dead");
    }



}

在之前设置过队列TTL的队列中绑定死信队列

package com.chif.rabbitmq.springbootorderrabbitmqproducer.config;

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;

@Configuration
public class TTLRabbitMqConfiguration {

    //1.声明注册fanout模式的交换机
    @Bean
    public DirectExchange ttlDirectExchange() {
        return new DirectExchange("ttl_order_exchange", true, false);
    }

    //2.声明队列 sms.direct.queue,email.direct.queue,duanxin.direct.queue
    @Bean
    public Queue ttlDirectQueue() {
        //设置ttl过期时间
        HashMap<String, Object> args = new HashMap();
        args.put("x-message-ttl",5000);

        //设置死信队列的交换机和队列routingKey
        args.put("x-dead-letter-exchange","dead_direct_exchange");
        args.put("x-dead-letter-routing-key","dead");
        //设置队列的最大长度
        args.put("x-max-length",5);

        return new Queue("ttl.direct.queue", true,false,false,args);
    }

    @Bean
    public Queue ttlDirectMessageQueue() {
        //从MessagePostProcessor设置ttl过期时间
        return new Queue("ttlMsg.direct.queue", true);
    }

    //3.完成绑定关系
    @Bean
    public Binding ttlDirectBinding() {
        return BindingBuilder.bind(ttlDirectQueue()).to(ttlDirectExchange()).with("ttl");
    }

    @Bean
    public Binding ttlDirectMsgBinding() {
        return BindingBuilder.bind(ttlDirectMessageQueue()).to(ttlDirectExchange()).with("TTLMsg");
    }

}

测试发送数据就会发现,消息过期之后就会到死信队列中去

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值