How do I unack the message with Stomp

 FAQ > Using Apache ActiveMQ Classic > How do I unack the message with Stomp

There is no explicit “unack” command in Stomp. Once the client receives the message it cannot be marked as “unconsumed” and sent to another subscriber (or redelivered to the same subscriber again). It’s up to your application (or Stomp client) to handle failed processing of received messages and implement “message redelivery”.

Stomp transactions are often mistakenly considered to be a solution for this use case. But that’s not the case, since transactions are only related to sending messages and acknowledgments. If you start a transaction, send a message ack in a transaction and finally abort it, the message will not be redelivered again. It just means that broker will not send any more messages to the client if the prefetch limit is reached.

Take a look at the following example:

StompConnection connection = new StompConnection();"localhost", 61613);
connection.connect("system", "manager");
connection.send("/queue/test", "message 1");
connection.send("/queue/test", "message 2");
connection.send("/queue/test", "message 3");
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("activemq.prefetchSize", "1");
connection.subscribe("/queue/test", "client", headers);
StompFrame frame = connection.receive();
connection.ack(frame, "tx1");
connection.ack(frame, "tx2");        //sending the ack again
frame = connection.receive();
connection.ack(frame, "tx2");
frame = connection.receive();
connection.ack(frame, "tx3");

This simple application will print

message 1
message 2
message 3

Since the transaction tx1 has been aborted, we needed to acknowledge that message again in tx2 in order to be able to receive the next message (since the prefetch size used is 1).

Also take a look at these pages for more info:

Apache, ActiveMQ, Apache ActiveMQ, the Apache feather logo, and the Apache ActiveMQ project logo are trademarks of The Apache Software Foundation. Copyright © 2024, The Apache Software Foundation. Licensed under Apache License 2.0.