Simple use of Topshelf+Quatz.Net

Posted by adamb10 on Mon, 11 Nov 2019 10:45:20 +0100

An overview of Topshelf

Topshelf is another way to create Windows services, a foreigner's article Create a .NET Windows Service in 5 steps with Topshelf Use Topshelf to create a Windows service in five steps. Topshelf It is an open-source cross platform hosting service framework that supports Windows and Mono. It only needs a few lines of code to build a very convenient service host.

II. Use of Topshelf

1. Create a Timer task and use it in Topshelf

 1 public class TownCrier
 2     {
 3         private readonly Timer timer;
 4         public TownCrier()
 5         {
 6             timer = new Timer(1000) { AutoReset = true };
 7             timer.Elapsed += (sender,eventArgs) => Console.WriteLine($"It is {DateTime.Now} and all is well");
 8         }
 9         public void Start()
10         {
11             timer.Start();
12         }
13         public void Stop()
14         {
15             timer.Stop();
16         }
17 
18     }
19 
20 static void Main(string[] args)
21         {
22             HostFactory.Run(x =>
23             {
24                 x.Service<TownCrier>(s =>
25                 {
26                     s.ConstructUsing(name => new TownCrier());
27                     s.WhenStarted(tc => tc.Start());
28                     s.WhenStopped(tc => tc.Stop());
29                 });
30                 x.RunAsLocalSystem();
31                 x.SetDescription("QuartzNet Task scheduling service, flexible configuration of task plan");
32                 x.SetDisplayName("QuartzJobShedule");
33                 x.SetServiceName("Quartz Task scheduling framework");
34             }
35             );
36         }

2. Execute the program and operate normally. The effect is as follows

Overview of Quartz.Net

In the development process of the project, it is inevitable to encounter tasks that need to be processed in the background, such as sending e-mail notifications regularly, processing time-consuming data in the background, etc. at this time, Quartz.Net can be used.

Quartz.Net is pure. It is a. Net assembly. It is a very popular C ා implementation of Java job scheduling system quartz.

Quartz.Net is a full-featured task scheduling system, which can be applied from small-scale application to large-scale enterprise system. The full range of functions is reflected in the diversity of triggers, that is, it supports simple timers and Cron expressions; it can perform repeated job tasks and specify exceptional calendars; tasks can also be diverse, as long as they inherit the IJob interface.

For small applications, it can be integrated into your system. For enterprise level systems, it provides Routing support, Group to organize and manage tasks, and persistence, plug-in functions, load balancing and fault migration to meet the needs of different application scenarios.

Use of Quartz.Net

Create a task to inherit the IJob interface

 1 /// <summary>
 2     /// inherit IJob A task of
 3     /// </summary>
 4     public class HelloQuartzJob : IJob
 5     {
 6         public Task Execute(IJobExecutionContext context)
 7         {
 8             return Task.Factory.StartNew(() =>
 9             {
10                 Console.WriteLine("Hello Quartz.Net");
11             });
12         }
13     }

2 create a Scheduler when the program starts, and add a schedule for HelloQuartzJob

 1 static async Task Main(string[] args)
 2         {
 3             //Get scheduler instance from factory
 4             var scheduler = await StdSchedulerFactory.GetDefaultScheduler();
 5 
 6             //Create jobs and triggers
 7             var jobDetail = JobBuilder.Create<HelloQuartzJob>().Build();
 8             var trigger = TriggerBuilder.Create()
 9                                         .WithSimpleSchedule(m =>
10                                         {
11                                             m.WithRepeatCount(10).WithIntervalInSeconds(1);
12                                         })
13                                         .Build();
14 
15             Console.WriteLine($"Task scheduler started");
16 
17             //Add scheduling
18             await scheduler.ScheduleJob(jobDetail, trigger);
19 
20             await scheduler.Start();
21             Console.ReadKey();
22         }

3. Execution result

Five Topshelf+Quartz.Net

1. Create the scheduler class of Quartz and implement the Start and Stop methods

 1 public class JobConfigure
 2     {
 3         private IScheduler scheduler;
 4 
 5         public async Task GetJobConfigure()
 6         {
 7             //Get scheduler instance from factory
 8             scheduler = await StdSchedulerFactory.GetDefaultScheduler();
 9             //Create jobs and triggers
10             var jobDetail = JobBuilder.Create<HelloQuartzJob>().Build();
11             var trigger = TriggerBuilder.Create().WithSchedule(SimpleScheduleBuilder.RepeatSecondlyForever(1).WithRepeatCount(10))
12                                         .Build();
13             //Add scheduling
14             await scheduler.ScheduleJob(jobDetail, trigger);
15         }
16 
17         public void Start()
18         {
19             this.GetJobConfigure().Wait();
20             scheduler.Start();
21         }
22         public void Stop()
23         {
24             scheduler.Shutdown(true);
25         }
26         
27     }

2 create a Scheduler in the launcher and add a schedule for JobConfigure

 1 static void Main(string[] args)
 2         {
 3             HostFactory.Run(x =>
 4             {
 5                 x.Service<JobConfigure>(s =>
 6                 {
 7                     s.ConstructUsing(name => new JobConfigure());
 8                     s.WhenStarted(tc => tc.Start());
 9                     s.WhenStopped(tc => tc.Stop());
10                 });
11                 x.RunAsLocalSystem();
12                 x.SetDescription("QuartzNet Task scheduling service, flexible configuration of task plan");
13                 x.SetDisplayName("QuartzJobShedule");
14                 x.SetServiceName("Quartz Task scheduling framework");
15             }
16             );
17         }

3. Start the program

Topics: C# Windows Java