Class BatchingSqsClientAdapter
- All Implemented Interfaces:
AutoCloseable,software.amazon.awssdk.awscore.AwsClient,software.amazon.awssdk.core.SdkClient,software.amazon.awssdk.services.sqs.SqsAsyncClient,software.amazon.awssdk.utils.SdkAutoCloseable
SqsAsyncClient adapter that provides automatic batching capabilities using AWS SDK's
SqsAsyncBatchManager.
This adapter automatically batches SQS operations to improve performance and reduce costs by combining multiple requests into fewer AWS API calls. All standard SQS operations are supported: send message, receive message, delete message, and change message visibility.
Important - Asynchronous Behavior: This adapter processes requests asynchronously through batching.
The returned CompletableFuture reflects the batching operation, not the final transmission to AWS SQS. This
can lead to false positives where the operation appears successful locally but fails during actual transmission. The
actual transmission happens in a background thread, up to the configured sendRequestFrequency after
enqueuing. Applications must:
- Handle the returned
CompletableFutureto detect transmission errors. Calling.join()will block until the batch is sent (up tosendRequestFrequency), while.exceptionally()or.handle()are required for non-blocking error handling. - Implement appropriate error handling, monitoring, and retry mechanisms for critical operations.
Batch Optimization: The underlying SqsAsyncBatchManager from the AWS SDK bypasses batching
for receiveMessage calls that include per-request configurations for certain parameters. To ensure batching
is not bypassed, it is recommended to configure these settings globally on the SqsAsyncBatchManager builder
instead of on each ReceiveMessageRequest. The parameters that trigger this bypass are:
messageAttributeNamesmessageSystemAttributeNamesmessageSystemAttributeNamesWithStringsoverrideConfiguration
By configuring these globally on the manager, you ensure consistent batching performance. If you require per-request
attribute configurations, using the standard SqsAsyncClient without this adapter may be more appropriate.
- Since:
- 4.0.0
- Author:
- Heechul Kang
- See Also:
-
Field Summary
Fields inherited from interface software.amazon.awssdk.services.sqs.SqsAsyncClient
SERVICE_METADATA_ID, SERVICE_NAME -
Constructor Summary
ConstructorsConstructorDescriptionBatchingSqsClientAdapter(software.amazon.awssdk.services.sqs.batchmanager.SqsAsyncBatchManager batchManager) Creates a newBatchingSqsClientAdapterwith the specified batch manager. -
Method Summary
Modifier and TypeMethodDescriptionCompletableFuture<software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityResponse> changeMessageVisibility(Consumer<software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityRequest.Builder> changeMessageVisibilityRequest) Changes the visibility timeout of a message in the specified SQS queue using automatic batching.CompletableFuture<software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityResponse> changeMessageVisibility(software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityRequest changeMessageVisibilityRequest) Changes the visibility timeout of a message in the specified SQS queue using automatic batching.voidclose()Closes the underlying batch manager and releases associated resources.CompletableFuture<software.amazon.awssdk.services.sqs.model.DeleteMessageResponse> deleteMessage(Consumer<software.amazon.awssdk.services.sqs.model.DeleteMessageRequest.Builder> deleteMessageRequest) Deletes a message from the specified SQS queue using automatic batching.CompletableFuture<software.amazon.awssdk.services.sqs.model.DeleteMessageResponse> deleteMessage(software.amazon.awssdk.services.sqs.model.DeleteMessageRequest deleteMessageRequest) Deletes a message from the specified SQS queue using automatic batching.CompletableFuture<software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse> receiveMessage(Consumer<software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest.Builder> receiveMessageRequest) Receives messages from the specified SQS queue using automatic batching.CompletableFuture<software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse> receiveMessage(software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest receiveMessageRequest) Receives messages from the specified SQS queue using automatic batching.CompletableFuture<software.amazon.awssdk.services.sqs.model.SendMessageResponse> sendMessage(Consumer<software.amazon.awssdk.services.sqs.model.SendMessageRequest.Builder> sendMessageRequest) Sends a message to the specified SQS queue using automatic batching.CompletableFuture<software.amazon.awssdk.services.sqs.model.SendMessageResponse> sendMessage(software.amazon.awssdk.services.sqs.model.SendMessageRequest sendMessageRequest) Sends a message to the specified SQS queue using automatic batching.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface software.amazon.awssdk.services.sqs.SqsAsyncClient
addPermission, addPermission, batchManager, cancelMessageMoveTask, cancelMessageMoveTask, changeMessageVisibilityBatch, changeMessageVisibilityBatch, createQueue, createQueue, deleteMessageBatch, deleteMessageBatch, deleteQueue, deleteQueue, getQueueAttributes, getQueueAttributes, getQueueUrl, getQueueUrl, listDeadLetterSourceQueues, listDeadLetterSourceQueues, listDeadLetterSourceQueuesPaginator, listDeadLetterSourceQueuesPaginator, listMessageMoveTasks, listMessageMoveTasks, listQueues, listQueues, listQueues, listQueuesPaginator, listQueuesPaginator, listQueuesPaginator, listQueueTags, listQueueTags, purgeQueue, purgeQueue, removePermission, removePermission, sendMessageBatch, sendMessageBatch, serviceClientConfiguration, setQueueAttributes, setQueueAttributes, startMessageMoveTask, startMessageMoveTask, tagQueue, tagQueue, untagQueue, untagQueue
-
Constructor Details
-
BatchingSqsClientAdapter
public BatchingSqsClientAdapter(software.amazon.awssdk.services.sqs.batchmanager.SqsAsyncBatchManager batchManager) Creates a newBatchingSqsClientAdapterwith the specified batch manager.- Parameters:
batchManager- theSqsAsyncBatchManagerto use for batching operations- Throws:
IllegalArgumentException- if batchManager is null
-
-
Method Details
-
serviceName
- Specified by:
serviceNamein interfacesoftware.amazon.awssdk.core.SdkClient
-
close
public void close()Closes the underlying batch manager and releases associated resources.This method should be called when the adapter is no longer needed to ensure proper cleanup of threads and connections.
- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfacesoftware.amazon.awssdk.utils.SdkAutoCloseable
-
sendMessage
public CompletableFuture<software.amazon.awssdk.services.sqs.model.SendMessageResponse> sendMessage(software.amazon.awssdk.services.sqs.model.SendMessageRequest sendMessageRequest) Sends a message to the specified SQS queue using automatic batching.Important: This method returns immediately, but the actual sending is performed asynchronously. Handle the returned
CompletableFutureto detect transmission errors.- Specified by:
sendMessagein interfacesoftware.amazon.awssdk.services.sqs.SqsAsyncClient- Parameters:
sendMessageRequest- the request containing queue URL and message details- Returns:
- a
CompletableFuturethat completes with the send result
-
sendMessage
public CompletableFuture<software.amazon.awssdk.services.sqs.model.SendMessageResponse> sendMessage(Consumer<software.amazon.awssdk.services.sqs.model.SendMessageRequest.Builder> sendMessageRequest) Sends a message to the specified SQS queue using automatic batching.Important: This method returns immediately, but the actual sending is performed asynchronously. Handle the returned
CompletableFutureto detect transmission errors.- Specified by:
sendMessagein interfacesoftware.amazon.awssdk.services.sqs.SqsAsyncClient- Parameters:
sendMessageRequest- a consumer to configure the send message request- Returns:
- a
CompletableFuturethat completes with the send result
-
receiveMessage
public CompletableFuture<software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse> receiveMessage(software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest receiveMessageRequest) Receives messages from the specified SQS queue using automatic batching.The batching manager may combine multiple receive requests to optimize AWS API usage.
- Specified by:
receiveMessagein interfacesoftware.amazon.awssdk.services.sqs.SqsAsyncClient- Parameters:
receiveMessageRequest- the request containing queue URL and receive options- Returns:
- a
CompletableFuturethat completes with the received messages
-
receiveMessage
public CompletableFuture<software.amazon.awssdk.services.sqs.model.ReceiveMessageResponse> receiveMessage(Consumer<software.amazon.awssdk.services.sqs.model.ReceiveMessageRequest.Builder> receiveMessageRequest) Receives messages from the specified SQS queue using automatic batching.The batching manager may combine multiple receive requests to optimize AWS API usage.
- Specified by:
receiveMessagein interfacesoftware.amazon.awssdk.services.sqs.SqsAsyncClient- Parameters:
receiveMessageRequest- a consumer to configure the receive message request- Returns:
- a
CompletableFuturethat completes with the received messages
-
deleteMessage
public CompletableFuture<software.amazon.awssdk.services.sqs.model.DeleteMessageResponse> deleteMessage(software.amazon.awssdk.services.sqs.model.DeleteMessageRequest deleteMessageRequest) Deletes a message from the specified SQS queue using automatic batching.Important: The actual deletion may be delayed due to batching. Handle the returned
CompletableFutureto confirm successful deletion.- Specified by:
deleteMessagein interfacesoftware.amazon.awssdk.services.sqs.SqsAsyncClient- Parameters:
deleteMessageRequest- the request containing queue URL and receipt handle- Returns:
- a
CompletableFuturethat completes with the deletion result
-
deleteMessage
public CompletableFuture<software.amazon.awssdk.services.sqs.model.DeleteMessageResponse> deleteMessage(Consumer<software.amazon.awssdk.services.sqs.model.DeleteMessageRequest.Builder> deleteMessageRequest) Deletes a message from the specified SQS queue using automatic batching.Important: The actual deletion may be delayed due to batching. Handle the returned
CompletableFutureto confirm successful deletion.- Specified by:
deleteMessagein interfacesoftware.amazon.awssdk.services.sqs.SqsAsyncClient- Parameters:
deleteMessageRequest- a consumer to configure the delete message request- Returns:
- a
CompletableFuturethat completes with the deletion result
-
changeMessageVisibility
public CompletableFuture<software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityResponse> changeMessageVisibility(software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityRequest changeMessageVisibilityRequest) Changes the visibility timeout of a message in the specified SQS queue using automatic batching.The batching manager may combine multiple visibility change requests to optimize AWS API usage.
- Specified by:
changeMessageVisibilityin interfacesoftware.amazon.awssdk.services.sqs.SqsAsyncClient- Parameters:
changeMessageVisibilityRequest- the request containing queue URL, receipt handle, and new timeout- Returns:
- a
CompletableFuturethat completes with the visibility change result
-
changeMessageVisibility
public CompletableFuture<software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityResponse> changeMessageVisibility(Consumer<software.amazon.awssdk.services.sqs.model.ChangeMessageVisibilityRequest.Builder> changeMessageVisibilityRequest) Changes the visibility timeout of a message in the specified SQS queue using automatic batching.The batching manager may combine multiple visibility change requests to optimize AWS API usage.
- Specified by:
changeMessageVisibilityin interfacesoftware.amazon.awssdk.services.sqs.SqsAsyncClient- Parameters:
changeMessageVisibilityRequest- a consumer to configure the change visibility request- Returns:
- a
CompletableFuturethat completes with the visibility change result
-