代码之家  ›  专栏  ›  技术社区  ›  Ben

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

  •  0
  • Ben  · 技术社区  · 6 年前

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

    @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
    
    }
    
    1 回复  |  直到 6 年前
        1
  •  2
  •   Boris    6 年前

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

    listener.addQueueNames(queueName);
    

    addQueueNames() 方法将在运行时将队列添加到容器中。看到了吗 here