DMLC message consumption and acknowledgement

2019-09-04 11:21发布

问题:

This is a follow up question for Difference between AUTO_ACKNOWLEDGEMENT mode with and without Spring JMS.

I am using DMLC and my concurrent consumers count is 1. The prefetch limit is > 1. I received a message and it is acknowledged before listener is executed. So, while the listener is executing, broker has more messages and it sends it to consumer as per prefetch settings. Since the listener is still executing, how will the consumption and acknowledgement works for subsequent messages?

Will the receive() be called for all new messages and will they be acknowledged and wait for listener execution to complete? [If this is the case then I am confused why I am getting unacknowledgedmessagecount in consumer stats] OR The receive() will be called but next message will not be acknowledged until the previous listener has completed its execution? [this could potentially explain the unacknowledged messages if my listener execution is blocked due to some other reason] OR Something else happens under the hood.

Can someone please explain this? It will help me a lot.

Thanks and Cheers!

回答1:

No. receive() is called to get the first message; it is acknowledged when receive returns; the container thread then invokes the listener. (You have to use transactions with the DMLC if you want to roll back a failed delivery - if your listener throws an exception).

Any prefetched messages are delivered by the broker to the client library, but they will not be seen by Spring until your listener exits (onMessage()) after processing the previous message. i.e. receive() is called on the same thread as the MessageListener.

Prefetch is good in that the next message will be immediately available for the next receive(), but the downside is, if you have multiple consumer threads and a listener can sometimes take a long time to process a message, prefetched messages can sit in his queue while other threads are idle.