You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/main/asciidoc/sqs.adoc
+51-88Lines changed: 51 additions & 88 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -812,124 +812,87 @@ Spring Cloud AWS supports automatic message batching using AWS SDK's `SqsAsyncBa
812
812
813
813
IMPORTANT: This is different from the <<Batch Processing,Batch Processing>> feature described above. Batch Processing refers to processing multiple messages in a single listener method call, while Automatic Batching refers to the AWS SDK automatically combining multiple SQS API calls into batched requests for efficiency.
814
814
815
-
===== Enabling Automatic Batching
815
+
====Automatic Request Batching with SqsAsyncBatchManager
816
816
817
-
To enable automatic batching, set the following property:
817
+
Spring Cloud AWS allows you to leverage the AWS SDK's `SqsAsyncBatchManager` for automatic request batching. This feature can significantly improve performance and reduce costs by transparently combining multiple SQS API calls (`sendMessage`, `deleteMessage`, etc.) into single batch requests.
818
818
819
-
[source,properties]
820
-
----
821
-
spring.cloud.aws.sqs.batch.enabled=true
822
-
----
823
-
824
-
When enabled, Spring Cloud AWS will automatically wrap the `SqsAsyncClient` with a `BatchingSqsClientAdapter` that uses `SqsAsyncBatchManager` internally.
825
-
826
-
===== Configuration Properties
827
-
828
-
The following properties can be used to configure the batching behavior:
829
-
830
-
[source,properties]
831
-
----
832
-
# Enable automatic batching (default: false)
833
-
spring.cloud.aws.sqs.batch.enabled=true
834
-
835
-
# Maximum number of messages in a batch (default: AWS SDK default, max: 10)
IMPORTANT: This is different from the <<Batch Processing,Batch Processing>> feature for `@SqsListener`. Listener batch processing deals with handling multiple messages within a single listener invocation, whereas automatic request batching optimizes the underlying API calls to AWS.
837
820
838
-
# Frequency at which batched requests are sent (default: AWS SDK default)
Since automatic batching is a powerful feature with specific trade-offs, Spring Cloud AWS does not auto-configure it. You can enable it by creating your own `SqsAsyncClient` bean using the provided `BatchingSqsClientAdapter`.
843
824
844
-
# Wait time for receiveMessage requests (default: AWS SDK default)
845
-
spring.cloud.aws.sqs.batch.wait-time-seconds=PT5S
825
+
###### 1. Defining the Batching Client Bean
826
+
The following example shows how to define a bean named `batchingSqsAsyncClient`. Notice the use of `@Qualifier("sqsAsyncClient")` in the method parameter. This is crucial to explicitly inject the standard, auto-configured `SqsAsyncClient` and avoid ambiguity.
846
827
847
-
# System attributes to request for receiveMessage calls
WARNING: When using automatic batching, operations are processed asynchronously by the AWS SDK. This means that a method call may return successfully, but the actual request to AWS SQS might fail later during the batching process. This can result in **false positives** where operations appear to succeed locally but fail during transmission.
857
-
858
-
Applications should:
859
-
860
-
- **Always handle the returned `CompletableFuture`** to detect actual transmission errors
861
-
- **Implement appropriate error handling and monitoring** to detect delayed failures
862
-
- **Consider retry mechanisms** for critical operations
863
-
864
-
IMPORTANT: **Batch Manager Bypass**: The AWS SDK batch manager will bypass batching and send regular asynchronous requests when `receiveMessage` is called with any of the following parameters:
833
+
// Define a constant for the bean name to avoid typos
834
+
public static final String BATCHING_SQS_ASYNC_CLIENT = "batchingSqsAsyncClient";
865
835
866
-
- `messageAttributeNames`
867
-
- `messageSystemAttributeNames`
868
-
- `messageSystemAttributeNamesWithStrings` (not used in Spring Cloud AWS `ReceiveMessageRequest`)
869
-
- `overrideConfiguration` (not used in Spring Cloud AWS `ReceiveMessageRequest`)
836
+
@Bean(name = BATCHING_SQS_ASYNC_CLIENT)
837
+
public SqsAsyncClient batchingSqsAsyncClient(
838
+
// Inject the standard, auto-configured client to wrap it
**Note**: When using Spring Cloud AWS's automatic batching feature, `SqsTemplate` automatically excludes `messageAttributeNames` and `messageSystemAttributeNames` from individual `receiveMessage` requests to maintain batching efficiency. These attributes should be configured globally in the batch configuration instead:
return new BatchingSqsClientAdapter(batchManager);
849
+
}
850
+
}
880
851
----
881
852
882
-
If you need to use different attribute configurations per request, consider disabling automatic batching and using the standard `SqsAsyncClient` instead.
883
-
884
-
Example of proper error handling:
853
+
###### 2. Using the Batching Client
854
+
Now, use `@Qualifier` to inject your named bean. The most common use case is configuring a dedicated `SqsTemplate`.
885
855
886
856
[source,java]
887
857
----
888
858
@Service
889
-
public class MessageService {
890
-
891
-
private final SqsTemplate sqsTemplate;
859
+
public class MyBatchingMessageService {
892
860
893
-
public MessageService(SqsTemplate sqsTemplate) {
894
-
this.sqsTemplate = sqsTemplate;
895
-
}
861
+
private final SqsTemplate batchingSqsTemplate;
896
862
897
-
public void sendMessage(String queueName, String message) {
WARNING: **Asynchronous Operations and False Positives**. The batching client processes operations asynchronously. A call to `sqsTemplate.sendAsync(...)` might return a `CompletableFuture` that completes successfully before the message is actually sent to AWS. The actual transmission happens later in a background thread. This can lead to **false positives**. Always attach error handling to the `CompletableFuture` to detect and handle real transmission failures.
917
876
918
-
- **Reduced API calls**: Multiple operations are combined into single API calls
919
-
- **Lower costs**: Fewer API calls result in reduced AWS charges
920
-
- **Improved throughput**: Batching reduces network overhead and latency
921
-
- **Better resource utilization**: More efficient use of network and AWS resources
// This is where you handle the actual transmission error
884
+
log.error("Failed to send message to queue {}: {}", queueName, ex.getMessage());
885
+
} else {
886
+
log.info("Message acknowledged for batch sending with ID: {}", result.messageId());
887
+
}
888
+
});
889
+
----
924
890
925
-
Automatic batching is compatiblewith:
891
+
WARNING: **Not Recommended for `@SqsListener`**. While technically compatible, using this batching client with `@SqsListener` for receiving messages is **not recommended**. The `@SqsListener` infrastructure already performs efficient batch receiving and has a complex acknowledgment lifecycle. Adding another layer of asynchronous batching provides limited performance benefits while significantly increasing complexity. For listeners, it's best to rely on the default `SqsAsyncClient`.
926
892
927
-
- `SqsTemplate` for sending and receiving messages
928
-
- `@SqsListener` methods for message processing
929
-
- Both standard and FIFO queues
930
-
- All message conversion and error handling features
893
+
IMPORTANT: **Bean Injection Safety**. By using a named bean and `@Qualifier` as shown in the configuration examples, you ensure the batching client is only used where intended. This prevents it from accidentally being injected into `@SqsListener` infrastructure, which should use the default `SqsAsyncClient`.
931
894
932
-
The batching is transparent to application code - existing `SqsTemplate` and `@SqsListener` code continues to work without changes.
895
+
IMPORTANT: **AWS SDK Batching Bypass**. The `SqsAsyncBatchManager` will bypass batching for `receiveMessage` calls if certain parameters like `messageAttributeNames` are set on a per-request basis. To ensure batching works effectively, these should be configured globally on the `SqsAsyncBatchManager` builder, not on individual `receiveMessage` calls. See the `BatchingSqsClientAdapter` Javadoc for more details.
0 commit comments