在SpringBoot的RabbitMQ配置中残腌,有如下一項:
spring.rabbitmq.template.mandatory = true
官方注釋:
Enable mandatory messages. If a mandatory message cannot be routed to a queue by the server, it will return an unroutable message with a Return method.
大意為:開啟強制消息投遞(mandatory
為設(shè)置為true
)枯夜,但消息未被路由至任何一個queue
,則回退一條消息到RabbitTemplate.ReturnCallback
中的returnedMessage
方法:
public interface ReturnCallback {
/**
* Returned message callback.
* @param message the returned message.
* @param replyCode the reply code.
* @param replyText the reply text.
* @param exchange the exchange.
* @param routingKey the routing key.
*/
void returnedMessage(Message message, int replyCode, String replyText,
String exchange, String routingKey);
}
瀏覽RabbitAutoConfiguration
發(fā)現(xiàn)如下代碼:
private boolean determineMandatoryFlag() {
Boolean mandatory = this.properties.getTemplate().getMandatory();
return (mandatory != null ? mandatory : this.properties.isPublisherReturns());
}
如果設(shè)置了mandatory
參數(shù),則直接取值疏尿;如若mandatory
參數(shù)為空榆鼠,則取之于否起開了消息回退