|
| 1 | +/* |
| 2 | + * Copyright 2013-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.awspring.cloud.sqs.operations; |
| 17 | + |
| 18 | +import java.util.concurrent.CompletableFuture; |
| 19 | +import java.util.function.Consumer; |
| 20 | +import org.springframework.util.Assert; |
| 21 | +import software.amazon.awssdk.services.sqs.SqsAsyncClient; |
| 22 | +import software.amazon.awssdk.services.sqs.batchmanager.SqsAsyncBatchManager; |
| 23 | +import software.amazon.awssdk.services.sqs.model.*; |
| 24 | + |
| 25 | +/** |
| 26 | + * An {@link SqsAsyncClient} adapter that provides automatic batching capabilities using AWS SDK's |
| 27 | + * {@link SqsAsyncBatchManager}. |
| 28 | + * |
| 29 | + * <p> |
| 30 | + * This adapter automatically batches SQS operations to improve performance and reduce costs by combining multiple |
| 31 | + * requests into fewer AWS API calls. All standard SQS operations are supported: send message, receive message, delete |
| 32 | + * message, and change message visibility. |
| 33 | + * |
| 34 | + * <p> |
| 35 | + * <strong>Important - Asynchronous Behavior:</strong> This adapter processes requests asynchronously through |
| 36 | + * batching. The returned {@link CompletableFuture} reflects the batching operation, |
| 37 | + * not the final transmission to AWS SQS. This can lead to false positives where the operation appears successful locally but fails during actual transmission. |
| 38 | + * The actual transmission happens in a background thread, up to the configured {@code sendRequestFrequency} after enqueuing. |
| 39 | + * Applications must: |
| 40 | + * <ul> |
| 41 | + * <li>Handle the returned {@link CompletableFuture} to detect transmission errors. |
| 42 | + * Calling {@code .join()} will block until the batch is sent (up to {@code sendRequestFrequency}), |
| 43 | + * while {@code .exceptionally()} or {@code .handle()} are required for non-blocking error handling.</li> |
| 44 | + * <li>Implement appropriate error handling, monitoring, and retry mechanisms for critical operations.</li> |
| 45 | + * </ul> |
| 46 | + * |
| 47 | + * <p> |
| 48 | + * <strong>Batch Optimization:</strong> The underlying {@code SqsAsyncBatchManager} from the AWS SDK bypasses batching |
| 49 | + * for {@code receiveMessage} calls that include per-request configurations for certain parameters. To ensure batching |
| 50 | + * is not bypassed, it is recommended to configure these settings globally on the {@code SqsAsyncBatchManager} builder |
| 51 | + * instead of on each {@code ReceiveMessageRequest}. The parameters that trigger this bypass are: |
| 52 | + * <ul> |
| 53 | + * <li>{@code messageAttributeNames}</li> |
| 54 | + * <li>{@code messageSystemAttributeNames}</li> |
| 55 | + * <li>{@code messageSystemAttributeNamesWithStrings}</li> |
| 56 | + * <li>{@code overrideConfiguration}</li> |
| 57 | + * </ul> |
| 58 | + * <p> |
| 59 | + * By configuring these globally on the manager, you ensure consistent batching performance. If you require per-request |
| 60 | + * attribute configurations, using the standard {@code SqsAsyncClient} without this adapter may be more appropriate. |
| 61 | + * @author Heechul Kang |
| 62 | + * @since 4.0.0 |
| 63 | + * @see SqsAsyncBatchManager |
| 64 | + * @see SqsAsyncClient |
| 65 | + */ |
| 66 | +public class BatchingSqsClientAdapter implements SqsAsyncClient { |
| 67 | + private final SqsAsyncBatchManager batchManager; |
| 68 | + |
| 69 | + /** |
| 70 | + * Creates a new {@code BatchingSqsClientAdapter} with the specified batch manager. |
| 71 | + * |
| 72 | + * @param batchManager the {@link SqsAsyncBatchManager} to use for batching operations |
| 73 | + * @throws IllegalArgumentException if batchManager is null |
| 74 | + */ |
| 75 | + public BatchingSqsClientAdapter(SqsAsyncBatchManager batchManager) { |
| 76 | + Assert.notNull(batchManager, "batchManager cannot be null"); |
| 77 | + this.batchManager = batchManager; |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public String serviceName() { |
| 82 | + return SqsAsyncClient.SERVICE_NAME; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Closes the underlying batch manager and releases associated resources. |
| 87 | + * |
| 88 | + * <p> |
| 89 | + * This method should be called when the adapter is no longer needed to ensure proper cleanup of threads and |
| 90 | + * connections. |
| 91 | + */ |
| 92 | + @Override |
| 93 | + public void close() { |
| 94 | + batchManager.close(); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Sends a message to the specified SQS queue using automatic batching. |
| 99 | + * |
| 100 | + * <p> |
| 101 | + * <strong>Important:</strong> This method returns immediately, but the actual sending is performed asynchronously. |
| 102 | + * Handle the returned {@link CompletableFuture} to detect transmission errors. |
| 103 | + * |
| 104 | + * @param sendMessageRequest the request containing queue URL and message details |
| 105 | + * @return a {@link CompletableFuture} that completes with the send result |
| 106 | + */ |
| 107 | + @Override |
| 108 | + public CompletableFuture<SendMessageResponse> sendMessage(SendMessageRequest sendMessageRequest) { |
| 109 | + return batchManager.sendMessage(sendMessageRequest); |
| 110 | + } |
| 111 | + |
| 112 | + /** |
| 113 | + * Sends a message to the specified SQS queue using automatic batching. |
| 114 | + * |
| 115 | + * <p> |
| 116 | + * <strong>Important:</strong> This method returns immediately, but the actual sending is performed asynchronously. |
| 117 | + * Handle the returned {@link CompletableFuture} to detect transmission errors. |
| 118 | + * |
| 119 | + * @param sendMessageRequest a consumer to configure the send message request |
| 120 | + * @return a {@link CompletableFuture} that completes with the send result |
| 121 | + */ |
| 122 | + @Override |
| 123 | + public CompletableFuture<SendMessageResponse> sendMessage(Consumer<SendMessageRequest.Builder> sendMessageRequest) { |
| 124 | + return batchManager.sendMessage(sendMessageRequest); |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Receives messages from the specified SQS queue using automatic batching. |
| 129 | + * |
| 130 | + * <p> |
| 131 | + * The batching manager may combine multiple receive requests to optimize AWS API usage. |
| 132 | + * |
| 133 | + * @param receiveMessageRequest the request containing queue URL and receive options |
| 134 | + * @return a {@link CompletableFuture} that completes with the received messages |
| 135 | + */ |
| 136 | + @Override |
| 137 | + public CompletableFuture<ReceiveMessageResponse> receiveMessage(ReceiveMessageRequest receiveMessageRequest) { |
| 138 | + return batchManager.receiveMessage(receiveMessageRequest); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Receives messages from the specified SQS queue using automatic batching. |
| 143 | + * |
| 144 | + * <p> |
| 145 | + * The batching manager may combine multiple receive requests to optimize AWS API usage. |
| 146 | + * |
| 147 | + * @param receiveMessageRequest a consumer to configure the receive message request |
| 148 | + * @return a {@link CompletableFuture} that completes with the received messages |
| 149 | + */ |
| 150 | + @Override |
| 151 | + public CompletableFuture<ReceiveMessageResponse> receiveMessage( |
| 152 | + Consumer<ReceiveMessageRequest.Builder> receiveMessageRequest) { |
| 153 | + return batchManager.receiveMessage(receiveMessageRequest); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * Deletes a message from the specified SQS queue using automatic batching. |
| 158 | + * |
| 159 | + * <p> |
| 160 | + * <strong>Important:</strong> The actual deletion may be delayed due to batching. Handle the returned |
| 161 | + * {@link CompletableFuture} to confirm successful deletion. |
| 162 | + * |
| 163 | + * @param deleteMessageRequest the request containing queue URL and receipt handle |
| 164 | + * @return a {@link CompletableFuture} that completes with the deletion result |
| 165 | + */ |
| 166 | + @Override |
| 167 | + public CompletableFuture<DeleteMessageResponse> deleteMessage(DeleteMessageRequest deleteMessageRequest) { |
| 168 | + return batchManager.deleteMessage(deleteMessageRequest); |
| 169 | + } |
| 170 | + |
| 171 | + /** |
| 172 | + * Deletes a message from the specified SQS queue using automatic batching. |
| 173 | + * |
| 174 | + * <p> |
| 175 | + * <strong>Important:</strong> The actual deletion may be delayed due to batching. Handle the returned |
| 176 | + * {@link CompletableFuture} to confirm successful deletion. |
| 177 | + * |
| 178 | + * @param deleteMessageRequest a consumer to configure the delete message request |
| 179 | + * @return a {@link CompletableFuture} that completes with the deletion result |
| 180 | + */ |
| 181 | + @Override |
| 182 | + public CompletableFuture<DeleteMessageResponse> deleteMessage( |
| 183 | + Consumer<DeleteMessageRequest.Builder> deleteMessageRequest) { |
| 184 | + return batchManager.deleteMessage(deleteMessageRequest); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * Changes the visibility timeout of a message in the specified SQS queue using automatic batching. |
| 189 | + * |
| 190 | + * <p> |
| 191 | + * The batching manager may combine multiple visibility change requests to optimize AWS API usage. |
| 192 | + * |
| 193 | + * @param changeMessageVisibilityRequest the request containing queue URL, receipt handle, and new timeout |
| 194 | + * @return a {@link CompletableFuture} that completes with the visibility change result |
| 195 | + */ |
| 196 | + @Override |
| 197 | + public CompletableFuture<ChangeMessageVisibilityResponse> changeMessageVisibility( |
| 198 | + ChangeMessageVisibilityRequest changeMessageVisibilityRequest) { |
| 199 | + return batchManager.changeMessageVisibility(changeMessageVisibilityRequest); |
| 200 | + } |
| 201 | + |
| 202 | + /** |
| 203 | + * Changes the visibility timeout of a message in the specified SQS queue using automatic batching. |
| 204 | + * |
| 205 | + * <p> |
| 206 | + * The batching manager may combine multiple visibility change requests to optimize AWS API usage. |
| 207 | + * |
| 208 | + * @param changeMessageVisibilityRequest a consumer to configure the change visibility request |
| 209 | + * @return a {@link CompletableFuture} that completes with the visibility change result |
| 210 | + */ |
| 211 | + @Override |
| 212 | + public CompletableFuture<ChangeMessageVisibilityResponse> changeMessageVisibility( |
| 213 | + Consumer<ChangeMessageVisibilityRequest.Builder> changeMessageVisibilityRequest) { |
| 214 | + return batchManager.changeMessageVisibility(changeMessageVisibilityRequest); |
| 215 | + } |
| 216 | +} |
0 commit comments