Spring rabbitmq 将新队列附加到现有侦听器

Spring rabbitmq attaching new queue to existing listener

我需要动态声明新队列并将其分配给我现有的侦听器。

我有一个这样声明的监听器:

@Component
public class AccountListener {
    @RabbitListener(id = "foobar")
    public String foo(String a) {
        System.out.println(a);
        return a + "xxx";
    }
}

我可以使用 RabbitListenerEndpointRegistry 检索此侦听器,但如何通过队列公开它?

@Autowired
private AmqpAdmin rabbit;
@Autowired
private RabbitListenerEndpointRegistry registry;


public void exposeQueue(String queueName) throws Exception {
      Queue queue = new Queue(queueName, false);

      rabbit.declareQueue(queue);
      SimpleMessageListenerContainer listener = (SimpleMessageListenerContainer) registry.getListenerContainer("foobar");

     // Attach $listener to $queue here

}

您应该将队列添加到容器的队列列表中:

listener.addQueueNames(queueName);

addQueueNames() method will add the queue to the container at runtime. See here 了解更多信息。