multiple Rabbitmq queues with spring boot

后端 未结 1 1284
无人共我
无人共我 2021-02-02 15:47

From spring boot tutorial: https://spring.io/guides/gs/messaging-rabbitmq/

They give an example of creating 1 queue and 1 queue only, but, what if I want to be able to c

相关标签:
1条回答
  • 2021-02-02 16:32

    Give the bean definition factory methods different names. Usually, by convention, you would name them the same as the queue, but that's not required...

    @Bean
    Queue queue1() {
        return new Queue(queueNameAAA, false);
    }
    
    @Bean
    Queue queue2() {
        return new Queue(queueNameBBB, false); 
    }
    

    The method name is the bean name.

    EDIT

    When using the queues in the binding beans, there are two options:

    @Bean
    Binding binding1(@Qualifier("queue1") Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueNameAAA);
    }
    
    @Bean
    Binding binding2(@Qualifier("queue2") Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(queueNameBBB);
    }
    

    or

    @Bean
    Binding binding1(TopicExchange exchange) {
        return BindingBuilder.bind(queue1()).to(exchange).with(queueNameAAA);
    }
    
    @Bean
    Binding binding2(TopicExchange exchange) {
        return BindingBuilder.bind(queue2()).to(exchange).with(queueNameBBB);
    }
    

    or even better...

    @Bean
    Binding binding1(TopicExchange exchange) {
        return BindingBuilder.bind(queue1()).to(exchange).with(queue1().getName());
    }
    
    @Bean
    Binding binding2(TopicExchange exchange) {
        return BindingBuilder.bind(queue2()).to(exchange).with(queue2().getName());
    }
    
    0 讨论(0)
提交回复
热议问题