本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
傳送 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 套件:
編程元素:
-
命名空間亞馬遜。SQS
-
命名空間亞馬遜。
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 簡單佇列服務開發人員指南中與訊息相關的配額。
-
訊息會保留在佇列中,直到刪除或清除佇列為止。當應用程式收到訊息時,即使該訊息仍存在於佇列中,也不會顯示在佇列中。如需可見性逾時的詳細資訊,請參閱 Amazon SQS 能見度逾時。
-
除了郵件內文之外,您還可以將屬性新增至郵件。如需詳細資訊,請參閱訊息中繼資料。