简体   繁体   English

RabbitMq 监听器在两个不同的队列上监听两种不同的消息类型

[英]RabbitMq listener listening to two different message types on two different queues

My requirement is to be able to listen to rabbitMq messages on two different queues.我的要求是能够在两个不同的队列上收听 rabbitMq 消息。 One queue receives message with content_type = text/plain and my Spring boot listener method accepts String parameter.一个队列接收到 content_type = text/plain 的消息,而我的 Spring 启动侦听器方法接受字符串参数。 Second queue receives message with content_type = application/json and my listener method accepts parameter of my POJO 'User' type.第二个队列接收带有 content_type = application/json 的消息,并且我的侦听器方法接受我的 POJO“用户”类型的参数。 I am sending messages using RabbitMQ web portal.我正在使用 RabbitMQ web 门户发送消息。 I am not able to successfully listen to both types of messages in the same spring boot listener application.我无法在同一个 spring 启动侦听器应用程序中成功收听这两种类型的消息。 Please help me to succeed in listening to both types of messages on two queues.Below is my listener class and configuraiton class code snippet.请帮助我成功收听两个队列上的两种类型的消息。下面是我的监听器 class 和配置 class 代码片段。

Listener class:监听器 class:

@Component
public class EventListener {

    public void processFirstQueue(String message) {
        try {
            if (message != null) {
                log.info("Received the message from Queue!");
                service.process(message);
            }
        } catch (Exception e) {
            log.error("Error occurred " + e);
        }
    }
    
    public void processSecondQueue(User user) {
        try {
            if (user!= null) {
                log.info("Received the message from Queue!");
                service.processUser(user);
            }
        } catch (Exception e) {
            log.error("Error occurred " + e);
        }
    }
}

RabbitMqConfig.java
public class RabbitMqConfig {
    
    @Bean(name = "rabbitmq-server")
    public CachingConnectionFactory getConnectionFactory() {
        CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
        cachingConnectionFactory.setHost("localhost");
        return cachingConnectionFactory;
    }
            
    @Bean
    public SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames("queue1", "queue2");
        container.setMessageListener(listenerAdapter);
        container.setMessageConverter(new Jackson2JsonMessageConverter());
        return container;
    }

    @Bean
    public MessageListenerAdapter listenerAdapter(OutgoingEventListener receiver) {
        MessageListenerAdapter listener = new MessageListenerAdapter(receiver);
        Map<String, String> queueToMethodName = new HashMap<>();
        queueToMethodName.put("queue1", "processFirstQueue");
        queueToMethodName.put("queue2", "processSecondQueue");
        listener.setQueueOrTagToMethodName(queueToMethodName);
        return listener;
    }
     @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    
     @Bean
    public RabbitTemplate getRabbitTemplate(ConnectionFactory connectionFactory) {
        return new RabbitTemplate(connectionFactory);
    }
    
}

I am able to post json message successfully to queue2 as seen in below screenshot.我能够成功地将 json 消息发布到 queue2,如下面的屏幕截图所示。

在此处输入图像描述

But when I am posting content_type = text/plain to queue1 as seen in below screenshot, I am getting error in my Spring boot service saying content-type text is found but json is expected.但是,当我将 content_type = text/plain 发布到 queue1 时,如下面的屏幕截图所示,我的 Spring 启动服务中出现错误,提示找到了内容类型文本,但预计会出现 json。

在此处输入图像描述

Try to add message converter for your consumer like that -尝试像这样为您的消费者添加消息转换器 -

import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
 
@Configuration
public class RabbitMqConfig {
  
    @Bean
    public MessageConverter jsonMessageConverter(){
        return new Jackson2JsonMessageConverter();
    }
    
    @Bean
    public SimpleRabbitListenerContainerFactory jsaFactory(ConnectionFactory connectionFactory,
            SimpleRabbitListenerContainerFactoryConfigurer configurer) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setMessageConverter(jsonMessageConverter());
        return factory;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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