首页 > 解决方案 > subscriptionClient.AbandonAsync 与 subscriptionClient.closeAsync

问题描述

subscriptionClient.AbandonAsync 与 subscriptionClient.closeAsync 有什么区别。

我需要检查订阅客户端中是否存在主题。由于某些限制,我无法使用 azure 管理客户端

标签: c#azureazure-servicebus-topicsazure-servicebus-subscriptions

解决方案


1. subscriptionClient.AbandonAsync 与 subscriptionClient.closeAsync 有什么区别。

根据我的研究和测试,该方法subscriptionClient.AbandonAsync用于告诉服务总线该消息已被丢弃,需要重新处理该消息。一旦消息的 DeliveryCount 达到 MaxDeliveryCount,消息就会移动到 DLQ,并指定MaxDeliveryCountExceeded原因代码。更多详细信息,请参阅文档 在此处输入图像描述

该方法subscriptionClient.closeAsync用于关闭订阅客户端,然后您的客户端无法接收来自订阅的消息。

我的测试:

代码:

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

    class Program
    {
        const string ServiceBusConnectionString = "<your_connection_string>";
        const string TopicName = "<your_topic_name>";
        const string SubscriptionName = "<your_subscription_name>";
        static ISubscriptionClient subscriptionClient;

        public static async Task Main(string[] args)
        {    
            subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName);

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

            // Register subscription message handler and receive messages in a loop
            RegisterOnMessageHandlerAndReceiveMessages();

            Console.ReadKey();

            await subscriptionClient.CloseAsync();    
        }

        static void RegisterOnMessageHandlerAndReceiveMessages()
        {
            // Configure the message handler 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 processes messages.
            subscriptionClient.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)}");


            // This can be done only if the subscriptionClient is created in ReceiveMode.PeekLock mode (which is the default).
             await subscriptionClient.AbandonAsync(message.SystemProperties.LockToken);
            Console.WriteLine(message.SystemProperties.DeliveryCount);

        }

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

在此处输入图像描述


推荐阅读