交换机到队列
通过returnCallback
消息从交换器发送到对应队列失败时触发
两种模式
交换机到队列不成功,则丢弃消息(默认)
交换机到队列不成功,返回给消息生产者,触发returnCallback
首先配置
#开启消息二次确认,交换机到队列的可靠性投递 publisher-returns: true #为true,则交换机处理消息到路由失败,则会返回给生产者 template: mandatory: true
测试方法
/** * 交换机到队列的确定消息 */ @Test public void test6() { //为true,则交换机处理消息到路由失败,则会返回给生产者 //开启强制消息投递(mandatory为设置为true),但消息未被路由至任何一个queue,则回退一条消息 template.setReturnCallback(new RabbitTemplate.ReturnCallback() { @Override public void returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { int code = replyCode; System.out.println("code="+code); System.out.println("message="+message); System.out.println("replyText="+replyText); System.out.println("exchange="+exchange); System.out.println("exchange="+routingKey); } }); template.convertAndSend(RabbitMQConfig.EXCHANGE_NAME,"order.new","新订单来啦11"); }
本文作者为DBC,转载请注明。