Search code examples
javarabbitmqspring-amqp

RabbitMQ: Dynamic addition of queues to a listener at runtime


I have a use case where I need to consume messages from queues which are discovered at runtime.

Here I have a config class and the listener class. I have defined a consumer for the two existing queues and want to consume messages from new queues which may be discovered at runtime and follow the same naming convention i.e. queue.animals.*

Also, I have another service which will send me the newly discovered queue name on a queue named "newQueues". This approach can be changed if not needed and we can get rid of the service sending messages on "newQueues".

@EnableRabbit
public class RabbitConfiguration implements RabbitListenerConfigurer {

    public static final String queue1= "queue.animals.cat";
    public static final String queue2= "queue.animals.dog";

    @Autowired
    private ConnectionFactory connectionFactory;

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory);
    }
    @Bean
    public DirectRabbitListenerContainerFactory rabbitListenerContainerFactory() {
        DirectRabbitListenerContainerFactory factory = new DirectRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(new Jackson2JsonMessageConverter());
        return factory;
    }

    @Bean
    public RabbitListenerEndpointRegistry listenerEndpointRegistry() {
        return new RabbitListenerEndpointRegistry();
    }

    @Override
    public void configureRabbitListeners(RabbitListenerEndpointRegistrar registrar) {
        registrar.setEndpointRegistry(listenerEndpointRegistry());
    }

@Autowired
private RabbitListenerEndpointRegistry listenerEndpointRegistry;

@RabbitListener(id = "qEvent", queues = {"newQueues"})
public void processQueueEvents(String newQueueName) {
    ((DirectMessageListenerContainer) this.listenerEndpointRegistry.getListenerContainer("animalQContainer"))
        .addQueueNames(newQueueName);

    System.out.println("Received a message with the new queue name: " + newQueueName);
    
}

@RabbitListener(id = "animalQContainer" , queues = { queue1, queue2 })
public void processAnimals(Animal animalObj, @Header(AmqpHeaders.CONSUMER_QUEUE) String queue) {
    System.out.println("Received a message on queue: " + queue + "data: " + animalObj);
    //process animalObj
}

I am getting the following exception currently:

Caused by: com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'queue': was expecting (JSON String, Number, Array, Object or token 'null', 'true' or 'false')

I am very new to RabbitMQ, so not sure if I have all the pieces correctly. Thank you for your help.


Solution

  • Since the Jackson2JsonMessageConverter is configured here, the processQueueEvents method can not parse a string. Created a new class and passed in an object to the processQueueEvent method to get past the exception mentioned in the question:

        public void processQueueEvents(NewQueue newQueueName) {
            System.out.println("Received a message on a new queue: " + newQueueName);
            String name = newQueueName.toString();