第 4 版 (V4) AWS SDK for .NET 已發行!
如需有關中斷變更和遷移應用程式的資訊,請參閱遷移主題。
本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
傳送 Amazon SQS 訊息
此範例說明如何使用 AWS SDK for .NET 傳送訊息至 Amazon SQS 佇列,您可以透過程式設計方式或使用 Amazon SQS 主控台
此範例和接收訊息的下一個範例可以一起使用,以查看 Amazon SQS 中的訊息流程。
下列各節提供此範例的程式碼片段。之後會顯示範例的完整程式碼,並可依原樣建置和執行。
傳送訊息
下列程式碼片段會傳送訊息至指定佇列 URL 所識別的佇列。
本主題結尾的範例顯示此程式碼片段正在使用中。
// // Method to put a message on a queue // Could be expanded to include message attributes, etc., in a SendMessageRequest private static async Task SendMessage( IAmazonSQS sqsClient, string qUrl, string messageBody) { SendMessageResponse responseSendMsg = await sqsClient.SendMessageAsync(qUrl, messageBody); Console.WriteLine($"Message added to queue\n {qUrl}"); Console.WriteLine($"HttpStatusCode: {responseSendMsg.HttpStatusCode}"); }
傳送一批訊息
下列程式碼片段會將一批訊息傳送至指定佇列 URL 所識別的佇列。
本主題結尾的範例顯示此程式碼片段正在使用中。
// // Method to put a batch of messages on a queue // Could be expanded to include message attributes, etc., // in the SendMessageBatchRequestEntry objects private static async Task SendMessageBatch( IAmazonSQS sqsClient, string qUrl, List<SendMessageBatchRequestEntry> messages) { Console.WriteLine($"\nSending a batch of messages to queue\n {qUrl}"); SendMessageBatchResponse responseSendBatch = await sqsClient.SendMessageBatchAsync(qUrl, messages); // Could test responseSendBatch.Failed here foreach(SendMessageBatchResultEntry entry in responseSendBatch.Successful) Console.WriteLine($"Message {entry.Id} successfully queued."); }
從佇列刪除所有訊息
下列程式碼片段會從指定佇列 URL 識別的佇列中刪除所有訊息。這也稱為清除佇列。
本主題結尾的範例顯示此程式碼片段正在使用中。
// // Method to delete all messages from the queue private static async Task DeleteAllMessages(IAmazonSQS sqsClient, string qUrl) { Console.WriteLine($"\nPurging messages from queue\n {qUrl}..."); PurgeQueueResponse responsePurge = await sqsClient.PurgeQueueAsync(qUrl); Console.WriteLine($"HttpStatusCode: {responsePurge.HttpStatusCode}"); }
完成程式碼
本節顯示此範例的相關參考和完整程式碼。
NuGet 套件:
程式設計元素:
using System; using System.Collections.Generic; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; namespace SQSSendMessages { // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Class to send messages to a queue class Program { // Some example messages to send to the queue private const string JsonMessage = "{\"product\":[{\"name\":\"Product A\",\"price\": \"32\"},{\"name\": \"Product B\",\"price\": \"27\"}]}"; private const string XmlMessage = "<products><product name=\"Product A\" price=\"32\" /><product name=\"Product B\" price=\"27\" /></products>"; private const string CustomMessage = "||product|Product A|32||product|Product B|27||"; private const string TextMessage = "Just a plain text message."; static async Task Main(string[] args) { // Do some checks on the command-line if(args.Length == 0) { Console.WriteLine("\nUsage: SQSSendMessages queue_url"); Console.WriteLine(" queue_url - The URL of an existing SQS queue."); return; } if(!args[0].StartsWith("https://sqs.")) { Console.WriteLine("\nThe command-line argument isn't a queue URL:"); Console.WriteLine($"{args[0]}"); return; } // Create the Amazon SQS client var sqsClient = new AmazonSQSClient(); // (could verify that the queue exists) // Send some example messages to the given queue // A single message await SendMessage(sqsClient, args[0], JsonMessage); // A batch of messages var batchMessages = new List<SendMessageBatchRequestEntry>{ new SendMessageBatchRequestEntry("xmlMsg", XmlMessage), new SendMessageBatchRequestEntry("customeMsg", CustomMessage), new SendMessageBatchRequestEntry("textMsg", TextMessage)}; await SendMessageBatch(sqsClient, args[0], batchMessages); // Let the user send their own messages or quit await InteractWithUser(sqsClient, args[0]); // Delete all messages that are still in the queue await DeleteAllMessages(sqsClient, args[0]); } // // Method to put a message on a queue // Could be expanded to include message attributes, etc., in a SendMessageRequest private static async Task SendMessage( IAmazonSQS sqsClient, string qUrl, string messageBody) { SendMessageResponse responseSendMsg = await sqsClient.SendMessageAsync(qUrl, messageBody); Console.WriteLine($"Message added to queue\n {qUrl}"); Console.WriteLine($"HttpStatusCode: {responseSendMsg.HttpStatusCode}"); } // // Method to put a batch of messages on a queue // Could be expanded to include message attributes, etc., // in the SendMessageBatchRequestEntry objects private static async Task SendMessageBatch( IAmazonSQS sqsClient, string qUrl, List<SendMessageBatchRequestEntry> messages) { Console.WriteLine($"\nSending a batch of messages to queue\n {qUrl}"); SendMessageBatchResponse responseSendBatch = await sqsClient.SendMessageBatchAsync(qUrl, messages); // Could test responseSendBatch.Failed here foreach(SendMessageBatchResultEntry entry in responseSendBatch.Successful) Console.WriteLine($"Message {entry.Id} successfully queued."); } // // Method to get input from the user // They can provide messages to put in the queue or exit the application private static async Task InteractWithUser(IAmazonSQS sqsClient, string qUrl) { string response; while (true) { // Get the user's input Console.WriteLine("\nType a message for the queue or \"exit\" to quit:"); response = Console.ReadLine(); if(response.ToLower() == "exit") break; // Put the user's message in the queue await SendMessage(sqsClient, qUrl, response); } } // // Method to delete all messages from the queue private static async Task DeleteAllMessages(IAmazonSQS sqsClient, string qUrl) { Console.WriteLine($"\nPurging messages from queue\n {qUrl}..."); PurgeQueueResponse responsePurge = await sqsClient.PurgeQueueAsync(qUrl); Console.WriteLine($"HttpStatusCode: {responsePurge.HttpStatusCode}"); } } }
其他考量
-
如需訊息各種限制的相關資訊,包括允許的字元,請參閱《Amazon Simple Queue Service 開發人員指南》中的訊息相關配額。
-
訊息會保留在佇列中,直到刪除或清除佇列為止。當應用程式收到訊息時,即使佇列中仍有訊息,它也不會出現在佇列中。如需可見性逾時的詳細資訊,請參閱 Amazon SQS 可見性逾時。
-
除了訊息內文之外,您也可以將屬性新增至訊息。如需詳細資訊,請參閱訊息中繼資料。