的版本 4 (V4) 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 包裹:
编程元素:
-
命名空间 Amazon.SQS
Amazon 上课 SQSClient
-
命名空间 Amazon.SQS.Model
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); } } }
其他注意事项
-
在消息处理过程中,您可以使用接收句柄来更改消息可见性超时。有关如何执行此操作的信息,请参阅 Amazon SQSClient 类
ChangeMessageVisibilityAsync的方法。
-
调用
DeleteMessageAsync方法将无条件地从队列中删除消息,而无论可见性超时设置如何。