Our program

Here is what our main.cs looks like when completed. You will notice that this is quite different from the other main files we have created in that we are also dealing with the Quartz scheduler as well:

static void Main(string[] args)
{
var builder = new ContainerBuilder();
// Service itself
builder.RegisterType<SchedulingMicroService>()
.AsImplementedInterfaces()
.AsSelf()
?.InstancePerLifetimeScope();
builder.RegisterType<Logger>().SingleInstance();
var container = builder.Build();
XmlConfigurator.ConfigureAndWatch(new FileInfo(@".log4net.config"));
HostFactory.Run(c =>
{
c?.UseAutofacContainer(container);
c?.UseLog4Net();
c?.ApplyCommandLineWithDebuggerSupport();
c?.EnablePauseAndContinue();
c?.EnableShutdown();
c?.OnException(ex => { Console.WriteLine(ex.Message); });

// Here is the main difference in this Microservice. We are going to run the Quartz scheduler as a microservice. We create our sample job, have it run every 30 seconds until we stop the microservice.

c.ScheduleQuartzJobAsService(q =>
q.WithJob(() => JobBuilder.Create<SampleJob>().Build())
.AddTrigger(() => TriggerBuilder.Create().WithSimpleSchedule(
build => build.WithIntervalInSeconds(30).RepeatForever()).Build())
).StartAutomatically();
c?.Service<SchedulingMicroService>(s =>
{
s.ConstructUsingAutofacContainer<SchedulingMicroService>();
s?.ConstructUsing(settings =>
{
var service = AutofacHostBuilderConfigurator.LifetimeScope.Resolve<SchedulingMicroService>();
return service;
});
s?.ConstructUsing(name => new SchedulingMicroService());
s?.WhenStarted((SchedulingMicroService server, HostControl host) => server.OnStart(host));
s?.WhenPaused(server => server.OnPause());
s?.WhenContinued(server => server.OnResume());
s?.WhenStopped(server => server.OnStop());
s?.WhenShutdown(server => server.OnShutdown());
});
c?.RunAsNetworkService();
c?.StartAutomaticallyDelayed();
c?.SetDescription(string.Intern("Scheduling Microservice Sample"));
c?.SetDisplayName(string.Intern("SchedulingMicroService"));
c?.SetServiceName(string.Intern("SchedulingMicroService"));
c?.EnableServiceRecovery(r =>
{
r?.OnCrashOnly();
r?.RestartService(1); //first
r?.RestartService(1); //second
r?.RestartService(1); //subsequents
r?.SetResetPeriod(0);
});
});
}

The highlighted lines are the difference with this microservice. We are starting Quartz.NET up as a service, adding a job and a trigger, configuring the execution frequency of the job, and then telling Topshelf to start the process automatically. You would need to remove this section if you were going to start a job programmatically when you receive a message. But for now, this microservice is designed to know what it needs to do automatically, and to do that once it starts up. Both scenarios may work for you, and you ultimately may choose to have both kinds of microservices (one that accepts messages and one that does not) running in your ecosystem.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset