aws / aws-sdk-java-v2

The official AWS SDK for Java - Version 2
Apache License 2.0
2.2k stars 853 forks source link

SQS Automatic Request Batching #5580

Closed joviegas closed 2 months ago

joviegas commented 2 months ago

Motivation and Context

Creating a Batch Manager

  1. Default Configuration via SqsAsyncClient: Users can easily obtain a SqsAsyncBatchManager with default configurations via SqsAsyncClient. This is the simplest approach, requiring minimal setup:
SqsAsyncClient asyncClient = SqsAsyncClient.create();
SqsAsyncBatchManager sqsAsyncBatchManager = asyncClient.batchManager();
  1. Custom Configuration via SqsAsyncBatchManager.Builder:
    batchManager = SqsAsyncBatchManager.builder()
                                   .client(client)
                                   .scheduledExecutor(Executors.newScheduledThreadPool(5))
                                   .overrideConfiguration(b -> b
                                       .maxBatchSize(10)
                                       .sendRequestFrequency(Duration.ofMillis(200))
                                       .receiveMessageMinWaitDuration(Duration.ofSeconds(10))
                                       .receiveMessageVisibilityTimeout(Duration.ofSeconds(20))
                                       .receiveMessageAttributeNames(Collections.singletonList("*"))
                                       .receiveMessageSystemAttributeNames(Collections.singletonList(MessageSystemAttributeName.ALL)))
                                   .build();

Method Usage Scenarios

Sending Messages:

CompletableFuture<SendMessageResponse> futureOne = sqsAsyncBatchManager.sendMessage(r -> r.messageBody("One").queueUrl("queue"));
SendMessageResponse messageOne = futureOne.join();

Receiving Messages:

With default settings:

CompletableFuture<ReceiveMessageResponse> responseFuture = batchManager.receiveMessage(
    r -> r.queueUrl(defaultQueueUrl));

With custom timeout:

CompletableFuture<ReceiveMessageResponse> response = batchManager.receiveMessage(
    r -> r.queueUrl(defaultQueueUrl)
          .waitTimeSeconds(5)
          .visibilityTimeout(20));

With limited number of messages configured on Request:

CompletableFuture<ReceiveMessageResponse> response = batchManager.receiveMessage(
    r -> r.queueUrl(defaultQueueUrl)
          .maxNumberOfMessages(3));

Modifications

Please refer to the PR in the commit

#

Types of changes

Checklist

License

sonarcloud[bot] commented 2 months ago

Quality Gate Failed Quality Gate failed

Failed conditions
B Reliability Rating on New Code (required ≥ A)

See analysis details on SonarCloud

Catch issues before they fail your Quality Gate with our IDE extension SonarLint

jhchee commented 1 month ago

Hi @joviegas thanks for the great work! I believe there's a need to update the documentation at [here] (https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-client-side-buffering-request-batching.html#using-buffered-async-client) as well.

joviegas commented 4 weeks ago

Hi @joviegas thanks for the great work! I believe there's a need to update the documentation at [here] (https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-client-side-buffering-request-batching.html#using-buffered-async-client) as well.

Hi @jhchee Thanks for reaching out I am working on releasing a new blog post , might take some time due to other priorities but will try my best .