

Azure Service Bus - Scheduled Message Delivery - The Long Walk
source link: https://www.pmichaels.net/2021/01/01/azure-service-bus-scheduled-message-delivery/?utm_campaign=azure-service-bus-scheduled-message-delivery
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Azure Service Bus – Scheduled Message Delivery
Azure Service Bus sets itself apart from other message brokers by the dizzying array of additional and useful features that it provides out of the box. This particular one is really useful for things like scheduled e-mails. Let’s say, for example, that you’re an event organiser, and you want to notify people a few days before the event. This feature enables you to tell Service Bus to simply send a message at that time (you could have a simple Azure function that then picked up the message and sent an e-mail).
If you’re new to Service Bus, or using it with .Net, then start here.
NuGet
The basic NuGet package you’ll need is here:
Microsoft.Azure.ServiceBus
Reading the Service Bus Message
For the purpose of this post, we’ll just set-up a basic console application that sends and receives the message; let’s start with the read:
private
static
Task ReadMessageEvent(
string
connectionString)
{
var
queueClient =
new
QueueClient(connectionString, QUEUE_NAME);
var
messageHandlerOptions =
new
MessageHandlerOptions(ExceptionHandler);
queueClient.RegisterMessageHandler(handleMessage, messageHandlerOptions);
return
Task.CompletedTask;
}
private
static
Task ExceptionHandler(ExceptionReceivedEventArgs arg)
{
Console.WriteLine(
"Something bad happened!"
);
return
Task.CompletedTask;
}
private
static
Task handleMessage(Message message, CancellationToken cancellation)
{
string
messageBody = Encoding.UTF8.GetString(message.Body);
Console.WriteLine(
"Message received: {0}"
, messageBody);
return
Task.CompletedTask;
}
There’s not much to say here – this event will simply print a message to the console when it’s received.
Schedule the Service Bus Message
Now that we’ve set up a method to receive the messages, let’s send one. You could add this to the same console app (obviously it would have to occur after the Read!)
var
queueClient =
new
QueueClient(connectionString, QUEUE_NAME);
string
messageBody = $
"{DateTime.Now}: Happy New Year! ({Guid.NewGuid()}) You won't get this until {dateTime}"
;
var
message =
new
Message(Encoding.UTF8.GetBytes(messageBody));
long
sequenceNumber =
await
queueClient.ScheduleMessageAsync(message, dateTime);
//await queueClient.CancelScheduledMessageAsync(sequenceNumber);
await
queueClient.CloseAsync();
dateTime is simply the time that you wish to send the message; for example:
var
dateTime = DateTime.UtcNow.AddSeconds(10)
Will send the message in 10 seconds.
The commented line above will then cancel the message from being sent – you only need to provide the sequence number (which you get from setting up the schedule in the first place).
References and A GitHub Example
For a working sample of this, please see here.
https://stackoverflow.com/questions/60437666/how-to-defer-a-azure-service-bus-message
Recommend
-
28
In the process of enhancing MySQL replication with new features for our users, we also improve the internals. A lot of work goes also to modernize the codebase and make it easier to maintain and obviously extend as well.
-
3
Help Mozilla build out its Location Service by taking a walk Update (2021-04-21): The Mozilla Strumbler app has been discontinued. The app is no lo...
-
7
Azure Service Bus SDK Libraries Leave a reply I’ve written pretty
-
14
Azure Automation Run Books – Setup Leave a reply I’ve recently been investigating
-
19
Travel|A Long Walk in a Fading Corner of Japan
-
5
Cyclomatic complexity is one of those terms that makes you think you missed something when you were learning programming. However, the concept is a really simple one. Cyclomatic complexity is simply the number of paths through your code. There are...
-
3
Pick denial – Adjust delivery qty and send message to ERP system Purpose : The purpose of this article is to understand the pick denial concept. Pick denial is an important concept in outboun...
-
7
Stadia A message about Stadia and our long term streaming strategy ...
-
3
What Is Azure Content Delivery Network Service? A distributed netw...
-
5
Amazon Delivery Drivers Walk Out in First-Ever Driver StrikeThe drivers unionized with the Teamsters in late April, and are on strike to demand that Amazon come to the bargaining table. by...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK