Ben
Ben

Reputation: 4279

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

}

Upvotes: 0

Views: 1403

Answers (1)

Boris
Boris

Reputation: 24443

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.

Upvotes: 2

Related Questions