JMS AUTO_ACKNOWLEDGE when is it acknowledged?
Asked Answered
M

2

32

I have tried to google this, but have not been successful. If I am using AUTO_ACKNOWLEDGE, and I have a consumer client written in Java, when is the message acknowledged? I am using a MessageListener which contains an onMessage method. Is the acknowledgement sent back to the server before onMessage or after onMessage completes or at some other point? Thanks in advance for any help anyone is able to provide!

Milord answered 26/7, 2012 at 16:1 Comment(0)
D
35

Please check this one (Wayback Machine link used as article went offline since 2020)

With AUTO_ACKNOWLEDGE mode the acknowledgment is always the last thing to happen implicitly after the onMessage() handler returns. The client receiving the messages can get finer-grained control over the delivery of guaranteed messages by specifying the CLIENT_ACKNOWLEDGE mode on the consuming session.

The use of CLIENT_ACKNOWLEDGE allows the application to control when the acknowledgment is sent. For example, an application can acknowledge a message - thereby relieving the JMS provider of its duty - and perform further processing of the data represented by the message. The key to this is the acknowledge() method on the Message object, as shown in Listing 1.

The acknowledge() method informs the JMS provider that the message has been successfully received by the consumer. This method throws an exception to the client if a provider failure occurs during the acknowledgment process. The provider failure results in the message being retained by the JMS server for redelivery.

Deckert answered 26/7, 2012 at 16:35 Comment(4)
What happens if an exception is thrown during the processing of onMessage()? Will the message be redelivered if the AUTO_ACKNOWLEDGE is set?Eaten
@Eaten - If exception is thrown in onReceive or in the JMSListener then the acknowledgment is not sent and will be redeliveredRiccio
If receiving messages synchronously with AUTO_ACKNOWLEDGE, the message is acknowledged when calling the consumer.receive methodDove
@Riccio - can you describe in detail how is the message redelivered and who delivers the message again and when does this redelivering happens?Peavey
G
15

CLIENT_ACKNOWLEDGE
With this acknowledgment mode, the client acknowledges a consumed message by calling the message's acknowledge method.

Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
...
msg = (TextMessage) consumer.receive();
//acknowledge
msg.acknowledge();

AUTO_ACKNOWLEDGE
With this acknowledgment mode, the session automatically acknowledges a client's receipt of a message either when the session has successfully returned from a call to receive or when the message listener the session has called to process the message successfully returns.

Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);

source
Best example

Galloping answered 17/12, 2015 at 7:29 Comment(2)
Quite descriptive and helpful. Thanks!Capuchin
"when the message listener the session has called to process the message successfully returns." sounds weirdLucielucien

© 2022 - 2024 — McMap. All rights reserved.