本文介绍了正确使用 SpringRabbit DirectMessageListenerContainer 和 setConsumersPerQueue 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 DirectMessageListenerContainer 来侦听单个队列,因为它为每个队列创建了一个使用者,如果queue1"上的消息需要一段时间来处理,而queue1"上的消息需要处理,则可以防止队列饥饿queue2"的处理速度很快.我对 DirectMessageListenerContainer.setConsumersPerQueue 方法感到困惑.默认值为 1 查看源代码.

I use the DirectMessageListenerContainer to listen to individual queues, since it creates a consumer per queue, preventing queue starvation if a messages on say "queue1" take a while to process, while the one on "queue2" are quick to process. I am confused about the DirectMessageListenerContainer.setConsumersPerQueue method. The default value is 1 looking at the source code.

在什么情况下需要将其增加到大于 1?如果保留默认值 1,如果获取大小大于 1,是否以多线程方式为给定队列处理消息?

Under what conditions would one need to increase it to greater than 1? If left to default value of 1, is the processing of messages done in a multithreaded way for a given queue, if fetch size is greater than 1?

    DirectMessageListenerContainer
        listenerContainer =
        new DirectMessageListenerContainer(connectionFactory);

    listenerContainer.setConsumersPerQueue(10);
    listenerContainer.addQueueNames("queue1","queue2");

推荐答案

是为了增加并发;例如,如果您的侦听器很慢,并且您不关心消息排序,则可以将多个消费者分配到同一个队列并并行处理消息.根据您的消息数量,您可能需要减少预取数量,以便一个消费者不会在启动期间抓取"所有消息.

It's to increase concurrency; for example if your listener is slow, and you don't care about message ordering, you can assign multiple consumers to the same queue and process messages in parallel. Depending on your message count, you may need to reduce the prefetch count so that one consumer doesn't "grab" all the messages during startup.

预取大小对并发没有影响;就是这样;预取.

The prefetch size has no effect on concurrency; it is just that; prefetch.

这篇关于正确使用 SpringRabbit DirectMessageListenerContainer 和 setConsumersPerQueue 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 04:21