选择您的 Cookie 首选项

我们使用必要 Cookie 和类似工具提供我们的网站和服务。我们使用性能 Cookie 收集匿名统计数据,以便我们可以了解客户如何使用我们的网站并进行改进。必要 Cookie 无法停用,但您可以单击“自定义”或“拒绝”来拒绝性能 Cookie。

如果您同意,AWS 和经批准的第三方还将使用 Cookie 提供有用的网站功能、记住您的首选项并显示相关内容,包括相关广告。要接受或拒绝所有非必要 Cookie,请单击“接受”或“拒绝”。要做出更详细的选择,请单击“自定义”。

接收 Amazon SQS 消息 - SDK for .NET (版本 3)

的版本 4 (V4) SDK for .NET 正在预览中!要在预览版中查看有关此新版本的信息,请参阅 AWS SDK for .NET (版本 4 预览版)开发者指南

请注意,SDK 的 V4 处于预览版,因此其内容可能会发生变化。

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

的版本 4 (V4) SDK for .NET 正在预览中!要在预览版中查看有关此新版本的信息,请参阅 AWS SDK for .NET (版本 4 预览版)开发者指南

请注意,SDK 的 V4 处于预览版,因此其内容可能会发生变化。

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

接收 Amazon SQS 消息

此示例向您展示如何使用接收 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 包裹:

编程元素:

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); } } }

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 开发人员指南》中的短轮询与长轮询

  • 在消息处理过程中,您可以使用接收句柄来更改消息可见性超时。有关如何执行此操作的信息,请参阅 Amazon SQSClientChangeMessageVisibilityAsync的方法。

  • 调用 DeleteMessageAsync 方法将无条件地从队列中删除消息,而无论可见性超时设置如何。

下一主题:

AWS Lambda

上一主题:

发送消息
隐私网站条款Cookie 首选项
© 2025, Amazon Web Services, Inc. 或其附属公司。保留所有权利。