There are currently a few ways that can be used to mark that a message has been successfully processed and therefore it can be resolved (deleted from the SQS Queue).
The easiest method is to just make your method not throw an exception when it is executed. In this scenario the MessageProcessor sees that the processing was successful, by not throwing an exception, and it will resolve the message.
public class MyClass {
private final SomeService someService;
public MyClass(final SomeService someService) {
this.someService = someService;
}
public void myMethod(@Payload final String payload) {
someService.importantProcessing(payload);
}
}
When the method includes an Acknowledge parameter,
it is now the message listener's responsibility to manually resolve the message by calling
acknowledge.acknowledgeSuccessful()
. If this is never done then the message will not be deleted and will potentially be replaced into the queue
depending on the re-drive policy.
Note: that this returns a CompletableFuture
and will only guarantee to be completed if that future resolves successfully.
public class MyClass {
private final SomeService someService;
public MyClass(final SomeService someService) {
this.someService = someService;
}
public void myMethod(@Payload final String payload, final Acknowledge acknowledge) {
someService.importantProcessing(payload);
acknowledge.acknowledgeSuccessful(); // If there is an exception thrown from now on the message will still be a success
someService.unimportantProcessing(payload);
}
}
When the method does not include an Acknowledge
parameter, and it returns a CompletableFuture
, then the message will be successfully resolved when that future resolves.
Warning: If this CompletableFuture
is never completed, the MessageProcessor
will wait forever and ultimately you will have no more messages processing as all threads are waiting for the future to complete.
public class MyClass {
private final SomeService someService;
public MyClass(final SomeService someService) {
this.someService = someService;
}
public CompletableFuture<?> myMethod(@Payload final String payload) {
return someService.importantProcessingAsync(payload);
}
}