接收 Amazon SQS 消息 - AWS SDK for .NET

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

接收 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 方法将无条件地从队列中删除消息,而无论可见性超时设置如何。