31

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!

Reid Mac
  • 2,411
  • 6
  • 37
  • 64

2 Answers2

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.

Ermiya Eskandary
  • 15,323
  • 3
  • 31
  • 44
Andrey Borisov
  • 3,160
  • 18
  • 18
  • 9
    What happens if an exception is thrown during the processing of `onMessage()`? Will the message be redelivered if the `AUTO_ACKNOWLEDGE` is set? – Geek Jun 13 '14 at 14:39
  • 2
    @Geek - If exception is thrown in onReceive or in the JMSListener then the acknowledgment is not sent and will be redelivered – GameSalutes Dec 18 '15 at 15:14
  • 1
    If receiving messages synchronously with AUTO_ACKNOWLEDGE, the message is acknowledged when calling the consumer.receive method – Wecherowski Oct 17 '16 at 19:01
  • @GameSalutes - can you describe in detail how is the message redelivered and who delivers the message again and when does this redelivering happens? – Simple-Solution Aug 16 '18 at 19:49
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

Premraj
  • 72,055
  • 26
  • 237
  • 180