Menghapus antrian Amazon SQS - AWS SDK for .NET

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Menghapus antrian Amazon SQS

Contoh ini menunjukkan kepada Anda cara menggunakan SQS antrean AWS SDK for .NET untuk menghapus Amazon. Aplikasi menghapus antrian, menunggu hingga jumlah waktu tertentu untuk antrian hilang, dan kemudian menunjukkan daftar antrian yang tersisa.

Jika Anda tidak memberikan argumen baris perintah apa pun, aplikasi hanya menampilkan daftar antrian yang ada.

Bagian berikut menyediakan cuplikan dari contoh ini. Kode lengkap untuk contoh ditampilkan setelah itu, dan dapat dibangun dan dijalankan apa adanya.

Hapus antrian

Cuplikan berikut menghapus antrian yang diidentifikasi oleh antrian yang diberikan. URL

Contoh di akhir topik ini menunjukkan cuplikan ini digunakan.

// // 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."); }

Tunggu antrian hilang

Cuplikan berikut menunggu proses penghapusan selesai, yang mungkin memakan waktu 60 detik.

Contoh di akhir topik ini menunjukkan cuplikan ini digunakan.

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

Tampilkan daftar antrian yang ada

Cuplikan berikut menunjukkan daftar antrian yang ada di wilayah klien. SQS

Contoh di akhir topik ini menunjukkan cuplikan ini digunakan.

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

Kode lengkap

Bagian ini menunjukkan referensi yang relevan dan kode lengkap untuk contoh ini.

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

Pertimbangan tambahan

  • DeleteQueueAsyncAPIPanggilan tidak memeriksa untuk melihat apakah antrian yang Anda hapus digunakan sebagai antrian huruf mati. Prosedur yang lebih canggih dapat memeriksa ini.

  • Anda juga dapat melihat daftar antrian dan hasil contoh ini di konsol Amazon SQS.