Deferred Messages
INFO
Remember that you can download the samples from the GitHub repository.
This sample makes use of Shuttle.Esb.AzureStorageQueues for the message queues. Local Azure Storage Queues should be provided by Azurite.
Once you have opened the Shuttle.Deferred.sln
solution in Visual Studio set the following projects as startup projects:
- Shuttle.Deferred.Client
- Shuttle.Deferred.Server
Implementation
Deferred messages refer to messages that are not immediately processed when available but are rather set to only process at a given future date.
In this guide we'll create the following projects:
Shuttle.Deferred.Client
(Console Application)Shuttle.Deferred.Server
(Console Application)Shuttle.Deferred.Messages
(Class Library)
Messages
Create a new class library called
Shuttle.Deferred.Messages
with a solution calledShuttle.Deferred
Note: remember to change the Solution name.
RegisterMember
Rename the
Class1
default file toRegisterMember
and add aUserName
property.
c#
namespace Shuttle.Deferred.Messages
{
public class RegisterMember
{
public string UserName { get; set; }
}
}
Client
Add a new
Console Application
to the solution calledShuttle.Deferred.Client
.
Install the
Shuttle.Esb.AzureStorageQueues
nuget package.
This will provide access to the Azure Storage Queues IQueue
implementation and also include the required dependencies.
Install the
Microsoft.Extensions.Configuration.Json
nuget package.
This will provide the ability to read the appsettings.json
file.
Add a reference to the
Shuttle.Deferred.Messages
project.
Program
Implement the main client code as follows:
c#
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Shuttle.Deferred.Messages;
using Shuttle.Esb;
using Shuttle.Esb.AzureStorageQueues;
namespace Shuttle.Deferred.Client
{
internal class Program
{
private static void Main(string[] args)
{
var services = new ServiceCollection();
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddServiceBus(builder =>
{
configuration.GetSection(ServiceBusOptions.SectionName).Bind(builder.Options);
});
services.AddAzureStorageQueues(builder =>
{
builder.AddOptions("azure", new AzureStorageQueueOptions
{
ConnectionString = "UseDevelopmentStorage=true;"
});
});
Console.WriteLine("Type some characters and then press [enter] to submit; an empty line submission stops execution:");
Console.WriteLine();
using (var bus = services.BuildServiceProvider().GetRequiredService<IServiceBus>().Start())
{
string userName;
while (!string.IsNullOrEmpty(userName = Console.ReadLine()))
{
bus.Send(new RegisterMember
{
UserName = userName
}, builder => builder.Defer(DateTime.Now.AddSeconds(5)));
}
}
}
}
}
The message sent will have its IgnoreTilleDate
(on the TransportMessage
) set to 5 seconds into the future.
Client configuration file
Add an
appsettings.json
file as follows:
json
{
"Shuttle": {
"ServiceBus": {
"MessageRoutes": [
{
"Uri": "azuresq://azure/shuttle-server-work",
"Specifications": [
{
"Name": "StartsWith",
"Value": "Shuttle.Deferred.Messages"
}
]
}
]
}
}
}
This tells the service bus that all messages sent having a type name starting with Shuttle.Deferred.Messages
should be sent to endpoint azuresq://azure/shuttle-server-work
.
Server
Add a new
Console Application
to the solution calledShuttle.Deferred.Server
.
Install the
Shuttle.Esb.AzureStorageQueues
nuget package.
This will provide access to the Azure Storage Queues IQueue
implementation and also include the required dependencies.
Install the
Microsoft.Extensions.Hosting
nuget package.
This allows a console application to be hosted using the .NET generic host.
Install the
Microsoft.Extensions.Configuration.Json
nuget package.
This will provide the ability to read the appsettings.json
file.
Add a reference to the
Shuttle.Deferred.Messages
project.
Program
Implement the
Program
class as follows:
c#
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Shuttle.Esb;
using Shuttle.Esb.AzureStorageQueues;
namespace Shuttle.Deferred.Server
{
public class Programs
{
public static void Main()
{
Host.CreateDefaultBuilder()
.ConfigureServices(services =>
{
var configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();
services.AddSingleton<IConfiguration>(configuration);
services.AddServiceBus(builder =>
{
configuration.GetSection(ServiceBusOptions.SectionName).Bind(builder.Options);
});
services.AddAzureStorageQueues(builder =>
{
builder.AddOptions("azure", new AzureStorageQueueOptions
{
ConnectionString = configuration.GetConnectionString("azure")
});
});
})
.Build()
.Run();
}
}
}
Server configuration file
Add an
appsettings.json
file as follows:
json
{
"ConnectionStrings": {
"azure": "UseDevelopmentStorage=true;"
},
"Shuttle": {
"ServiceBus": {
"Inbox": {
"WorkQueueUri": "azuresq://azure/shuttle-server-work",
"DeferredQueueUri": "azuresq://azure/shuttle-server-deferred",
"ErrorQueueUri": "azuresq://azure/shuttle-error"
}
}
}
}
RegisterMemberHandler
Add a new class called
RegisterMemberHandler
that implements theIMessageHandler<RegisterMember>
interface as follows:
c#
using System;
using Shuttle.Esb;
using Shuttle.Deferred.Messages;
namespace Shuttle.Deferred.Server
{
public class RegisterMemberHandler : IMessageHandler<RegisterMember>
{
public void ProcessMessage(IHandlerContext<RegisterMember> context)
{
Console.WriteLine($"[MEMBER REGISTERED] : user name = '{context.Message.UserName}'");
}
}
}
Run
Set both the client and server projects as the startup.
Execute
Execute the application.
The client application will wait for you to input a user name. For this example enter my user name and press enter:
INFO
After 5 seconds you will observe that the server application has processed the message.
You have now implemented deferred message sending.