-
-
Notifications
You must be signed in to change notification settings - Fork 353
Add automatic SQS request batching support component: sqs #1438
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+900
−0
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f42e019
Refactor: Focus on BatchingSqsClientAdapter;
khc41 9f043a3
Remove SQS batch configuration support and simplify `ReceiveMessageRe…
khc41 5e3ea66
Update documentation to include manual configuration instructions for…
khc41 1fc5234
Enhance BatchingSqsClientAdapter: improve documentation on batch opti…
khc41 b09a917
Update documentation for automatic request batching: clarify usage wi…
khc41 ec8d6cc
Update BatchingSqsClientAdapter documentation: clarify asynchronous b…
khc41 ea99930
Refactor BatchingSqsClientAdapterIntegrationTests: remove unnecessary…
khc41 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
214 changes: 214 additions & 0 deletions
214
...loud-aws-sqs/src/main/java/io/awspring/cloud/sqs/operations/BatchingSqsClientAdapter.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,214 @@ | ||
| /* | ||
| * Copyright 2013-2024 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package io.awspring.cloud.sqs.operations; | ||
|
|
||
| import java.util.concurrent.CompletableFuture; | ||
| import java.util.function.Consumer; | ||
| import org.springframework.util.Assert; | ||
| import software.amazon.awssdk.services.sqs.SqsAsyncClient; | ||
| import software.amazon.awssdk.services.sqs.batchmanager.SqsAsyncBatchManager; | ||
| import software.amazon.awssdk.services.sqs.model.*; | ||
|
|
||
| /** | ||
| * An {@link SqsAsyncClient} adapter that provides automatic batching capabilities using AWS SDK's | ||
| * {@link SqsAsyncBatchManager}. | ||
| * | ||
| * <p> | ||
| * 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. | ||
| * | ||
| * <p> | ||
| * <strong>Important - False Positives Warning:</strong> This adapter processes requests asynchronously through | ||
| * batching. Method calls may return successfully before the actual request is sent to AWS SQS. This can result in false | ||
| * positives where the operation appears to succeed locally but fails during the actual transmission to AWS. | ||
| * Applications should: | ||
| * <ul> | ||
| * <li>Always handle the returned {@link CompletableFuture} to detect actual transmission errors</li> | ||
| * <li>Implement appropriate error handling and monitoring</li> | ||
| * <li>Consider retry mechanisms for critical operations</li> | ||
| * </ul> | ||
| * | ||
| * <p> | ||
| * <strong>Batch Optimization:</strong> The underlying {@code SqsAsyncBatchManager} from the AWS SDK bypasses batching | ||
| * for {@code 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 {@code SqsAsyncBatchManager} builder | ||
| * instead of on each {@code ReceiveMessageRequest}. The parameters that trigger this bypass are: | ||
| * <ul> | ||
| * <li>{@code messageAttributeNames}</li> | ||
| * <li>{@code messageSystemAttributeNames}</li> | ||
| * <li>{@code messageSystemAttributeNamesWithStrings}</li> | ||
| * <li>{@code overrideConfiguration}</li> | ||
| * </ul> | ||
| * <p> | ||
| * By configuring these globally on the manager, you ensure consistent batching performance. If you require per-request | ||
| * attribute configurations, using the standard {@code SqsAsyncClient} without this adapter may be more appropriate. | ||
| * @author Heechul Kang | ||
| * @since 3.2 | ||
|
||
| * @see SqsAsyncBatchManager | ||
| * @see SqsAsyncClient | ||
| */ | ||
| public class BatchingSqsClientAdapter implements SqsAsyncClient { | ||
| private final SqsAsyncBatchManager batchManager; | ||
|
|
||
| /** | ||
| * Creates a new {@code BatchingSqsClientAdapter} with the specified batch manager. | ||
| * | ||
| * @param batchManager the {@link SqsAsyncBatchManager} to use for batching operations | ||
| * @throws IllegalArgumentException if batchManager is null | ||
| */ | ||
| public BatchingSqsClientAdapter(SqsAsyncBatchManager batchManager) { | ||
| Assert.notNull(batchManager, "batchManager cannot be null"); | ||
| this.batchManager = batchManager; | ||
| } | ||
|
|
||
| @Override | ||
| public String serviceName() { | ||
| return SqsAsyncClient.SERVICE_NAME; | ||
| } | ||
|
|
||
| /** | ||
| * Closes the underlying batch manager and releases associated resources. | ||
| * | ||
| * <p> | ||
| * This method should be called when the adapter is no longer needed to ensure proper cleanup of threads and | ||
| * connections. | ||
| */ | ||
| @Override | ||
| public void close() { | ||
| batchManager.close(); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a message to the specified SQS queue using automatic batching. | ||
| * | ||
| * <p> | ||
| * <strong>Important:</strong> This method returns immediately, but the actual sending is performed asynchronously. | ||
| * Handle the returned {@link CompletableFuture} to detect transmission errors. | ||
| * | ||
| * @param sendMessageRequest the request containing queue URL and message details | ||
| * @return a {@link CompletableFuture} that completes with the send result | ||
| */ | ||
| @Override | ||
| public CompletableFuture<SendMessageResponse> sendMessage(SendMessageRequest sendMessageRequest) { | ||
| return batchManager.sendMessage(sendMessageRequest); | ||
| } | ||
|
|
||
| /** | ||
| * Sends a message to the specified SQS queue using automatic batching. | ||
| * | ||
| * <p> | ||
| * <strong>Important:</strong> This method returns immediately, but the actual sending is performed asynchronously. | ||
| * Handle the returned {@link CompletableFuture} to detect transmission errors. | ||
| * | ||
| * @param sendMessageRequest a consumer to configure the send message request | ||
| * @return a {@link CompletableFuture} that completes with the send result | ||
| */ | ||
| @Override | ||
| public CompletableFuture<SendMessageResponse> sendMessage(Consumer<SendMessageRequest.Builder> sendMessageRequest) { | ||
| return batchManager.sendMessage(sendMessageRequest); | ||
| } | ||
|
|
||
| /** | ||
| * Receives messages from the specified SQS queue using automatic batching. | ||
| * | ||
| * <p> | ||
| * The batching manager may combine multiple receive requests to optimize AWS API usage. | ||
| * | ||
| * @param receiveMessageRequest the request containing queue URL and receive options | ||
| * @return a {@link CompletableFuture} that completes with the received messages | ||
| */ | ||
| @Override | ||
| public CompletableFuture<ReceiveMessageResponse> receiveMessage(ReceiveMessageRequest receiveMessageRequest) { | ||
| return batchManager.receiveMessage(receiveMessageRequest); | ||
| } | ||
|
|
||
| /** | ||
| * Receives messages from the specified SQS queue using automatic batching. | ||
| * | ||
| * <p> | ||
| * The batching manager may combine multiple receive requests to optimize AWS API usage. | ||
| * | ||
| * @param receiveMessageRequest a consumer to configure the receive message request | ||
| * @return a {@link CompletableFuture} that completes with the received messages | ||
| */ | ||
| @Override | ||
| public CompletableFuture<ReceiveMessageResponse> receiveMessage( | ||
| Consumer<ReceiveMessageRequest.Builder> receiveMessageRequest) { | ||
| return batchManager.receiveMessage(receiveMessageRequest); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a message from the specified SQS queue using automatic batching. | ||
| * | ||
| * <p> | ||
| * <strong>Important:</strong> The actual deletion may be delayed due to batching. Handle the returned | ||
| * {@link CompletableFuture} to confirm successful deletion. | ||
| * | ||
| * @param deleteMessageRequest the request containing queue URL and receipt handle | ||
| * @return a {@link CompletableFuture} that completes with the deletion result | ||
| */ | ||
| @Override | ||
| public CompletableFuture<DeleteMessageResponse> deleteMessage(DeleteMessageRequest deleteMessageRequest) { | ||
| return batchManager.deleteMessage(deleteMessageRequest); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes a message from the specified SQS queue using automatic batching. | ||
| * | ||
| * <p> | ||
| * <strong>Important:</strong> The actual deletion may be delayed due to batching. Handle the returned | ||
| * {@link CompletableFuture} to confirm successful deletion. | ||
| * | ||
| * @param deleteMessageRequest a consumer to configure the delete message request | ||
| * @return a {@link CompletableFuture} that completes with the deletion result | ||
| */ | ||
| @Override | ||
| public CompletableFuture<DeleteMessageResponse> deleteMessage( | ||
| Consumer<DeleteMessageRequest.Builder> deleteMessageRequest) { | ||
| return batchManager.deleteMessage(deleteMessageRequest); | ||
| } | ||
|
|
||
| /** | ||
| * Changes the visibility timeout of a message in the specified SQS queue using automatic batching. | ||
| * | ||
| * <p> | ||
| * The batching manager may combine multiple visibility change requests to optimize AWS API usage. | ||
| * | ||
| * @param changeMessageVisibilityRequest the request containing queue URL, receipt handle, and new timeout | ||
| * @return a {@link CompletableFuture} that completes with the visibility change result | ||
| */ | ||
| @Override | ||
| public CompletableFuture<ChangeMessageVisibilityResponse> changeMessageVisibility( | ||
| ChangeMessageVisibilityRequest changeMessageVisibilityRequest) { | ||
| return batchManager.changeMessageVisibility(changeMessageVisibilityRequest); | ||
| } | ||
|
|
||
| /** | ||
| * Changes the visibility timeout of a message in the specified SQS queue using automatic batching. | ||
| * | ||
| * <p> | ||
| * The batching manager may combine multiple visibility change requests to optimize AWS API usage. | ||
| * | ||
| * @param changeMessageVisibilityRequest a consumer to configure the change visibility request | ||
| * @return a {@link CompletableFuture} that completes with the visibility change result | ||
| */ | ||
| @Override | ||
| public CompletableFuture<ChangeMessageVisibilityResponse> changeMessageVisibility( | ||
| Consumer<ChangeMessageVisibilityRequest.Builder> changeMessageVisibilityRequest) { | ||
| return batchManager.changeMessageVisibility(changeMessageVisibilityRequest); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.