简体   繁体   English

使用 spring boot 的多个 Rabbitmq 队列

[英]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.

相关问题 使用Spring Boot创建RabbitMQ队列 - Create RabbitMQ queues with spring boot 我可以在 rabbitMQ 和 spring 引导中将多个队列绑定到同一个使用者吗? - Can I bind multiple queues to the same consumer in rabbitMQ and spring boot? 如何使用自动装配的Spring Boot监听多个队列? - How to listen to multiple queues with autowired Spring Boot? 如何在spring boot中配置多个solace队列 - How to configure multiple solace queues in spring boot Spring RabbitMQ:一个@RabbitListener 指定队列优先级的多个队列 - Spring RabbitMQ: one @RabbitListener multiple queues with the priority of the specified queue Spring Boot 多个 RabbitMQ 监听器到单个容器 - Spring Boot Multiple RabbitMQ Listeners to Single Container 具有多个消费者的 Spring Boot RabbitMQ 单队列 - Spring Boot RabbitMQ Single Queue with multiple consumers 当我添加 spring-boot-starter-web-services 时 spring 云 rabbitmq 不创建队列 - spring cloud rabbitmq not create queues when i add spring-boot-starter-web-services 如何在 Spring Boot 中为现有的 RabbitMQ 队列关闭自动队列声明? - How to turn off auto queue declaration in Spring Boot for existing RabbitMQ queues? 是否可以在 Spring 引导中收听多个 TIbco EMS 队列? - Is it possible to listen to multiple TIbco EMS queues in Spring boot?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM