当前位置:   article > 正文

RabbitMQ整合springboot(三)---消息回退之交换机或队列未收到消息_rabbitmq 回调收不到消息

rabbitmq 回调收不到消息

(一)、配置文件

  1. /**
  2. * @author Mr.simple
  3. * @description 发布确认高级 (交换机或者队列未接收到信息)
  4. * @time 2021/12/22 16:38
  5. */
  6. @Configuration
  7. public class ConfirmConfig {
  8. public static final String CONFIRM_EXCHANGE = "confirm_exchange";
  9. public static final String CONFIRM_QUEUE = "confirm_queue";
  10. public static final String CONFIRM_ROUTING_KEY = "confirm_routing_key";
  11. //声明交换机
  12. @Bean("confirmExchange")
  13. public DirectExchange confirmExchange() {
  14. return new DirectExchange(CONFIRM_EXCHANGE);
  15. }
  16. //声明队列
  17. @Bean("confirmQueue")
  18. public Queue confirmQueue() {
  19. return QueueBuilder.durable(CONFIRM_QUEUE).build();
  20. }
  21. //绑定
  22. @Bean("confirmQueueBinding")
  23. public Binding confirmQueueBinding(@Qualifier("confirmQueue") Queue confirmQueue,
  24. @Qualifier("confirmExchange") DirectExchange confirmExchange) {
  25. return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_ROUTING_KEY);
  26. }
  27. }

(二)、消息生产者

  1. @RestController
  2. @RequestMapping("/confirm")
  3. public class ConfirmController {
  4. @Autowired
  5. private RabbitTemplate rabbitTemplate;
  6. @GetMapping("/sendConfirm")
  7. public void sendConfirm(@RequestParam("msg") String msg) {
  8. CorrelationData correlationData = new CorrelationData("1");
  9. CorrelationData correlationData2 = new CorrelationData("2");
  10. rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE, ConfirmConfig.CONFIRM_ROUTING_KEY, msg+"key1", correlationData);
  11. log.info("发送信息{} ", msg+"key1");
  12. rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE, ConfirmConfig.CONFIRM_ROUTING_KEY+"key2", msg+"key2", correlationData2);
  13. log.info("发送信息{} ", msg+"key2--错误");
  14. }
  15. }

(3)、消息消费者

  1. @Slf4j
  2. @Component
  3. public class ConfirmConsumer {
  4. @RabbitListener(queues =ConfirmConfig.CONFIRM_QUEUE)
  5. public void receive(Message message) {
  6. String s = new String(message.getBody());
  7. log.info("队列接收到信息=={}, routingkey为{confirm_routing_key}", s);
  8. }
  9. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/1018382
推荐阅读
相关标签
  

闽ICP备14008679号