如何使用自动连线的Spring Boot侦听多个队列? [英] How to listen to multiple queues with autowired Spring Boot?

查看:166
本文介绍了如何使用自动连线的Spring Boot侦听多个队列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Spring Boot的新手,我正在玩它.目前,我已经构建了一些应用,希望能够通过队列相互通信. 我目前有一个侦听器对象,可以从特定队列接收消息.

I'm new to Spring boot and I'm playing around with it. Currently I've build some apllications that I want to be able to communicate with each other through queues. I currently have a Listener object that can receive message from a particular queue.

@Configuration
public class Listener {

    final static String queueName = "myqueue";

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    Receiver receiver() {
        return new Receiver();
    }

    @Bean
    MessageListenerAdapter listenerAdapter(Receiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }
}

这有效.但是,现在我希望能够收听另一个队列.所以我想我会复制上面的对象并更改队列名称.不幸的是,这没有用,因为Spring boot只为其中一个创建连接. 关于如何让Spring Boot应用程序侦听多个队列的任何想法?

This works. However, now I want to be able to listen to another queue. So I figured I'd copy the above object and change the queue name. Unfortunately this did not work as Spring boot only creates a connection for one of them. Any ideas on how I can have my Spring Boot application listen to multiple queues?

推荐答案

好,我想出了如何让它侦听多个队列的方法.认为与我的其他解决方案相比,可能存在一些缺点,主要是如果列出的队列不存在,则它不起作用. 我最终使用了一个完全不同的方法,即使用@RabbitListener

Ok, I figured out how to get it to listen to multiple queues. Think there might be some downsides compared to my other solution, mainly that it doesn't work if the queue listed does not exist. I ended up using a totally different approach using a @RabbitListener

@Component
public class EventListener {

    private static Logger LOG = LoggerFactory.getLogger(EventListener.class);
    private CountDownLatch latch = new CountDownLatch(1);

    @RabbitListener(queues = "myqueue")
    public void processPaymentMessage(Object message) {
        LOG.info("Message is of type: " + message.getClass().getName());
        if(!(message instanceof byte[])) message = ((Message) message).getBody();
        String content = new String((byte[])message, StandardCharsets.UTF_8);
        LOG.info("Received on myqueue: " + content);
        latch.countDown();
    }

    @RabbitListener(queues = "myotherqueue")
    public void processOrderMessage(Object message) {
        LOG.info("Message is of type: " + message.getClass().getName());
        if(!(message instanceof byte[])) message = ((Message) message).getBody();
        String content = new String((byte[])message, StandardCharsets.UTF_8);           
        LOG.info("Received on myotherqueue: " + content);
        latch.countDown();
    }   
}

整个检查byte []就在那儿,因为从命令行发送的消息看起来像什么.否则,它是org.springframework.amqp.core.Message.

The whole check on byte[] is in there because that what a message send from the commandline looks like. Otherwise it's a org.springframework.amqp.core.Message.

这篇关于如何使用自动连线的Spring Boot侦听多个队列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆