多个带有 Spring Boot 的 Rabbitmq 队列 [英] multiple Rabbitmq queues with spring boot

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

问题描述

来自 Spring Boot 教程:https://spring.io/guides/gs/messaging-rabbitmq/p>

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

显然,我不能两次创建同一个 bean:

@Bean队列队列(){返回新队列(queueNameAAA,false);}@豆角,扁豆队列队列(){返回新队列(queueNameBBB,假);}

你不能两次创建同一个bean,它会变得模棱两可.

解决方案

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

@Bean队列 queue1() {返回新队列(queueNameAAA,false);}@豆角,扁豆队列 queue2() {返回新队列(queueNameBBB,假);}

方法名就是bean名.

编辑

在绑定bean中使用队列时,有两种选择:

@Bean绑定 binding1(@Qualifier("queue1") 队列队列,TopicExchange 交换) {return BindingBuilder.bind(queue).to(exchange).with(queueNameAAA);}@豆角,扁豆绑定 binding2(@Qualifier("queue2") 队列队列,TopicExchange 交换) {return BindingBuilder.bind(queue).to(exchange).with(queueNameBBB);}

@Bean绑定 binding1(TopicExchange 交换) {return BindingBuilder.bind(queue1()).to(exchange).with(queueNameAAA);}@豆角,扁豆绑定 binding2(TopicExchange 交换){return BindingBuilder.bind(queue2()).to(exchange).with(queueNameBBB);}

甚至更好...

@Bean绑定 binding1(TopicExchange 交换) {return BindingBuilder.bind(queue1()).to(exchange).with(queue1().getName());}@豆角,扁豆绑定 binding2(TopicExchange 交换){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天全站免登陆