RabbitMQ学习笔记:mandatory、publisher-confirms、publisher-return属性区别

作者简介:大家好,我是smart哥,前中兴通讯、美团架构师,现某互联网公司CTO

联系qq:184480602,加我进群,大家一起学习,一起进步,一起对抗互联网寒冬

学习必须往深处挖,挖的越深,基础越扎实!

阶段1、深入多线程

阶段2、深入多线程设计模式

阶段3、深入juc源码解析


阶段4、深入jdk其余源码解析


阶段5、深入jvm源码解析

码哥源码部分

码哥讲源码-原理源码篇【2024年最新大厂关于线程池使用的场景题】

码哥讲源码【炸雷啦!炸雷啦!黄光头他终于跑路啦!】

码哥讲源码-【jvm课程前置知识及c/c++调试环境搭建】

​​​​​​码哥讲源码-原理源码篇【揭秘join方法的唤醒本质上决定于jvm的底层析构函数】

码哥源码-原理源码篇【Doug Lea为什么要将成员变量赋值给局部变量后再操作?】

码哥讲源码【你水不是你的错,但是你胡说八道就是你不对了!】

码哥讲源码【谁再说Spring不支持多线程事务,你给我抽他!】

终结B站没人能讲清楚红黑树的历史,不服等你来踢馆!

打脸系列【020-3小时讲解MESI协议和volatile之间的关系,那些将x86下的验证结果当作最终结果的水货们请闭嘴】

rabbitmq客户端发送消息首先发送的交换器exchange,然后通过路由键routingKey和bindingKey比较判定需要将消息发送到那个队列queue上;在这个过程有两个地方消息可能丢失,第一消息发送到交换器exchange的过程,第二消息从交换器exchange发送到队列queue的过程;

1.publiser-confirm模式可以确保生产者到交换器exchange消息有没有发送成功
    #设置此属性配置可以确保消息成功发送到交换器
    spring.rabbitmq.publisher-confirms=true
2.publisher-return模式可以在消息没有被路由到指定的queue时将消息返回,而不是丢弃
    #可以确保消息在未被队列接收时返回
    spring.rabbitmq.publisher-returns=true

在使用上面的属性配置时通常会和mandatory属性配合一起使用:

    #指定消息在没有被队列接收时是否强行退回还是直接丢弃
    spring.rabbitmq.template.mandatory=true

到这里你可能会有一个疑问,这两个配置都是指定未找到合适队列时将消息退回,究竟是如何分别起作用呢?接下来我们看下RabbitAutoConfiguration自动化配置类就清楚了:

            @Bean
            @ConditionalOnSingleCandidate(ConnectionFactory.class)
            @ConditionalOnMissingBean
            public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
                PropertyMapper map = PropertyMapper.get();
                RabbitTemplate template = new RabbitTemplate(connectionFactory);
                MessageConverter messageConverter = (MessageConverter)this.messageConverter.getIfUnique();
                if (messageConverter != null) {
                    template.setMessageConverter(messageConverter);
                }
    						//设置rabbitmq处理未被queue接收消息的模式
                template.setMandatory(this.determineMandatoryFlag());
                Template properties = this.properties.getTemplate();
                if (properties.getRetry().isEnabled()) {
                    template.setRetryTemplate((new RetryTemplateFactory((List)this.retryTemplateCustomizers.orderedStream().collect(Collectors.toList()))).createRetryTemplate(properties.getRetry(), Target.SENDER));
                }
    
                properties.getClass();
                map.from(properties::getReceiveTimeout).whenNonNull().as(Duration::toMillis).to(template::setReceiveTimeout);
                properties.getClass();
                map.from(properties::getReplyTimeout).whenNonNull().as(Duration::toMillis).to(template::setReplyTimeout);
                properties.getClass();
                map.from(properties::getExchange).to(template::setExchange);
                properties.getClass();
                map.from(properties::getRoutingKey).to(template::setRoutingKey);
                properties.getClass();
                map.from(properties::getDefaultReceiveQueue).whenNonNull().to(template::setDefaultReceiveQueue);
                return template;
            }
    				//判定是否将未找到合适queue的消息退回
            private boolean determineMandatoryFlag() {
              	/**
                  * 获取spring.rabbitmq.template.mandatory属性配置;
                  * 这里面会有三种可能,为null、false、true
                  * 而只有在mandatory为null时才会读取publisher-return属性值
                  **/
                Boolean mandatory = this.properties.getTemplate().getMandatory();
                return mandatory != null ? mandatory : this.properties.isPublisherReturns();
            }

阅读上面的源码可以获取如下信息:

  1. spring.rabbitmq.template.mandatory属性的优先级高于spring.rabbitmq.publisher-returns的优先级
  2. spring.rabbitmq.template.mandatory属性可能会返回三种值null、false、true,
  3. spring.rabbitmq.template.mandatory结果为true、false时会忽略掉spring.rabbitmq.publisher-returns属性的值
  4. spring.rabbitmq.template.mandatory结果为null(即不配置)时结果由spring.rabbitmq.publisher-returns确定
  • 7
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值