【问题标题】:Inspecting and managing the Azure Service Bus dead letter queue检查和管理 Azure 服务总线死信队列
【发布时间】:2020-09-20 19:28:16
【问题描述】:

我正在使用 Azure 服务总线队列。

我需要实现一个诊断网页来显示“死信队列”(DLQ)中的项目列表,检查它,当我决定时,将单个 DLQ 消息移动到主队列进行重新处理。

我想我必须使用“Microsoft.Azure.ServiceBus”命名空间,也许是 ManagementClient,但我不确定这是正确的做法。

有没有人可以举个例子说明一下?

我想实现类似于 Service Bus Explorer 的 DLQ 管理,允许再次提交消息。

【问题讨论】:

  • 如果您正在考虑开箱即用的解决方案,您可以查看 Serverless360 来读取您的消息并对其进行诊断以进行进一步的消息处理。

标签: azure azureservicebus azure-servicebus-queues


【解决方案1】:

我想我必须使用“Microsoft.Azure.ServiceBus”命名空间, 也许是 ManagementClient,但我不确定这是正确的方法 跟随。

由于您正在处理消息(即数据),因此您需要使用 Microsoft.Azure.ServiceBus 命名空间。管理命名空间用于管理命名空间、队列、主题等。

有关代码示例,请参阅此链接:https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-get-started-with-queues

来自link

namespace CoreReceiverApp
{
    using System;
    using System.Text;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Azure.ServiceBus;

    class Program
    {
        // Connection String for the namespace can be obtained from the Azure portal under the 
        // 'Shared Access policies' section.
        const string ServiceBusConnectionString = "<your_connection_string>";
        const string QueueName = "<your_queue_name>";
        static IQueueClient queueClient;

        static void Main(string[] args)
        {
            MainAsync().GetAwaiter().GetResult();
        }

        static async Task MainAsync()
        {
            queueClient = new QueueClient(ServiceBusConnectionString, QueueName);

            Console.WriteLine("======================================================");
            Console.WriteLine("Press ENTER key to exit after receiving all the messages.");
            Console.WriteLine("======================================================");

            // Register QueueClient's MessageHandler and receive messages in a loop
            RegisterOnMessageHandlerAndReceiveMessages();

            Console.ReadKey();

            await queueClient.CloseAsync();
        }

        static void RegisterOnMessageHandlerAndReceiveMessages()
        {
            // Configure the MessageHandler Options in terms of exception handling, number of concurrent messages to deliver etc.
            var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
            {
                // Maximum number of Concurrent calls to the callback `ProcessMessagesAsync`, set to 1 for simplicity.
                // Set it according to how many messages the application wants to process in parallel.
                MaxConcurrentCalls = 1,

                // Indicates whether MessagePump should automatically complete the messages after returning from User Callback.
                // False below indicates the Complete will be handled by the User Callback as in `ProcessMessagesAsync` below.
                AutoComplete = false
            };

            // Register the function that will process messages
            queueClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
        }

        static async Task ProcessMessagesAsync(Message message, CancellationToken token)
        {
            // Process the message
            Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");

            // Complete the message so that it is not received again.
            // This can be done only if the queueClient is created in ReceiveMode.PeekLock mode (which is default).
            await queueClient.CompleteAsync(message.SystemProperties.LockToken);

            // Note: Use the cancellationToken passed as necessary to determine if the queueClient has already been closed.
            // If queueClient has already been Closed, you may chose to not call CompleteAsync() or AbandonAsync() etc. calls 
            // to avoid unnecessary exceptions.
        }

        static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs)
        {
            Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}.");
            var context = exceptionReceivedEventArgs.ExceptionReceivedContext;
            Console.WriteLine("Exception context for troubleshooting:");
            Console.WriteLine($"- Endpoint: {context.Endpoint}");
            Console.WriteLine($"- Entity Path: {context.EntityPath}");
            Console.WriteLine($"- Executing Action: {context.Action}");
            return Task.CompletedTask;
        }
    }
}

这是一个从队列接收消息的控制台应用程序。为了接收来自 DLQ 的消息,您只需将队列的名称更改为 &lt;your-queue-name&gt;/$deadletterqueue

此代码再次创建一个接收器,该接收器不断接收来自队列的消息,并且由于您正在构建网页,因此您需要按需接收消息或实现某种 websocket 类型的功能,以便服务器可以将更改推送到客户。

【讨论】:

  • 所以,也许我可以“接收”DLQ 消息并将它们存储在我的数据库中,例如调用我的 Web 应用程序的 webapi。这可能是一个解决方案。在此期间,我找到了我正在寻找的“服务总线资源管理器”(我所做的编辑中的图像)。我将尝试了解它是否从 DLQ 接收所有消息(不从队列中删除),或者它是否能够在消息之间迭代并管理单个项目。你认为它有什么作用?
猜你喜欢
  • 2018-04-01
  • 2014-12-09
  • 2017-07-01
  • 2017-08-13
  • 1970-01-01
  • 1970-01-01
  • 2017-02-13
  • 2017-06-07
  • 1970-01-01
相关资源
最近更新 更多