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: README.md
+96-1Lines changed: 96 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -162,6 +162,100 @@ await _eventBridgePublisher.PublishAsync(message, new EventBridgeOptions
162
162
});
163
163
```
164
164
165
+
## Batch publishing to SQS
166
+
167
+
The `ISQSPublisher` also supports sending messages in batches using the SQS [`SendMessageBatch`](https://docs.aws.amazon.com/AWSSimpleQueueService/latest/APIReference/API_SendMessageBatch.html) API. This can significantly increase throughput — from 300 messages per second with individual sends to up to 3,000 messages per second with batching.
168
+
169
+
The `SendBatchAsync` method accepts a collection of messages and automatically chunks them into groups of 10 (the SQS maximum per batch request).
170
+
171
+
### Simple batch — no per-message options
172
+
For standard (non-FIFO) queues where no per-message options are needed, you can pass the messages as a simple collection:
When each message in the batch needs its own options (e.g., different `MessageGroupId` values for FIFO queues), use `SQSBatchEntry<T>` to pair each message with its own `SQSMessageOptions`:
`SendBatchAsync` returns a `SQSSendBatchResponse` that contains:
221
+
*`Successful` — a list of `SQSSendBatchResponseEntry` with the `Id` and `MessageId` (SQS-assigned ID) for each successfully sent message.
222
+
*`Failed` — a list of `SQSSendBatchResponseFailedEntry` with `Id`, `Code`, `Message`, and `SenderFault` for each message that failed.
223
+
224
+
The `Id` on each response entry corresponds to the `MessageEnvelope.Id` that the framework generated for that message, so you can correlate successes and failures back to your original inputs.
Console.WriteLine($"Sent message {success.Id} with SQS MessageId: {success.MessageId}");
232
+
}
233
+
234
+
foreach (varfailureinresponse.Failed)
235
+
{
236
+
Console.WriteLine($"Failed to send {failure.Id}: [{failure.Code}] {failure.Message}");
237
+
}
238
+
```
239
+
240
+
> **Note:** Messages are automatically chunked into groups of 10. If you send 25 messages, the framework will make 3 SQS `SendMessageBatch` API calls (10 + 10 + 5) and aggregate the results into a single `SQSSendBatchResponse`.
241
+
242
+
### Error handling and partial results
243
+
If an `AmazonSQSException` is thrown during a batch send (e.g., on the second chunk after the first chunk already succeeded), the framework throws a `FailedToPublishBatchException`. This exception includes a `PartialResponse` property containing any results from chunks that were sent before the failure occurred, so you can determine what was already published and avoid duplicate retries.
To consume messages, implement a message handler using the `IMessageHandler` interface for each message type you wish to process. The mapping between message types and message handlers is configured in the project startup.
@@ -438,7 +532,8 @@ To use the AWS Message Processing Framework for .NET to publish a message to an
/// This interface allows sending messages from application code to Amazon SQS.
10
10
/// It exposes the <see cref="SendAsync{T}(T, SQSOptions?, CancellationToken)"/> method which takes in a user-defined message, and <see cref="SQSOptions"/> to set additional parameters while sending messages to SQS.
11
+
/// It also exposes <see cref="SendBatchAsync{T}(IEnumerable{T}, CancellationToken)"/> and
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
2
+
// SPDX-License-Identifier: Apache-2.0
3
+
4
+
usingAmazon.SQS.Model;
5
+
6
+
namespaceAWS.Messaging.Publishers.SQS;
7
+
8
+
/// <summary>
9
+
/// Contains per-message properties that can be set when sending individual messages
10
+
/// within a batch to an SQS queue.
11
+
/// <para>
12
+
/// This class is used on <see cref="SQSBatchEntry{T}.Options"/> to specify message-level
13
+
/// settings such as <see cref="MessageGroupId"/> and <see cref="MessageDeduplicationId"/>.
14
+
/// </para>
15
+
/// </summary>
16
+
publicclassSQSMessageOptions
17
+
{
18
+
/// <summary>
19
+
/// The length of time, in seconds, for which to delay a specific message.
20
+
/// Its valid values are between 0 to 900.
21
+
/// Messages with a positive DelaySeconds value become available for processing after the delay period is finished.
22
+
/// If you don't specify a value, the default value for the queue applies.
23
+
/// When you set FifoQueue, you can't set DelaySeconds per message. You can set this parameter only on a queue level.
24
+
/// </summary>
25
+
publicint?DelaySeconds{get;set;}
26
+
27
+
/// <summary>
28
+
/// Each message attribute consists of a Name, Type, and Value.
29
+
/// For more information, see <see href="https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-message-metadata.html#sqs-message-attributes">the Amazon SQS developer guide.</see>
/// This parameter applies only to FIFO(first-in-first-out) queues and is used for deduplication of sent messages.
35
+
/// If a message with a particular MessageDeduplicationId is sent successfully, any messages sent with the same
36
+
/// MessageDeduplicationId are accepted successfully but aren't delivered during the 5-minute deduplication interval.
37
+
/// For more information, see <see href="https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/FIFO-queues-exactly-once-processing.html">Exactly-once processing</see>
38
+
/// in the Amazon SQS Developer Guide.
39
+
/// <para>
40
+
/// Each message in a batch should have a unique MessageDeduplicationId. Setting the same deduplication ID
41
+
/// across multiple messages in a batch would incorrectly indicate they are duplicates of each other.
42
+
/// </para>
43
+
/// </summary>
44
+
publicstring?MessageDeduplicationId{get;set;}
45
+
46
+
/// <summary>
47
+
/// This parameter applies only to FIFO(first-in-first-out) queues and specifies that a message belongs to a specific message group.
48
+
/// Messages that belong to the same message group are processed in a FIFO manner
49
+
/// (however, messages in different message groups might be processed out of order).
50
+
/// To interleave multiple ordered streams within a single queue, use MessageGroupId values
51
+
/// (for example, session data for multiple users).
0 commit comments