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

前端 未结 7 1838
慢半拍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:44

    As at (2018-09-11) version 1.4.1 of the Microsoft Azure Storage Explorer doesn’t have the ability to move messages from one Azure queue to another.

    I blogged a simple solution to transfer poison messages back to the originating queue and thought it might save someone a few minutes. Obviously, you'll need to have fixed the error that caused the messages to end up in the poison message queue!

    You’ll need to add a NuGet package reference to Microsoft.NET.Sdk.Functions :

    using Microsoft.WindowsAzure.Storage;
    using Microsoft.WindowsAzure.Storage.Queue;
    
    void Main()
    {
        const string queuename = "MyQueueName";
    
        string storageAccountString = "xxxxxx";
    
        RetryPoisonMesssages(storageAccountString, queuename);
    }
    
    private static int RetryPoisonMesssages(string storageAccountString, string queuename)
    {
        CloudQueue targetqueue = GetCloudQueueRef(storageAccountString, queuename);
        CloudQueue poisonqueue = GetCloudQueueRef(storageAccountString, queuename + "-poison");
    
        int count = 0;
        while (true)
        {
            var msg = poisonqueue.GetMessage();
            if (msg == null)
                break;
    
            poisonqueue.DeleteMessage(msg);
            targetqueue.AddMessage(msg);
            count++;
        }
    
        return count;
    }
    
    private static CloudQueue GetCloudQueueRef(string storageAccountString, string queuename)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageAccountString);
        CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
        CloudQueue queue = queueClient.GetQueueReference(queuename);
    
        return queue;
    }
    

提交回复
热议问题