Azure: How to move messages from poison queue to back to main queue?

前端 未结 7 1872
慢半拍i
慢半拍i 2020-12-30 06:57

I\'m wondering if there is a tool or lib that can move messages between queues? Currently, i\'m doing something like below

public static void ProcessQueueMes         


        
7条回答
  •  暖寄归人
    2020-12-30 07:46

    Here's an updated version of Mitch's answer, using the latest Microsoft.Azure.Storage.Queue package. Simply create a new .NET Console application, add the above-mentioned package to it, and replace the contents of Program.cs with the following:

    using Microsoft.Azure.Storage;
    using Microsoft.Azure.Storage.Queue;
    using System.Threading.Tasks;
    
    namespace PoisonMessageDequeuer
    {
        class Program
        {
            static async Task Main(string[] args)
            {
                const string queuename = "MyQueueName";
    
                string storageAccountString = "xxx";
    
                await RetryPoisonMesssages(storageAccountString, queuename);
            }
    
            private static async Task RetryPoisonMesssages(string storageAccountString, string queuename)
            {
                var targetqueue = GetCloudQueueRef(storageAccountString, queuename);
                var poisonqueue = GetCloudQueueRef(storageAccountString, queuename + "-poison");
    
                var count = 0;
                while (true)
                {
                    var msg = await poisonqueue.GetMessageAsync();
                    if (msg == null)
                        break;
    
                    await poisonqueue.DeleteMessageAsync(msg);
                    await targetqueue.AddMessageAsync(msg);
                    
                    count++;
                }
    
                return count;
            }
    
            private static CloudQueue GetCloudQueueRef(string storageAccountString, string queuename)
            {
                var storageAccount = CloudStorageAccount.Parse(storageAccountString);
                var queueClient = storageAccount.CreateCloudQueueClient();
                var queue = queueClient.GetQueueReference(queuename);
    
                return queue;
            }
        }
    }
    

    It's still pretty slow if you're working with >1000 messages though, so I'd recommend looking into batch APIs for higher quantities.

提交回复
热议问题