• MQ回退消息 springboot


    Mandatory参数

      在仅开启了生产者确认机制的情况下,交换机接收到消息后,会直接给消息生产者发送确认消息,如果发现该消息不可路由,那么消息会被直接丢弃,此时生产者是不知道消息被丢弃这个事件的。那么如何让无法被路由的消息帮我想办法处理一下?最起码通知我一声,我好自己处理啊。通过设置mandatory参数可以在当消息传递过程中不可达目的地时将消息返回给生产者。  

     消息生产者:

    1. @Slf4j
    2. @RestController
    3. public class MessageProduce implements RabbitTemplate.ConfirmCallback ,RabbitTemplate.ReturnCallback{
    4. @Autowired
    5. private RabbitTemplate rabbitTemplate;
    6. @PostConstruct
    7. private void init()
    8. {
    9. rabbitTemplate.setConfirmCallback(this);
    10. /**
    11. * true:
    12. * 交换机无法将消息进行路由的时候,会将该消息返回给生产者
    13. * false:
    14. * 如果发现消息无法进行路由,则直接将消息扔掉
    15. */
    16. rabbitTemplate.setMandatory(true);
    17. //将回退消息交给谁处理
    18. rabbitTemplate.setReturnCallback(this);
    19. }
    20. @GetMapping("sendMessage/{message}")
    21. public void sendMessage(@PathVariable String message)
    22. {
    23. //让消息绑定一个id值
    24. CorrelationData correlationData1 = new CorrelationData(UUID.randomUUID().toString());
    25. rabbitTemplate.convertAndSend("confirm.exchange","key1",message+"key1",correlationData1);
    26. rabbitTemplate.convertAndSend("confirm.exchange","key2",message+"key2",correlationData1);
    27. log.info("发送消息id位{}内容为{}",correlationData1.getId(),message+"key1");
    28. log.info("发送消息id位{}内容为{}",correlationData1.getId(),message+"key2");
    29. }
    30. @Override
    31. public void confirm(CorrelationData correlationData, boolean b, String s) {
    32. String id= correlationData!=null?correlationData.getId():"";
    33. if(b)
    34. {
    35. log.info("交换机收到消息确认成功;id{}",id);
    36. }
    37. else {
    38. log.error("消息id{}未成功投递到交换机,原因是:{}",id,s);
    39. }
    40. }
    41. @Override
    42. public void returnedMessage(Message message, int replyCode, String replyText,
    43. String exchange, String routingKey) {
    44. log.info("消息:{}被服务器退回,退回的原因是{},交换机是{}",
    45. new String(message.getBody()),replyText,exchange,routingKey);
    46. }
    47. }

    回调接口

    1. @Component
    2. @Slf4j
    3. public class MyCallBack implements RabbitTemplate.ConfirmCallback,RabbitTemplate.ReturnCallback {
    4. @Override
    5. public void confirm(CorrelationData correlationData, boolean ack ,String cause) {
    6. String id=correlationData!=null?correlationData.getId():"";
    7. if(ack)
    8. {
    9. log.info("交换机已经收到id为{}的消息",id);
    10. }
    11. else
    12. {
    13. log.info("交换机还未收到id未:{}的消息,原因是{}",cause);
    14. }
    15. }
    16. @Override
    17. public void returnedMessage(Message message, int replyCode, String replyText,
    18. String exchange, String routingKey) {
    19. log.error("消息{},被交换机{}退回。退回的原因是:{},路由key为{}",new String(message.getBody()),
    20. exchange,replyText,routingKey);
    21. }
    22. }

    消费者

    1. @Component
    2. @Slf4j
    3. public class ConfirmConsumer {
    4. public static final String CONFIRM_QUEUE_NAME="confirm.queue";
    5. @RabbitListener(queues = CONFIRM_QUEUE_NAME)
    6. public void receiveMsg(Message message)
    7. {
    8. String s = new String(message.getBody());
    9. log.info("接收到队列confirm.queue消息:{}",s);
    10. }
    11. }

     http://localhost:8989/sendMessage/8888

    结果:

     

     

  • 相关阅读:
    2-1-1.spring源码--BeanFactory
    【设计模式深度剖析】【6】【结构型】【外观模式】| 以电脑开关按钮为例,并结合微服务架构的API网关加深理解
    关于AI(深度学习)相关项目 K8s 部署的一些思考
    C高级day4循环语句
    MySQL 日期函数大全(更新中.....)
    Java集合基础知识点系统性总结篇
    力扣 -- 416. 分割等和子集(01背包问题)
    2023职教高考报名开启,报考人数继续增加
    Python每日一练(牛客数据分析篇新题库)——第31天:中位函数
    4、wireshark使用教程
  • 原文地址:https://blog.csdn.net/m0_62436868/article/details/126118643