When using Visual Studio 2015+ the NuGet packages should be restored automatically. If you find that they do not or if you are using an older version of Visual Studio please execute the following in a Visual Studio command prompt:
cd {extraction-folder}\Shuttle.Esb.Samples\Shuttle.Deferred
nuget restore
Once you have opened the Shuttle.Deferred.sln
solution in Visual Studio set the following projects as startup projects:
Set
Shuttle.Core.Host.exe
as the Start external program option by navigating to the bin\debug folder of the server project for the Shuttle.Deferred.Server project.
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
Shuttle.Deferred.Server
Shuttle.Deferred.Messages
that will contain all our message classesCreate a new class library called
Shuttle.Deferred.Messages
with a solution calledShuttle.Deferred
Note: remember to change the Solution name.
Rename the
Class1
default file toRegisterMemberCommand
and add aUserName
property.
namespace Shuttle.Deferred.Messages
{
public class RegisterMemberCommand
{
public string UserName { get; set; }
}
}
Add a new
Console Application
to the solution calledShuttle.Deferred.Client
.
Install the
Shuttle.Esb.Msmq
nuget package.
This will provide access to the Msmq IQueue
implementation and also include the required dependencies.
Add a reference to the
Shuttle.Deferred.Messages
project.
Implement the main client code as follows:
using System;
using Shuttle.Esb;
using Shuttle.Deferred.Messages;
namespace Shuttle.Deferred.Client
{
class Program
{
static void Main(string[] args)
{
using (var bus = ServiceBus.Create().Start())
{
string userName;
while (!string.IsNullOrEmpty(userName = Console.ReadLine()))
{
bus.Send(new RegisterMemberCommand
{
UserName = userName
}, c => c.Defer(DateTime.Now.AddSeconds(5)));
}
}
}
}
}
The message sent will have its IgnoreTilleDate
set to 5 seconds into the future. You can have a look at the TransportMessage for more information on the message structure.
Create the shuttle configuration as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name='serviceBus' type="Shuttle.Esb.ServiceBusSection, Shuttle.Esb"/>
</configSections>
<serviceBus>
<messageRoutes>
<messageRoute uri="msmq://./shuttle-server-work">
<add specification="StartsWith" value="Shuttle.Deferred.Messages" />
</messageRoute>
</messageRoutes>
</serviceBus>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
This tells shuttle that all messages that are sent and have a type name starting with Shuttle.Deferred.Messages
should be sent to endpoint msmq://./shuttle-server-work
.
Add a new
Class Library
to the solution calledShuttle.Deferred.Server
.
Install the
Shuttle.Esb.Msmq
nuget package.
This will provide access to the Msmq IQueue
implementation and also include the required dependencies.
Install both the
Shuttle.Core.Host
andshuttle-core-infrastructure-log4net
nuget packages.
The default mechanism used to host an endpoint is by using a Windows service. However, by using the Shuttle.Core.Host
executable we are able to run the endpoint as a console application or register it as a Windows service for deployment.
We are also adding Log4Net to demonstrate how to add a third-party logging mechanism to shuttle.
Add a reference to the
Shuttle.Deferred.Messages
project.
Rename the default
Class1
file toHost
and implement theIHost
andIDisposabe
interfaces as follows:
using System;
using log4net;
using Shuttle.Core.Host;
using Shuttle.Core.Infrastructure;
using Shuttle.Core.Log4Net;
using Shuttle.Esb;
namespace Shuttle.Deferred.Server
{
public class Host : IHost, IDisposable
{
private IServiceBus _bus;
public void Start()
{
Log.Assign(new Log4NetLog(LogManager.GetLogger(typeof(Host))));
_bus = ServiceBus.Create().Start();
}
public void Dispose()
{
_bus.Dispose();
}
}
}
Add an
Application Configuration File
item to create theApp.config
and populate as follows:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name='serviceBus' type="Shuttle.Esb.ServiceBusSection, Shuttle.Esb"/>
<section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler, log4net" />
</configSections>
<log4net>
<appender name="ConsoleAppender" type="log4net.Appender.ColoredConsoleAppender">
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<appender name="RollingFileAppender" type="log4net.Appender.RollingFileAppender">
<file value="logs\deferred-server" />
<appendToFile value="true" />
<rollingStyle value="Composite" />
<maxSizeRollBackups value="10" />
<maximumFileSize value="100000KB" />
<datePattern value="-yyyyMMdd.'log'" />
<param name="StaticLogFileName" value="false" />
<layout type="log4net.Layout.PatternLayout">
<conversionPattern value="%d [%t] %-5p %c - %m%n" />
</layout>
</appender>
<root>
<level value="TRACE" />
<appender-ref ref="ConsoleAppender" />
<appender-ref ref="RollingFileAppender" />
</root>
</log4net>
<serviceBus>
<inbox
workQueueUri="msmq://./shuttle-server-work"
deferredQueueUri="msmq://./shuttle-server-deferred"
errorQueueUri="msmq://./shuttle-error" />
</serviceBus>
</configuration>
Add a new class called
RegisterMemberHandler
that implements theIMessageHandler<RegisterMemberCommand>
interface as follows:
using System;
using Shuttle.Esb;
using Shuttle.Deferred.Messages;
namespace Shuttle.Deferred.Server
{
public class RegisterMemberHandler : IMessageHandler<RegisterMemberCommand>
{
public void ProcessMessage(IHandlerContext<RegisterMemberCommand> context)
{
Console.WriteLine();
Console.WriteLine("[MEMBER REGISTERED] : user name = '{0}'", context.Message.UserName);
Console.WriteLine();
}
}
}
This will write out some information to the console window and send a response back to the sender (client).
Set
Shuttle.Core.Host.exe
as the Start external program option by navigating to the bin\debug folder of the server project.
Set both the client and server projects as the startup.
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:
You have now implemented deferred message sending.
You will also notice that Log4Net
has created the log file under ~\Shuttle.Deferred\Shuttle.Deferred.Server\bin\Debug\logs.