This article is contributed. See the original author and article here.

Use Case:

To peek/delete the scheduled messages from Azure service bus.

 

Pre-Requisites:

  • Azure Service bus Namespace
  • Azure Service bus SAS connection string
  • Console Application to peek/delete the scheduled message

Scenarios:

  • Query scheduled messages before the enqueue time.
  • You can process the scheduled message with future enqueue time and can delete it post processing them.

Steps to follow:

You can schedule messages either by setting the ScheduledEnqueueTimeUtc property when sending a message through the regular send path, or explicitly with the ScheduleMessageAsync API. The latter immediately returns the scheduled message’s SequenceNumber, which you can later use to cancel the scheduled message.

Scheduled messages and their sequence numbers can also be discovered using message browsing.

 

Message browsing, or peeking, enables a Service Bus client to enumerate all messages that reside in a queue or subscription, typically for diagnostic and debugging purposes. The peek operations return all messages that exist in the queue or subscription message log, not only those available for immediate acquisition with Receive() or the OnMessage() loop. The State property of each message tells you whether the message is active (available to be received), deferred, or scheduled.

 

Pull messages from the service bus entity using the ‘Peek’ method, this method fetches all the active (available to be received) and scheduled messages (future Enqueue date) and if fetched message ScheduledEnqueueTimeUtc property is not null then that message is scheduled message.

Post processing the scheduled message if you want to delete it then please use the CancelScheduledMessageAsync method by passing the sequenceNumber. 

Refer the sample code to Peek the messages from the queue and then delete the Scheduled messages.

using System.Threading.Tasks;

using Microsoft.Azure.ServiceBus;

using Microsoft.Azure.ServiceBus.Core;

using System.Collections;

using System.Collections.Generic;

namespace ReceiverQueueScheduledMessage

{

    class Program

    {

        // Connection String for the namespace can be obtained from the Azure portal under the

        // ‘Shared Access policies’ section.

        const string ServiceBusConnectionString = “[Service bus connection string]”;

        const string QueueName = “[Queue Name]”;

        static IQueueClient queueClient;

        static IMessageReceiver messageReceiver;

 

        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(“======================================================”);

 

                messageReceiver = new MessageReceiver(ServiceBusConnectionString, QueueName);

 

                Message message = await messageReceiver.PeekAsync();

 

                // Message with property ScheduledEnqueueTimeUtc not null means scheduled message     

                if (message != null && message.ScheduledEnqueueTimeUtc != null)

                {

                    await queueClient.CancelScheduledMessageAsync(message.SystemProperties.SequenceNumber);

                }

         

                 Console.ReadKey();

 

            await queueClient.CloseAsync();

        }

}

}

 

Pushed schedule messages to the queue, as shown in below screenshot (Queue name ‘samplequeue2’) has 2 scheduled messages:

 

yosing_0-1599655832475.png

 

 

Run the above code and Peek the message and check schedule message ‘ScheduledEnqueueTimeUtc’ property:

 

yosing_1-1599655832484.png

 

 

Once CancelScheduledMessageAsync method is called for the schedule message, schedule message will be deleted from the queue:

 

yosing_2-1599655832493.png

 

 

 

 

Running the above sample code, you should be able to peek/delete all the scheduled messages from Azure service bus.

Hope this helps!

Brought to you by Dr. Ware, Microsoft Office 365 Silver Partner, Charleston SC.

%d bloggers like this: