获取 Rabbit Listener 服务监听的队列列表

Get liste of queues listen to by RabbitListener service

能否建议一种动态获取 rabbitMQ 监听的队列列表的方法 spring RabbitListener (org.springframework.amqp.rabbit.annotation.RabbitListener)

这是我的监听器示例,它处理来自大量队列的消息(列表可以在属性文件中更新) 我想在另一个服务中动态获取列表:

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

@Service
public class GenericListener {

@RabbitListener(queues = {
                "${queue1}", "${queue2}", "${queue3}", "${queue4}", "${queue5}"
})
 public void receiveMessage(Message message) {

 }
}

具体问题是:如何在运行时动态获取 bean 接口 RabbitListener 参数?

使用 RabbitListenerEndpointRegistry bean。

给听众一个id;例如@RabbitListener(id = "foo", ...) 然后

((AbstractMessageListenerContainer) registry.getListenerContainer("foo")).getQueueNames();

https://docs.spring.io/spring-amqp/docs/current/reference/html/#container-management

Containers created for annotations are not registered with the application context. You can obtain a collection of all containers by invoking getListenerContainers() on the RabbitListenerEndpointRegistry bean. You can then iterate over this collection, for example, to stop or start all containers or invoke the Lifecycle methods on the registry itself, which will invoke the operations on each container.

You can also get a reference to an individual container by using its id, using getListenerContainer(String id) — for example, registry.getListenerContainer("multi") for the container created by the snippet above.

...