RabbitMQ: Dynamic addition of queues to a listener at runtime

有些话、适合烂在心里 提交于 2020-08-10 21:08:11

问题


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.


回答1:


When you declare this:

public void processAnimals(Animal animalObj, 
                                  @Header(AmqpHeaders.CONSUMER_QUEUE) String queue) 

You must ensure the String is a JSON String. Before processing it, you could convert it via:

String jsonStringQueue= objectMapper.writeValueAsString(queue);



回答2:


Your question is confusing - the headline is about adding new queues but then you talk about JSON exceptions. Sounds like you are receiving non-json content.

What is the problem exactly?

To add new queues,

((AbsrtactMessageListenerContainer) rabbitListenEndpointRegistry.getMessageListenerContainer("animalQContainer"))
    .addQeueNames("newQueue1", "newQueue2");



回答3:


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();


来源:https://stackoverflow.com/questions/63162812/rabbitmq-dynamic-addition-of-queues-to-a-listener-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!