[英]multiple Rabbitmq queues with spring boot
From spring boot tutorial: https://spring.io/guides/gs/messaging-rabbitmq/来自spring boot教程: 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?他们给出了仅创建 1 个队列和 1 个队列的示例,但是,如果我希望能够创建多于 1 个队列怎么办? how would it be possible?
怎么可能?
Obviously, I can't just create the same bean twice:显然,我不能只创建同一个 bean 两次:
@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.你不能两次创建同一个 bean,它会产生歧义。
Give the bean definition factory methods different names.给 bean 定义工厂方法不同的名称。 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.方法名称是 bean 名称。
EDIT编辑
When using the queues in the binding beans, there are two options:在绑定 bean 中使用队列时,有两种选择:
@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());
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.