問題描述
我有一個項目,我們將在 rabbit 中擁有數百個(可能數千個)隊列,并且每個隊列都需要被一個消費者池消費.
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 注釋,它允許我靜態分配這個特定消費者將處理的隊列.
In rabbit (using spring-amqp), you have the rabbitlistener annotation which allows me to statically assign the queues this particular consumer(s) will handle.
我的問題是 - 對于 rabbit 和 spring,我是否有一種干凈的方式來獲取一段隊列(比如說以 ac 開頭的隊列),然后還監聽在消費者運行時創建的任何隊列.
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.
示例(開始時):
- 螞蟻隊列
- 蘋果隊列
- 貓隊列
消費者運行時:
- 添加蝙蝠隊列
這是我目前擁有的(非常簡單的)代碼:
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 ? >
}
}
我應該補充一下 - 我已經瀏覽了我在網上找到的 spring amqp 文檔,除了靜態(硬編碼或通過屬性)聲明隊列之外,我沒有找到任何東西
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
.獲取對監聽器容器的引用(使用注解上的
id
屬性給它一個已知的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)
).將容器轉換為
AbstractMessageListenerContainer
并調用addQueues()
或addQueueNames()
.Cast the container to an
AbstractMessageListenerContainer
and calladdQueues()
oraddQueueNames()
.請注意,動態添加隊列時使用
DirectMessageListenerContainer
效率更高;使用SimpleMessageListenerContainer
消費者會停止并重新啟動.使用直接容器,每個隊列都有自己的消費者.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).請參閱選擇容器.
這篇關于在運行時向兔子偵聽器動態添加隊列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!