赞
踩
- /**
- * @author Mr.simple
- * @description 发布确认高级 (交换机或者队列未接收到信息)
- * @time 2021/12/22 16:38
- */
- @Configuration
- public class ConfirmConfig {
- public static final String CONFIRM_EXCHANGE = "confirm_exchange";
- public static final String CONFIRM_QUEUE = "confirm_queue";
- public static final String CONFIRM_ROUTING_KEY = "confirm_routing_key";
-
- //声明交换机
- @Bean("confirmExchange")
- public DirectExchange confirmExchange() {
- return new DirectExchange(CONFIRM_EXCHANGE);
- }
-
- //声明队列
- @Bean("confirmQueue")
- public Queue confirmQueue() {
- return QueueBuilder.durable(CONFIRM_QUEUE).build();
- }
-
- //绑定
- @Bean("confirmQueueBinding")
- public Binding confirmQueueBinding(@Qualifier("confirmQueue") Queue confirmQueue,
- @Qualifier("confirmExchange") DirectExchange confirmExchange) {
- return BindingBuilder.bind(confirmQueue).to(confirmExchange).with(CONFIRM_ROUTING_KEY);
- }
- }
- @RestController
- @RequestMapping("/confirm")
- public class ConfirmController {
- @Autowired
- private RabbitTemplate rabbitTemplate;
-
- @GetMapping("/sendConfirm")
- public void sendConfirm(@RequestParam("msg") String msg) {
- CorrelationData correlationData = new CorrelationData("1");
- CorrelationData correlationData2 = new CorrelationData("2");
- rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE, ConfirmConfig.CONFIRM_ROUTING_KEY, msg+"key1", correlationData);
- log.info("发送信息{} ", msg+"key1");
- rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE, ConfirmConfig.CONFIRM_ROUTING_KEY+"key2", msg+"key2", correlationData2);
- log.info("发送信息{} ", msg+"key2--错误");
- }
- }
- @Slf4j
- @Component
- public class ConfirmConsumer {
-
- @RabbitListener(queues =ConfirmConfig.CONFIRM_QUEUE)
- public void receive(Message message) {
- String s = new String(message.getBody());
- log.info("队列接收到信息=={}, routingkey为{confirm_routing_key}", s);
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。