问题
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.
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
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.
回答1:
Your understanding is correct.
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.
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.
来源:https://stackoverflow.com/questions/58661518/simplemessagelistener-vs-directmessagelistener