本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
接收 Amazon SQS 消息
此示例向您展示如何使用 适用于 .NET 的 AWS 开发工具包 从 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 程序包:
编程元素:
-
命名空间 亚马逊.SQS
职业 AmazonSQS客户
-
命名空间 Amazon.SQS.型号
职业 接收消息请求
职业 接收消息响应
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); } } }
其他注意事项
-
在消息处理期间,您可以使用接收句柄更改消息可见性超时。有关如何执行此操作的信息,请参阅
ChangeMessageVisibilityAsync
方法 AmazonSQS客户 类。
-
调用
DeleteMessageAsync
方法无条件从队列中删除消息,无论可见性超时设置如何。