How to peek the deadletter messages

风流意气都作罢 提交于 2020-07-20 04:50:28

问题


It is very hard to find some good documentation on getting all the messages in a deadletter queue and getting to take a peek at them.

I have an Azure Servicebus Queue and everything I can find is for Azure Servicebus Topics.

Can someone help me with a quick guide?


回答1:


Dead letter queue is a secondary sub-queue where the poison messages are moved to.

In case of Azure Servicebus Queue the standard path for DLQ is queuePath/$DeadLetterQueue. So you need to have another queueClient to read the DLQ.

And you will do something like this in .NET clients.

string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
var client = QueueClient.CreateFromConnectionString(connectionString, "QueueName");

// do whatever regular queue reading activities

// this is for dead letter queue
var deadLetterClient = QueueClient.CreateFromConnectionString(connectionString, QueueClient.FormatDeadLetterPath(client.Path), ReceiveMode.ReceiveAndDelete);

BrokeredMessage receivedDeadLetterMessage;
while ((receivedDeadLetterMessage = deadLetterClient.Receive(TimeSpan.FromSeconds(10))) != null)
{
    Console.WriteLine(receivedDeadLetterMessage);
}



回答2:


The Azure portal now offers a service bus explorer (preview) tool to perform basic operations (such as Send, Receive, Peek) on Queues/Topics and their dead-letter subentities, right from the portal itself. Check this link on detailed instructions about using this tool - azure-service-bus-messaging-explorer.

For using more advanced features such as import/export functionality or the ability to test topic, queues, subscriptions, relay services, notification hubs and events hubs, try using the community-owned operations support system (OSS) tool service bus explorer. As on today, you can only peek 32 messages on the preview version of service bus explorer on Azure portal. So, if you have more messages to deal with, then you might want to opt for the above said standalone community-owned OSS tool.




回答3:


string connectionString = ConfigurationManager.AppSettings["connectionString"];    
string queueName = ConfigurationManager.AppSettings["queueName"];    
ServiceBusConnectionStringBuilder builder = new ServiceBusConnectionStringBuilder(connectionString);    
MessagingFactory factory = MessagingFactory.CreateFromConnectionString(builder.ToString());    
var client = QueueClient.CreateFromConnectionString(connectionString, queueName);    
string deadLetterQueuePath = QueueClient.FormatDeadLetterPath(queueName);    
QueueClient deadletterQueueClient = factory.CreateQueueClient(deadLetterQueuePath);    
while (true)    
{    
       BrokeredMessage brokeredMessage = deadletterQueueClient.Receive();    
       // Your Logic    
}



回答4:


Here is an example of how you can get a list of all messages in the dead letter queue using Peek:

public async Task<IEnumerable<BrokeredMessage>> GetDeadLetterMessagesAsync(string connectionString,
    string queueName)
{
    var queue = QueueClient.CreateFromConnectionString(connectionString, QueueClient.FormatDeadLetterPath(queueName));
    var messageList = new List<BrokeredMessage>();
    BrokeredMessage message;
    do
    {
        message = await queue.PeekAsync();
        if (message != null)
        {
            messageList.Add(message);
        }
    } while (message != null);
    return messageList;
}


来源:https://stackoverflow.com/questions/38784331/how-to-peek-the-deadletter-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!