Developer Guide

Sending an Amazon SQS Message Batch

You can use the AWS SDK for .NET to send batch messages to an Amazon SQS queue. The SendMessageBatch method delivers up to 10 messages to the specified queue. This is a batch version of SendMessage.

For a FIFO queue, multiple messages within a single batch are enqueued in the order they are sent.

For more information about sending batch messages, see SendMessageBatch in the Amazon SQS API Reference.

To send batch messages to an Amazon SQS queue

  1. Create an AmazonSQSClient instance and initialize a SendMessageBatchRequest object. Specify the queue name and the message you want to send, as follows.

    AmazonSQSClient client = new AmazonSQSClient(); var sendMessageBatchRequest = new SendMessageBatchRequest { Entries = new List<SendMessageBatchRequestEntry> { new SendMessageBatchRequestEntry("message1", "FirstMessageContent"), new SendMessageBatchRequestEntry("message2", "SecondMessageContent"), new SendMessageBatchRequestEntry("message3", "ThirdMessageContent") }, QueueUrl = "SQS_QUEUE_URL" };

    For more information about your queue URL, see Constructing Amazon SQS Queue URLs.

    Each queue message must be composed of Unicode characters only, and can be up to 64 KB in size. For more information about queue messages, see SendMessage in the Amazon SQS API Reference.

  2. After you create the request, pass it as a parameter to the SendMessageBatch method. The method returns a SendMessageBatchResponse object, which contains the unique ID of each message and the message content for each successfully sent message. It also returns the message ID, message content, and a sender's fault flag if the message failed to send.

    SendMessageBatchResponse response = client.SendMessageBatch(sendMessageBatchRequest); Console.WriteLine("Messages successfully sent:"); foreach (var success in response.Successful) { Console.WriteLine(" Message id : {0}", success.MessageId); Console.WriteLine(" Message content MD5 : {0}", success.MD5OfMessageBody); } Console.WriteLine("Messages failed to send:"); foreach (var failed in response.Failed) { Console.WriteLine(" Message id : {0}", failed.Id); Console.WriteLine(" Message content : {0}", failed.Message); Console.WriteLine(" Sender's fault? : {0}", failed.SenderFault); }