
1、1 在使用 RabbitMQ 的时候,作为消息发送方希望杜绝任何消息丢失或者投递失败场景。 RabbitMQ 为我们提供了两种方式用来控制消息的投递可靠性模式。 rabbitmq 整个消息投递的路径为: producer--->rabbitmq broker--->exchange--->queue--->consumer 1、2 confirm 确认模式 消息从 producer 到 exchange 则会返回一个 confirmCallback 。 1、3 return 退回模式 消息从 exchange-->queue 投递失败则会返回一个 returnCallback 。
2、1 确认模式:
2、2 配置文件开启确认模式:
# 开启消息的/confirm/i机制
publisher-/confirm/is: true
2、3 使用rabbitTemplate.set/confirm/iCallback设置回调函数。
@Test
public void test/confirm/i(){
rabbitTemplate.set/confirm/iCallback((correlationData, b, s) -> {
System.out.println("confirm 方法被指行了.........");
if(b){
System.out.println("接受消息成功" + s);
}else{
System.out.println("接受失败" + s);
}
});
// 正常情况
rabbitTemplate.convertAndSend("springboot_topic_exchange","t1.topic.test","hello springboot topic");
// 模拟异常
rabbitTemplate.convertAndSend("springboot_topic_exchange1","t1.topic.test","hello springboot topic");
}
2、4
运行测试。
3、1 退回模式:
3、2 配置文件开启此模式:
# 开启 退回模式
publisher-returns: true
3、3 具体代码如下:
@Test
public void testReturn(){
// 1、设置交换机处理失败消息的模式
rabbitTemplate.setMandatory(true);
// 2、设置ReturnCallBack
rabbitTemplate.setReturnCallback((message,replyCode,replyText,exchange,routingKey) -> {
System.out.println("return 执行了....");
System.out.println(message);
System.out.println(replyCode);
System.out.println(replyText);
System.out.println(exchange);
System.out.println(routingKey);
});
// 正常情况
rabbitTemplate.convertAndSend(RmConfig.EXCHANGE_NAME,"t1.topic.test","hello springboot topic");
// 失败情况
// rabbitTemplate.convertAndSend(RmConfig.EXCHANGE_NAME,"t2.topic.test","hello springboot topic");
}
项目代码链接:https://github.com/Mbm7280/rabbitmq_demo
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)