background

Most of the business is based on timed tasks, and a framework like Quartz is particularly well suited to solving timed problems. For specific usage of Quartz, please refer to the official documentation. Let’s talk about the encapsulation of quartz plug-ins. We use Quartz. Plugin. Then, schedule is defined in the quartz_jobs. XML method, which is flexible in that it defines the attributes of Jobs. When the QuartzPlugin start method is executed, the quartz_jobs file will be loaded to load job information one by one.

solution

In practice, development is relatively simple, with no concern for how job tasks are scheduled. Simply define a class in the program that implements the Job interface, populate the business code, and fill in the file with the job property:

[DisallowConcurrentExecution] public class AnalysisJob : Public void Execute(IJobExecutionContext context) {XXXXXXXXXX}} <job> <name> name </name> <group> Group </group> <description> Description </description> <job-type> Class library </job-type> <durable>true</durable> <recover>false</recover> </job>Copy the code

This encapsulation gives the framework new skills and greatly improves the efficiency of the developer.

The main code

using System; using System.Collections.Generic; using System.Linq; using Topshelf; namespace HSCP.Task { class Program { static void Main(string[] args) { HostFactory.Run(x => { x.Service<MainService>((s) => { s.ConstructUsing(settings => new MainService()); s.WhenStarted(tr => tr.Start()); s.WhenStopped(tr => tr.Stop()); }); x.RunAsLocalSystem(); x.SetDescription(""); X.setdisplayname (" XXXX Task Manager "); x.SetServiceName("HSCP.Task"); }); } } } using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Quartz; using Quartz.Impl; namespace HSCP.Task { class MainService { static IScheduler sched; public void Start() { try { ISchedulerFactory factory = new StdSchedulerFactory(); sched = factory.GetScheduler(); sched.Start(); Console.WriteLine($" total {sched.getJobGroupNames ().count} task "); foreach (string gn in sched.GetJobGroupNames()) Console.WriteLine(gn); } catch (Exception exc) { Console.WriteLine(exc.ToString()); } // nlogger. Info(string.Format(" started successfully {0}", datetime.now)); } public void Stop() { sched.Shutdown(true); }}}Copy the code

Open source address

Github.com/quartznet/q…