使用Spring Boot启动多个Rabbitmq队列 [英] multiple Rabbitmq queues with spring boot

查看:535
本文介绍了使用Spring Boot启动多个Rabbitmq队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从Spring Boot教程中: https://spring.io/guides/gs/messaging-rabbitmq/

他们给出了一个仅创建1个队列和1个队列的示例,但是,如果我希望能够创建1个以上的队列怎么办?怎么可能?

显然,我不能两次创建相同的bean:

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

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

您不能创建相同的bean两次,这将导致模棱两可.

解决方案

为bean定义工厂方法提供不同的名称.通常,按照惯例,您可以将它们命名为与队列相同的名称,但这不是必需的...

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

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

方法名称是bean名称.

编辑

在绑定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);
}

@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);
}

甚至更好...

@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());
}

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.

解决方案

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());
}

这篇关于使用Spring Boot启动多个Rabbitmq队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆