spring.rabbitmq.publisher-returns=true
和 spring.rabbitmq.template.mandatory = true
有什么区别?
当这两个配置之一设置为
true
时,当消息发送到 Exchange 但无法路由到任何队列时,它将返回到生产者。为什么有两种配置具有相同的功能,或者它们之间有什么我不知道的差异?
里面的逻辑是这样的:
doSend(channel, exchange, routingKey, message,
(RabbitTemplate.this.returnsCallback != null
|| (correlationData != null && StringUtils.hasText(correlationData.getId())))
&& isMandatoryFor(message),
correlationData);
其中
returnsCallback
是所有内容的全局选项,correlationData
是每条消息。
为了方便起见,实际上
mandatory
也必须设置为 true
,但仍然可以通过 RabbitTemplate.setMandatoryExpression()
按消息进行配置。因此,即使启用了 returns
,您仍然可以按消息控制它。不幸的是,无法通过 Spring Boot 配置属性来设置 SpEL 表达式。