How to publish messages on RabbitMQ with fanout exchange using Spring Boot

我与影子孤独终老i 提交于 2020-01-04 06:32:28

问题


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 !");
    }

}

回答1:


You are using the wrong convertAndSend method; the first argument to that method is the routingKey.

Use this.rabbitTemplate.convertAndSend(EXCHANGE_NAME, "", "Hello World !");.



来源:https://stackoverflow.com/questions/45803231/how-to-publish-messages-on-rabbitmq-with-fanout-exchange-using-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!