0

what is difference between subscriptionClient.AbandonAsync vs subscriptionClient.closeAsync.

i need to check whether topic exist in subscriptionClient or not. I can not use azure management client because of some limitation

Mashhad Saleem
  • 176
  • 1
  • 2
  • 17
  • As far as I knew, using the management client to check if the topic exits or the subscription exits is a good way. Why do not you want to use it? – Jim Xu Jan 22 '20 at 09:04
  • "_I can not use azure management client because of some limitation_" - what limitation specifically you're referring to? – Sean Feldman Jan 22 '20 at 18:53
  • because i have desktop application where i can not send credentials over thousand of installed app – Mashhad Saleem Jan 23 '20 at 10:44

2 Answers2

2

1. what is difference between subscriptionClient.AbandonAsync vs subscriptionClient.closeAsync.

According to my research and test, the method subscriptionClient.AbandonAsync is used to tell service bus that the message is abandoned and it needs to process the message again. Once the message's DeliveryCount reaches the MaxDeliveryCount, the message is moved to the DLQ, specifying the MaxDeliveryCountExceeded reason code. For more details, please refer to the document enter image description here

The method subscriptionClient.closeAsync is used to close the subscriptionClient then your client cannot receive the message from the subscription.

My test:

Code:

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

enter image description here

Jim Xu
  • 21,610
  • 2
  • 19
  • 39
  • please read the question carefully, i need to check whether topic exist from subscriptionClient or not. I can not use azure management client because of some limitation. Your answer does not tell whether topic exist or not – Mashhad Saleem Jan 29 '20 at 06:10
0

There are several options to validate a topic exists or not. I can think of the two most common ways that would be:

  1. Using Azure Service Bus SDK library with the ManagementClient.
  2. Using Azure Service Bus Management library.

The difference between the two is what you use to authenticate your code. With the SDK library, you can use a connection string or a TokenProvider (Managed Identity or another way to authenticate). Wherewith the Management library you can only authenticate with Azure Active Directory (requires tenant ID, client ID, client secret, subscription ID, and resource group name).

In regards to what seems to be a completly separate question

what is difference between subscriptionClient.AbandonAsync vs subscriptionClient.closeAsync.

subscriptionClient.AbandonAsync() abandons the message to allow another competing consumer to pick it up. Where subscriptionClient.closeAsync() closes SubscriptionClient's connection to the broker.

Sean Feldman
  • 23,443
  • 7
  • 55
  • 80