删除 Amazon SQS 队列 - Amazon SDK for .NET
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

是否要将 .NET 应用程序部署到Amazon通过几个简单点击即可? 尝试我们的新.NET CLI 工具为了简化部署体验!阅读我们的博客帖子然后提交反馈GitHub

有关更多信息,请参阅部署工具

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

删除 Amazon SQS 队列

此示例向您演示如何使用Amazon SDK for .NET以删除 Amazon SQS 队列。应用程序删除队列,等待最多给定的时间才能消失队列,然后显示剩余队列的列表。

如果你不提供任何命令行参数,应用程序只会显示现有队列的列表。

以下各节提供了此示例的代码段。这些区域有:例子的完整代码在此之后显示,并且可以按原样构建和运行。

删除队列

以下代码段删除由给定队列 URL 标识的队列。

这个示例本主题末尾的显示了这个片段正在使用中。

// // Method to delete an SQS queue private static async Task DeleteQueue(IAmazonSQS sqsClient, string qUrl) { Console.WriteLine($"Deleting queue {qUrl}..."); await sqsClient.DeleteQueueAsync(qUrl); Console.WriteLine($"Queue {qUrl} has been deleted."); }

等待队列消失

以下代码片段等待删除过程完成,这可能需要 60 秒钟。

这个示例本主题末尾的显示了这个片段正在使用中。

// // Method to wait up to a given number of seconds private static async Task Wait( IAmazonSQS sqsClient, int numSeconds, string qUrl) { Console.WriteLine($"Waiting for up to {numSeconds} seconds."); Console.WriteLine("Press any key to stop waiting. (Response might be slightly delayed.)"); for(int i=0; i<numSeconds; i++) { Console.Write("."); Thread.Sleep(1000); if(Console.KeyAvailable) break; // Check to see if the queue is gone yet var found = false; ListQueuesResponse responseList = await sqsClient.ListQueuesAsync(""); foreach(var url in responseList.QueueUrls) { if(url == qUrl) { found = true; break; } } if(!found) break; } }

显示现有队列的列表

以下代码段显示了 SQS 客户端区域中现有队列的列表。

这个示例本主题末尾的显示了这个片段正在使用中。

// // Method to show a list of the existing queues private static async Task ListQueues(IAmazonSQS sqsClient) { ListQueuesResponse responseList = await sqsClient.ListQueuesAsync(""); Console.WriteLine("\nList of queues:"); foreach(var qUrl in responseList.QueueUrls) Console.WriteLine($"- {qUrl}"); }

代码完成

此部分显示了此示例的相关参考和完整代码。

NuGet 软件包:

编程元素:

using System; using System.Threading; using System.Threading.Tasks; using Amazon.SQS; using Amazon.SQS.Model; namespace SQSDeleteQueue { // = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = // Class to update a queue class Program { private const int TimeToWait = 60; static async Task Main(string[] args) { // Create the Amazon SQS client var sqsClient = new AmazonSQSClient(); // If no command-line arguments, just show a list of the queues if(args.Length == 0) { Console.WriteLine("\nUsage: SQSCreateQueue queue_url"); Console.WriteLine(" queue_url - The URL of the queue you want to delete."); Console.WriteLine("\nNo arguments specified."); Console.Write("Do you want to see a list of the existing queues? ((y) or n): "); var response = Console.ReadLine(); if((string.IsNullOrEmpty(response)) || (response.ToLower() == "y")) await ListQueues(sqsClient); return; } // If given a queue URL, delete that queue if(args[0].StartsWith("https://sqs.")) { // Delete the queue await DeleteQueue(sqsClient, args[0]); // Wait for a little while because it takes a while for the queue to disappear await Wait(sqsClient, TimeToWait, args[0]); // Show a list of the remaining queues await ListQueues(sqsClient); } else { Console.WriteLine("The command-line argument isn't a queue URL:"); Console.WriteLine($"{args[0]}"); } } // // Method to delete an SQS queue private static async Task DeleteQueue(IAmazonSQS sqsClient, string qUrl) { Console.WriteLine($"Deleting queue {qUrl}..."); await sqsClient.DeleteQueueAsync(qUrl); Console.WriteLine($"Queue {qUrl} has been deleted."); } // // Method to wait up to a given number of seconds private static async Task Wait( IAmazonSQS sqsClient, int numSeconds, string qUrl) { Console.WriteLine($"Waiting for up to {numSeconds} seconds."); Console.WriteLine("Press any key to stop waiting. (Response might be slightly delayed.)"); for(int i=0; i<numSeconds; i++) { Console.Write("."); Thread.Sleep(1000); if(Console.KeyAvailable) break; // Check to see if the queue is gone yet var found = false; ListQueuesResponse responseList = await sqsClient.ListQueuesAsync(""); foreach(var url in responseList.QueueUrls) { if(url == qUrl) { found = true; break; } } if(!found) break; } } // // Method to show a list of the existing queues private static async Task ListQueues(IAmazonSQS sqsClient) { ListQueuesResponse responseList = await sqsClient.ListQueuesAsync(""); Console.WriteLine("\nList of queues:"); foreach(var qUrl in responseList.QueueUrls) Console.WriteLine($"- {qUrl}"); } } }

其他注意事项

  • 这些区域有:DeleteQueueAsyncAPI 调用不会检查您要删除的队列是否被用作死信队列。一个更复杂的程序可以检查这一点。