简体   繁体   中英

Spring rabbitmq attaching new queue to existing listener

I have a need to dynamically declare and assign new queues to my existing listener.

I have a listener declared like so:

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

I can retrieve this listener using RabbitListenerEndpointRegistry , but how do I expose it via a queue?

@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

}

You should add the queue to the container's list of queues:

listener.addQueueNames(queueName);

addQueueNames() method will add the queue to the container at runtime. See here for more info.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM