Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple Rabbitmq queues with spring boot

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 create more then 1 queue? how would it be possible?

Obviously, I can't just create the same bean twice:

@Bean
Queue queue() {
    return new Queue(queueNameAAA, false);
}

@Bean
Queue queue() {
    return new Queue(queueNameBBB, false);
}

You can't create the same bean twice, it will make ambiguous.

like image 392
winter Avatar asked Dec 18 '16 16:12

winter


People also ask

Can RabbitMQ have multiple queues?

Use multiple queues and consumers Queues are single-threaded in RabbitMQ, and one queue can handle up to about 50 thousand messages. You will achieve better throughput on a multi-core system if you have multiple queues and consumers and if you have as many queues as cores on the underlying node(s).

Can RabbitMQ have multiple consumers?

No it's not, single queue/multiple consumers with each consumer handling the same message ID isn't possible. Having the exchange route the message onto into two separate queues is indeed better.

Does RabbitMQ automatically create queues?

In RabbitMQ, a producer never sends a message directly to a queue. Instead, it uses an exchange as a routing mediator. Therefore, the exchange decides if the message goes to one queue, to multiple queues, or is simply discarded.


1 Answers

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());
}
like image 93
Gary Russell Avatar answered Sep 28 '22 03:09

Gary Russell