問(wèn)題描述
我有一個(gè)項(xiàng)目,我們將在 rabbit 中擁有數(shù)百個(gè)(可能數(shù)千個(gè))隊(duì)列,并且每個(gè)隊(duì)列都需要被一個(gè)消費(fèi)者池消費(fèi).
I've got a project where we are going to have hundreds (potentially thousands) of queues in rabbit and each of these queues will need to be consumed by a pool of consumers.
在 rabbit(使用 spring-amqp)中,你有 rabbitlistener 注釋,它允許我靜態(tài)分配這個(gè)特定消費(fèi)者將處理的隊(duì)列.
In rabbit (using spring-amqp), you have the rabbitlistener annotation which allows me to statically assign the queues this particular consumer(s) will handle.
我的問(wèn)題是 - 對(duì)于 rabbit 和 spring,我是否有一種干凈的方式來(lái)獲取一段隊(duì)列(比如說(shuō)以 ac 開(kāi)頭的隊(duì)列),然后還監(jiān)聽(tīng)在消費(fèi)者運(yùn)行時(shí)創(chuàng)建的任何隊(duì)列.
My question is - with rabbit and spring, is there a clean way for me to grab a section of queues (lets say queues that start with a-c) and then also listen for any queues that are created while the consumer is running.
示例(開(kāi)始時(shí)):
- 螞蟻隊(duì)列
- 蘋果隊(duì)列
- 貓隊(duì)列
消費(fèi)者運(yùn)行時(shí):
- 添加蝙蝠隊(duì)列
這是我目前擁有的(非常簡(jiǎn)單的)代碼:
Here is the (very simple) code I currently have:
@Component
public class MessageConsumer {
public MessageConsumer() {
// ideally grab a section of queues here, initialize a parameter and give to the rabbitlistener annotation
}
@RabbitListener(queues= {"ant-queue", "apple-queue", "cat-queue"})
public void processQueues(String messageAsJson) {
< how do I update the queues declared in rabbit listener above ? >
}
}
我應(yīng)該補(bǔ)充一下 - 我已經(jīng)瀏覽了我在網(wǎng)上找到的 spring amqp 文檔,除了靜態(tài)(硬編碼或通過(guò)屬性)聲明隊(duì)列之外,我沒(méi)有找到任何東西
I should add - I've gone through the spring amqp documentation I found online and I haven't found anything outside of statically (either hardcoded or via properties) declaring the queues
推薦答案
注入(
@Autowired
或其他方式)RabbitListenerEndpointRegistry
.獲取對(duì)監(jiān)聽(tīng)器容器的引用(使用注解上的
id
屬性給它一個(gè)已知的id)(registry.getListenerContainer(id)
).Get a reference to the listener container (use the
id
attribute on the annotation to give it a known id) (registry.getListenerContainer(id)
).將容器轉(zhuǎn)換為
AbstractMessageListenerContainer
并調(diào)用addQueues()
或addQueueNames()
.Cast the container to an
AbstractMessageListenerContainer
and calladdQueues()
oraddQueueNames()
.請(qǐng)注意,動(dòng)態(tài)添加隊(duì)列時(shí)使用
DirectMessageListenerContainer
效率更高;使用SimpleMessageListenerContainer
消費(fèi)者會(huì)停止并重新啟動(dòng).使用直接容器,每個(gè)隊(duì)列都有自己的消費(fèi)者.Note that is more efficient to use a
DirectMessageListenerContainer
when adding queues dynamically; with aSimpleMessageListenerContainer
the consumer(s) are stopped and restarted. With the direct container, each queue gets its own consumer(s).請(qǐng)參閱選擇容器.
這篇關(guān)于在運(yùn)行時(shí)向兔子偵聽(tīng)器動(dòng)態(tài)添加隊(duì)列的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!
【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!