Quartz.NET是一個開源的做業調度框架。框架
官網地址:https://www.quartz-scheduler.net/
目前最新版本爲3.0.7,須要.NET Framework 4.5.2及更高版本以上。
.NET Framework 4.0及如下能夠用版本2.6.2。
本文用的是2.6.2版本,下面爲入門例子。ui
一、建立2個簡單任務spa
Hello1Job.cs.net
public class Hello1Job : IJob { public void Execute(IJobExecutionContext context) { LogHelper.Info("hello1"); } }
Hello2Job.cscode
public class Hello2Job : IJob { public void Execute(IJobExecutionContext context) { LogHelper.Info("hello2"); } }
二、建立任務服務blog
QuartzService.csit
//引用Quartz、Quartz.Impl; public class QuartzService { //調度器 IScheduler scheduler; public QuartzService() { //調度器工廠 ISchedulerFactory factory = new StdSchedulerFactory(); scheduler = factory.GetScheduler(); } public void Start() { //建立任務 IJobDetail job1 = JobBuilder.Create<Hello1Job>().WithIdentity(new JobKey("job1")).Build(); //建立觸發器,配置3秒執行一次 ITrigger trigger1 = TriggerBuilder.Create().WithCronSchedule("0/3 * * * * ?") .Build(); //建立任務 IJobDetail job2 = JobBuilder.Create<Hello2Job>().WithIdentity(new JobKey("job2")).Build(); //建立觸發器,配置5秒執行一次 ITrigger trigger2 = TriggerBuilder.Create().WithCronSchedule("0/5 * * * * ?").Build(); //將任務與觸發器添加到調度器中,若是須要多個就綁定多個便可 scheduler.ScheduleJob(job1, trigger1); scheduler.ScheduleJob(job2, trigger2); //開始執行 scheduler.Start(); } public void Shutdown() { if (scheduler != null) { scheduler.Shutdown(true); } } }
三、調用io
QuartzService quartzJob = new QuartzService(); quartzJob.Start();
運行結果以下:入門
2019-06-03 09:52:39,667 [DefaultQuartzScheduler_Worker-1] INFO logging - hello1 2019-06-03 09:52:40,000 [DefaultQuartzScheduler_Worker-2] INFO logging - hello2 2019-06-03 09:52:42,000 [DefaultQuartzScheduler_Worker-3] INFO logging - hello1 2019-06-03 09:52:45,000 [DefaultQuartzScheduler_Worker-4] INFO logging - hello1 2019-06-03 09:52:45,000 [DefaultQuartzScheduler_Worker-5] INFO logging - hello2 2019-06-03 09:52:48,000 [DefaultQuartzScheduler_Worker-6] INFO logging - hello1 2019-06-03 09:52:50,000 [DefaultQuartzScheduler_Worker-7] INFO logging - hello2 2019-06-03 09:52:51,000 [DefaultQuartzScheduler_Worker-8] INFO logging - hello1 2019-06-03 09:52:54,000 [DefaultQuartzScheduler_Worker-9] INFO logging - hello1 2019-06-03 09:52:55,000 [DefaultQuartzScheduler_Worker-10] INFO logging - hello2