Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to defer a Azure Service Bus message?

Current I'm using Microsoft.Azure.ServiceBus.IQueueClient to RegisterMessageHandler, and then the message I receive is of type Microsoft.Azure.ServiceBus.Message.

According to the documentation:

Message deferral APIs The API is BrokeredMessage.Defer or BrokeredMessage.DeferAsync in the .NET Framework client, MessageReceiver.DeferAsync in the .NET Standard client, and IMessageReceiver.defer or IMessageReceiver.deferAsync in the Java client.

...but none of those libraries seam to relate to the classes I'm actually using. How do I defer? What classes and stuff do I have to use in order to be able to defer messages? All the samples above dont give enough code snippets to explain it.

Update as requested by @Gaurav

from your answer, I can see my message has that property:

message.ScheduledEnqueueTimeUtc = DateTime.UtcNow.AddHours(1);

but the queueClient also has this method:

queueClient.ScheduleMessageAsync(message, DateTime.UtcNow.AddHours(1));

I'm going to try 'scheduledMessageAsync' as I cant see how to communicate that I've set ScheduledEnqueueTimeUtc without calling the queueClient

like image 771
Ninjanoel Avatar asked Feb 27 '20 16:02

Ninjanoel


People also ask

What is a deferred message?

Deferred message are events observed when the destination server is temporarily limiting the inbound mail. In such situations, the sender (this would be our mail server in this case) will attempt to send the deferred message for up to 72 hours until it delivers it.

Is Azure Service Bus a message Bus?

What is Azure Service Bus? Azure Service Bus is a messaging service on cloud used to connect any applications, devices, and services running in the cloud to any other applications or services. As a result, it acts as a messaging backbone for applications available in the cloud or across any devices.

What is message lock duration in Azure Service Bus?

By default, the message lock expires after 60 seconds. This value can be extended to 5 minutes.


1 Answers

Microsoft.Azure.ServiceBus.Message has a property called ScheduledEnqueueTimeUtc. Just set the value of this property to a date/time value in future when you want the message to appear in the queue. Message will be hidden till that time and will only appear in the queue at that date/time.

UPDATE

So I ran a test and confirmed that both ScheduledEnqueueTimeUtc and ScheduleMessageAsync works. I used version 4.1.1 for Microsoft.Azure.ServiceBus SDK.

Here's the code I wrote:

    static void Main(string[] args)
    {
        var connectionString = "my-connection-string";
        var queueName = "test";
        QueueClient queueClient = new QueueClient(connectionString, queueName);
        Message msg1 = new Message()
        {
            Body = Encoding.UTF8.GetBytes("This message has ScheduledEnqueueTimeUtc property set. It will appear in queue after 2 minutes. Current date/time is: " + DateTime.Now),
            ScheduledEnqueueTimeUtc = DateTime.UtcNow.AddMinutes(2)
        };
        queueClient.SendAsync(msg1).GetAwaiter().GetResult();
        Message msg2 = new Message()
        {
            Body = Encoding.UTF8.GetBytes("This message is sent via ScheduleMessageAsync method. It will appear in queue after 2 minutes. Current date/time is: " + DateTime.Now)
        };
        queueClient.ScheduleMessageAsync(msg2, new DateTimeOffset(DateTime.UtcNow.AddMinutes(2))).GetAwaiter().GetResult();
        Console.ReadLine();
    }

And this is what I see when I fetch the messages in Peek-Lock mode:

enter image description here

like image 117
Gaurav Mantri Avatar answered Sep 30 '22 19:09

Gaurav Mantri