Azure Service Bus: Scheduling Reliable Messages

Azure Service Bus

Introduction To Azure Service Bus

The Azure Service Bus provides developers a simple way to handle when messages are available on your Azure Service Bus Queues. Let’s take a look at how you schedule messages and a simple example of when you would use it.

How Azure Service Bus It Works

When posting a message to the Azure Service Bus, we need to set the ScheduledEnqueueTimeUtc on the message. As the property name implies, the time set on this property should be in UTC. This makes it very easy to tell the Azure Service Bus when the message should appear in the Queue and be available to any subscribers. Let’s look at a quick code example:

    private async Task SendMessage(string queue, string message, 
            DateTimeOffset? enqueueTime = null)
        {
            await using var queueClient = new ServiceBusClient(_connection);

            ServiceBusMessage messageQueueMessage = new ServiceBusMessage(message);

            if (enqueueTime != null) 
                messageQueueMessage.ScheduledEnqueueTime = enqueueTime.Value;

            await queueClient.CreateSender(queue).SendMessageAsync(messageQueueMessage);
        }

To be clear, this is not the time the message gets processed, it’s just when it becomes available in the Queue. Processing of the message will depend on how many messages are in the queue.

Real-Life Example

Let’s say within your solution you have an Azure App Service hosting your API that posts messages on your Azure Service Bus. Listening to a Queue on the bus is an Azure Function with a Service Bus Trigger. When our Azure Function is triggered, it will send out a text message immediately.

Service Bus Graphic 1

Over a few weeks, the business receives feedback that end users would prefer to receive text messages after a certain time. So easy enough, when your API posts messages to the Azure Service Bus, developers set the ScheduledEnqueueTimeUtc to the appropriate time.

Checking To See If It Works

Now that we have messages scheduled, we can look inside our Azure Service Bus, select the Queue the messages are in, and check out how many are scheduled.

Azure Service Bus Queue

Conclusion

As you can see while looking into Azure Service Bus above it’s not as complicated as you might believe. It is a simple way to handle messages in the queue, and simple to execute. To learn more about Azure Service Bus or any other Azure services, contact a Rōnin today. 

About Rōnin Consulting – Rōnin Consulting provides software engineering and systems integration services for healthcare, financial services, distribution, technology, and other business lines. Services include custom software development and architecture, cloud and hybrid implementations, business analysis, data analysis, and project management for a range of clients from the Fortune 500 to rapidly evolving startups. For more information, please contact us today.

Author:
Brian Weiss is a UI/UX Architect hellbent on designing and developing, meaningful, human-focused applications. He's also hellbent on finding the best chocolate chip cookie this world has to offer. When he's not developing software or eating cookies, he's probably watching anime and collecting Funko Pops.