在运行时向兔子侦听器动态添加队列

2023-02-20Java开发问题
2

本文介绍了在运行时向兔子侦听器动态添加队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个项目,我们将在 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 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).

    请参阅选择容器.

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

    The End

相关推荐

如何使用 JAVA 向 COM PORT 发送数据?
How to send data to COM PORT using JAVA?(如何使用 JAVA 向 COM PORT 发送数据?)...
2024-08-25 Java开发问题
21

如何使报表页面方向更改为“rtl"?
How to make a report page direction to change to quot;rtlquot;?(如何使报表页面方向更改为“rtl?)...
2024-08-25 Java开发问题
19

在 Eclipse 项目中使用西里尔文 .properties 文件
Use cyrillic .properties file in eclipse project(在 Eclipse 项目中使用西里尔文 .properties 文件)...
2024-08-25 Java开发问题
18

有没有办法在 Java 中检测 RTL 语言?
Is there any way to detect an RTL language in Java?(有没有办法在 Java 中检测 RTL 语言?)...
2024-08-25 Java开发问题
11

如何在 Java 中从 DB 加载资源包消息?
How to load resource bundle messages from DB in Java?(如何在 Java 中从 DB 加载资源包消息?)...
2024-08-25 Java开发问题
13

如何更改 Java 中的默认语言环境设置以使其保持一致?
How do I change the default locale settings in Java to make them consistent?(如何更改 Java 中的默认语言环境设置以使其保持一致?)...
2024-08-25 Java开发问题
13