SimpleMessageListener 与 DirectMessageListener [英] SimpleMessageListener vs DirectMessageListener
问题描述
我想看看 DirectMessageListener
和 SimpleMessageListener
之间的区别.我有这张图只是想问问它是否正确.
I'm trying to see difference between DirectMessageListener
and SimpleMessageListener
. I have this drawing just to ask if it is correct.
让我试着描述一下我是如何理解它的,也许你告诉我它是否正确.
Let me try to describe how I understood it and maybe you tell me if it is correct.
在spring-rabbit
前面有一个rabbit-client
java 库,它连接rabbit-mq 服务器并将消息传递给spring-rabbit 库.这个客户端有一些 ThreadPoolExecutor
(我认为在这种情况下有 - 16 个线程).因此,rabbit 中有多少个队列并不重要 - 如果只有一个连接,我会得到 16 个线程.如果我使用 DirectMessageListener
,这些相同的线程会被重用 - 当消息到达时,这个处理程序方法 listen
在所有这 16 个线程中执行.因此,如果我在处理程序中做了一些复杂的事情,rabbit-client
必须等待线程释放才能使用该线程获取下一条消息.此外,如果我将 setConsumersPerQueue
增加到 20,它将为每个队列创建 20 个消费者,但不会创建线程.在我的例子中,这 20*5 个消费者都会重用 ThreadPoolExecutor
提供的这 16 个线程吗?
In front of spring-rabbit
there is rabbit-client
java library, that is connecting to rabbit-mq server and delivering messages to spring-rabbit library. This client has some ThreadPoolExecutor
(which has in this case I think - 16 threads). So, it does not matter how many queues are there in rabbit - if there is a single connection, I get 16 threads. These same threads are reused if I use DirectMessageListener
- and this handler method listen
is executed in all of these 16 threads when messages arrive. So if I do something complex in handler, rabbit-client
must wait for thread to get free in order to get next message using this thread. Also if I increase setConsumersPerQueue
to lets say 20, It will create 20 consumer per queue, but not threads. These 20*5 consumers in my case will all reuse these 16 threads offered by ThreadPoolExecutor
?
SimpleMessageListener
另一方面,会有自己的线程.如果并发消费者 == 1(我猜是默认值,就像我的情况一样)它只有一个线程.每当 secondUseCase*
队列中的任何一个有消息时,rabbit-client
java 库将使用它的 16 个线程之一,将消息转发到单个内部线程我在 SimpleMessageListener
中.一旦转发,rabbit-client
java 库线程被释放,它可以返回从rabbit 服务器获取更多消息.
SimpleMessageListener
on the other hand, would have its own threads. If concurrent consumers == 1 (I guess default as in my case) it has only one thread. Whenever there is a message on any of secondUseCase*
queues, rabbit-client
java library will use one of its 16 threads in my case, to forward message to single internal thread that I have in SimpleMessageListener
. As soon as it is forwarded, rabbit-client
java library thread is freed and it can go back fetching more messages from rabbit server.
推荐答案
你的理解是正确的.
主要区别在于,使用 DMLC,所有侦听器容器中的所有侦听器都在 amqp-client 中的共享线程池上调用(如果需要,您可以增加 16 个).您需要确保池足够大以处理所有容器之间的预期并发性,否则您会饿死.
The main difference is that, with the DMLC, all listeners in all listener containers are called on the shared thread pool in the amqp-client (you can increase the 16 if needed). You need to ensure the pool is large enough to handle your expected concurrency across all containers, otherwise you will get starvation.
因为线程是共享的,所以效率更高.
It's more efficient because threads are shared.
使用 SMLC,您不必担心这一点,但代价是每个并发拥有一个线程.在这种情况下,amqp-client 中的小池通常就足够了.
With the SMLC, you don't have to worry about that, but at the expense of having a thread per concurrency. In that case, a small pool in the amqp-client will generally be sufficient.
这篇关于SimpleMessageListener 与 DirectMessageListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!