Amazon SQS 대기열 삭제 - AWS SDK for .NET

기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.

Amazon SQS 대기열 삭제

이 예제에서는 를 사용하여 Amazon SQS 대기열 AWS SDK for .NET 을 삭제하는 방법을 보여줍니다. 애플리케이션은 대기열을 삭제하고 대기열이 없어질 때까지 지정된 시간까지 기다린 다음 나머지 대기열의 목록을 표시합니다.

명령줄 인수를 제공하지 않으면 애플리케이션은 단순히 기존 대기열의 목록을 표시합니다.

다음 섹션에서는 이 예제의 코드 조각을 제공합니다. 예제의 전체 코드는 그 뒤에 표시되며, 그대로 빌드하고 실행할 수 있습니다.

대기열 삭제

다음 코드 조각은 지정된 대기열 에서 식별된 대기열을 삭제합니다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}"); } } }

추가 고려 사항

  • DeleteQueueAsync API 통화는 삭제하려는 대기열이 데드 레터 대기열로 사용되고 있는지 확인하지 않습니다. 좀 더 정교한 절차를 통해 이를 확인할 수 있습니다.

  • Amazon SQS 콘솔 에서 대기열 목록과 이 예제의 결과를 볼 수도 있습니다.