View a markdown version of this page

接收 Amazon SQS 訊息 - AWS SDK for .NET (V4)

第 4 版 (V4) AWS SDK for .NET 已發行!

如需有關中斷變更和遷移應用程式的資訊,請參閱遷移主題

Orange button with text "Click here for details".

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

接收 Amazon SQS 訊息

此範例說明如何使用 AWS SDK for .NET 接收來自 Amazon SQS 佇列的訊息,您可以透過程式設計方式或使用 Amazon SQS 主控台建立。應用程式會從佇列讀取單一訊息、處理訊息 (在此情況下, 會在主控台上顯示訊息內文),然後從佇列刪除訊息。應用程式會重複這些步驟,直到使用者在鍵盤上輸入金鑰為止。

此範例和先前有關傳送訊息的範例可以一起使用,以查看 Amazon SQS 中的訊息流程。

下列各節提供此範例的程式碼片段。之後會顯示範例的完整程式碼,並可依原樣建置和執行。

接收訊息

下列程式碼片段會從指定佇列 URL 識別的佇列接收訊息。

本主題結尾的範例顯示此程式碼片段正在使用中。

// // Method to read a message from the given queue // In this example, it gets one message at a time private static async Task<ReceiveMessageResponse> GetMessage( IAmazonSQS sqsClient, string qUrl, int waitTime=0) { return await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest{ QueueUrl=qUrl, MaxNumberOfMessages=MaxMessages, WaitTimeSeconds=waitTime // (Could also request attributes, set visibility timeout, etc.) }); }

刪除訊息

下列程式碼片段會從指定佇列 URL 識別的佇列中刪除訊息。

本主題結尾的範例顯示此程式碼片段正在使用中。

// // Method to delete a message from a queue private static async Task DeleteMessage( IAmazonSQS sqsClient, Message message, string qUrl) { Console.WriteLine($"\nDeleting message {message.MessageId} from queue..."); await sqsClient.DeleteMessageAsync(qUrl, message.ReceiptHandle); }

完成程式碼

本節顯示此範例的相關參考和完整程式碼。

NuGet 套件:

程式設計元素:

using System; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; namespace SQSReceiveMessages { class Program { private const int MaxMessages = 1; private const int WaitTime = 2; static async Task Main(string[] args) { // Do some checks on the command-line if(args.Length == 0) { Console.WriteLine("\nUsage: SQSReceiveMessages 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) // Read messages from the queue and perform appropriate actions Console.WriteLine($"Reading messages from queue\n {args[0]}"); Console.WriteLine("Press any key to stop. (Response might be slightly delayed.)"); do { var msg = await GetMessage(sqsClient, args[0], WaitTime); if(msg.Messages.Count != 0) { if(ProcessMessage(msg.Messages[0])) await DeleteMessage(sqsClient, msg.Messages[0], args[0]); } } while(!Console.KeyAvailable); } // // Method to read a message from the given queue // In this example, it gets one message at a time private static async Task<ReceiveMessageResponse> GetMessage( IAmazonSQS sqsClient, string qUrl, int waitTime=0) { return await sqsClient.ReceiveMessageAsync(new ReceiveMessageRequest{ QueueUrl=qUrl, MaxNumberOfMessages=MaxMessages, WaitTimeSeconds=waitTime // (Could also request attributes, set visibility timeout, etc.) }); } // // Method to process a message // In this example, it simply prints the message private static bool ProcessMessage(Message message) { Console.WriteLine($"\nMessage body of {message.MessageId}:"); Console.WriteLine($"{message.Body}"); return true; } // // Method to delete a message from a queue private static async Task DeleteMessage( IAmazonSQS sqsClient, Message message, string qUrl) { Console.WriteLine($"\nDeleting message {message.MessageId} from queue..."); await sqsClient.DeleteMessageAsync(qUrl, message.ReceiptHandle); } } }

其他考量

  • 若要指定長輪詢,此範例會針對 ReceiveMessageAsync方法的每個呼叫使用 WaitTimeSeconds 屬性。

    您也可以在建立更新佇列時使用 ReceiveMessageWaitTimeSeconds 屬性,為佇列上的所有訊息指定長輪詢。

    如需短輪詢與長輪詢的相關資訊,請參閱《Amazon Simple Queue Service 開發人員指南》中的短輪詢和長輪詢

  • 在訊息處理期間,您可以使用接收控點來變更訊息可見性逾時。如需如何執行此操作的詳細資訊,請參閱 AmazonSQSClient 類別ChangeMessageVisibilityAsync的方法。

  • 無論可見性逾時設定為何,無條件呼叫 DeleteMessageAsync方法都會從佇列中移除訊息。