在运行时将队列动态添加到Rabbit侦听器 [英] Dynamic addition of queues to a rabbit listener at runtime

查看:52
本文介绍了在运行时将队列动态添加到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.

我的问题是-对于兔子和春天,有没有一种干净的方法可以让我抓取一部分队列(比如说以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 call addQueues() or addQueueNames().

    请注意,动态添加队列时,使用 DirectMessageListenerContainer 效率更高;使用 SimpleMessageListenerContainer ,使用者将停止并重新启动.使用直接容器,每个队列都有自己的使用者.

    Note that is more efficient to use a DirectMessageListenerContainer when adding queues dynamically; with a SimpleMessageListenerContainer the consumer(s) are stopped and restarted. With the direct container, each queue gets its own consumer(s).

    请参见选择容器.

    这篇关于在运行时将队列动态添加到Rabbit侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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