如何使用 Spring Boot 通过扇出交换在 RabbitMQ 上发布消息 [英] How to publish messages on RabbitMQ with fanout exchange using Spring Boot
本文介绍了如何使用 Spring Boot 通过扇出交换在 RabbitMQ 上发布消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下一段代码,它使用 fanout exchange
将消息发布到 RabbitMQ
队列.exchange
正在创建,但在 RabbitMQ
队列中看不到消息.我也没有看到任何错误.
I have the following piece of code that publishes messages onto RabbitMQ
queues using fanout exchange
. The exchange
is getting created but the message cannot be see in RabbitMQ
queues. I am not seeing any error either.
BasicApplication.java
@SpringBootApplication
public class BasicApplication {
public static final String QUEUE_NAME_1 = "helloworld.fanout.q1";
public static final String QUEUE_NAME_2 = "helloworld.fanout.q2";
public static final String EXCHANGE_NAME = "helloworld.fanout.x";
//here the message ==> xchange ==> queue1, queue2
@Bean
public List<Declarable> fanoutBindings() {
Queue fanoutQueue1 = new Queue(QUEUE_NAME_1, false);
Queue fanoutQueue2 = new Queue(QUEUE_NAME_2, false);
FanoutExchange fanoutExchange = new FanoutExchange(EXCHANGE_NAME);
return Arrays.asList(
fanoutQueue1,
fanoutQueue2,
fanoutExchange,
bind(fanoutQueue1).to(fanoutExchange),
BindingBuilder.bind(fanoutQueue2).to(fanoutExchange));
}
public static void main(String[] args) {
SpringApplication.run(BasicApplication.class, args).close();
}
}
Producer.java
@Component
public class Producer implements CommandLineRunner {
@Autowired
private RabbitTemplate rabbitTemplate;
@Override
public void run(String... args) throws Exception {
this.rabbitTemplate.convertAndSend(EXCHANGE_NAME, "Hello World !");
}
}
推荐答案
您使用了错误的 convertAndSend
方法;该方法的第一个参数是 routingKey
.
You are using the wrong convertAndSend
method; the first argument to that method is the routingKey
.
使用this.rabbitTemplate.convertAndSend(EXCHANGE_NAME, "", "Hello World !");
.
这篇关于如何使用 Spring Boot 通过扇出交换在 RabbitMQ 上发布消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文