Spring SqsTemplate Manual Acknowledgement: How To?

151 Views Asked by At

I'm using Spring Boot 3.2.2 and Spring Cloud AWS 3.1.0 and I need to get a message from AWS SQS queue and to manually acknowledge it after successfully processing the message. I can read the message but couldn't figure out how to manually send an ack.

I created a custom SqsTemplate with manual acknowledgement mode:

@Configuration
public class AwsServicesConfig {

 
  @Bean
  public SqsTemplate customSqsTemplate(SqsAsyncClient sqsAsyncClient) {
    return SqsTemplate.builder()
        .sqsAsyncClient(sqsAsyncClient)
        .configure(options -> options
            // NOTE: manual ACK mode
            .acknowledgementMode(TemplateAcknowledgementMode.MANUAL)
        ).build();
  }
}

The logic in the @Component is something like this:

  public List<String> processMessages() {
    var processedMessages = new ArrayList<String>();

    var messages = customSqsTemplate.receiveMany(from -> from.queue("my-queue")
        .maxNumberOfMessages(10),
        String.class
    );

    while (!messages.isEmpty()) {
      messages.forEach(message -> {
         
            doProcess(message);
            processedMessages.add(message);

           // HERE I want to send an ack to the source queue

       });

      messages = sqsTemplate.receiveMany(from -> 
          from.queue(mcSendProperties.getSqs().getDlqUrl())
              .maxNumberOfMessages(MAX_NUMBER_OF_MESSAGES),
          String.class
     );
    }
    return processedMessages;
  }

I don't find any method on SqsTemplate object to send an ack. I checked the tests in GitHub repo of the project, but it only tells that the ack shall not be sent automatically: https://github.com/awspring/spring-cloud-aws/blob/main/spring-cloud-aws-sqs/src/test/java/io/awspring/cloud/sqs/operations/SqsTemplateTests.java#L770

0

There are 0 best solutions below