什么是Quartz
Quartz.Net是根据Java的Quartz用C#改写而来,源码在https://github.com/quartznet/quartznet 。主要作用是做一些周期性的工作,或者定时工作。比如每天凌晨2点对前一天的数据统计。
Quartz 核心概念 我们需要明白 Quartz 的几个核心概念,这样理解起 Quartz 的原理就会变得简单了。
Job 表示一个工作,要执行的具体内容。
JobDetail 表示一个具体的可执行的调度程序,Job 是这个可执行程调度程序所要执行的内容,另外 JobDetail 还包含了这个任务调度的方案和策略。
Trigger 代表一个调度参数的配置,什么时候去调。 触发器常用的有两种:SimpleTrigger触发器和CronTrigger触发器。
SimpleTrigger:能是实现简单业务,如每隔几分钟,几小时触发执行,并限制执行次数。
1 2 3 4 5 var trigger = TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(2 ).WithRepeatCount(5 )) .UsingJobData("key1" , 321 ) .WithIdentity("trigger" , "group" ) .Build();
CronTrigger:Cron表达式包含7个字段,秒 分 时 月内日期 月 周内日期 年(可选)。
举例
1 2 3 4 5 6 var trigger = TriggerBuilder.Create() .WithCronSchedule("0 0 0 1 1 ?" ) .UsingJobData("key1" , 321 ) .UsingJobData("key2" , "trigger-key2" ) .WithIdentity("trigger4" , "group14" ) .Build();
Scheduler 代表一个调度容器,一个调度容器中可以注册多个 JobDetail 和 Trigger。当 Trigger 与 JobDetail 组合,就可以被 Scheduler 容器调度了。
代码案例 以WebApi项目举例,用VS脚手架功能新建WebApi项目。
注册ISchedulerFactory的实例
1 2 3 4 5 6 public void ConfigureServices (IServiceCollection services ){ services.AddControllers(); services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>(); }
Controller中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;using Microsoft.AspNetCore.Http;using Microsoft.AspNetCore.Mvc;using Quartz;namespace QuartzNet.Web.Controllers { [ApiController ] [Route("[controller]" ) ] public class ValuesController : ControllerBase { private readonly ISchedulerFactory _schedulerFactory; private IScheduler _scheduler; public ValuesController (ISchedulerFactory schedulerFactory ) { this ._schedulerFactory = schedulerFactory; } [HttpGet ] public async Task<string []> Get() { _scheduler = await _schedulerFactory.GetScheduler(); await _scheduler.Start(); var trigger = TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(2 ).RepeatForever()) .Build(); var jobDetail = JobBuilder.Create<MyJob>() .WithIdentity("job" , "group" ) .Build(); await _scheduler.ScheduleJob(jobDetail, trigger); return await Task.FromResult(new string [] { "value1" , "value2" }); } } }
MyJob
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 using Quartz;using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace QuartzNet.Web { public class MyJob : IJob { public Task Execute (IJobExecutionContext context ) { return Task.Run(() => { using (StreamWriter sw = new StreamWriter(@"error.log" , true , Encoding.UTF8)) { sw.WriteLine(DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss" )); } }); } } }
可以看到在error.log中输出结果
1 2 3 4 5 6 2020-04-17 11-55-45 2020-04-17 11-55-47 2020-04-17 11-55-49 2020-04-17 11-55-51 2020-04-17 11-55-53 2020-04-17 11-55-55
你可以给Job增加参数
在Trigger中添加参数值
1 2 3 4 5 6 var trigger3 = TriggerBuilder.Create() .WithSimpleSchedule(x =>x.WithIntervalInSeconds(2 ).RepeatForever()) .UsingJobData("key1" , 321 ) .UsingJobData("key2" , "123" ) .WithIdentity("trigger2" , "group1" ) .Build();
在Job中添加参数值
1 2 3 4 5 IJobDetail job = JobBuilder.Create<MyJob>() .UsingJobData("key1" , 123 ) .UsingJobData("key2" , "123" ) .WithIdentity("job1" , "group1" ) .Build();
在job中获取参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 public class MyJob : IJob { public Task Execute (IJobExecutionContext context ) { var jobData = context.JobDetail.JobDataMap; var triggerData = context.Trigger.JobDataMap; var data = context.MergedJobDataMap; var value1= jobData.GetInt("key1" ); var value2= jobData.GetString("key2" ); var dateString = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss" );return Task.Run(() => { using (StreamWriter sw = new StreamWriter(@"error.log" , true , Encoding.UTF8)) { sw.WriteLine(dateString); } }); } }
当Job中的参数和Trigger中的参数名称一样时,用 context.MergedJobDataMap获取参数时,Trigger中的值会覆盖Job中的值。
上面那种情况只能适应那种,参数值不变的情况。假如有这种情况,这次的参数值是上一次执行后计算的值,就不能使用上面方法了。如 每两秒实现累加一操作,现在初始值是0,如果按照上面那种获取值的操作,一直都是0+1,返回值一直都是1。为了满足这个情况,只需要加一个特性[PersistJobDataAfterExecution]。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 [PersistJobDataAfterExecution ] public class MyJob : IJob { public Task Execute (IJobExecutionContext context ) { var jobData = context.JobDetail.JobDataMap; var triggerData = context.Trigger.JobDataMap; var data = context.MergedJobDataMap; var value1 = jobData.GetInt("key1" ); var value2 = jobData.GetString("key2" ); var value3 = data.GetString("key2" ); var dateString = DateTime.Now.ToString("yyyy-MM-dd HH-mm-ss" ); Random random = new Random(); jobData["key1" ] = random.Next(1 , 20 ); jobData["key2" ] = dateString; return Task.Run(() => { using (StreamWriter sw = new StreamWriter(@"C:\Users\Administrator\Desktop\error.log" , true , Encoding.UTF8)) { sw.WriteLine($"{dateString} value1:{value1} value2:{value2} " ); } }); } }
上面的例子是在api中使用Quartz
你也可以在 startup.cs中注册代码
1 2 3 4 5 6 7 8 9 10 11 12 13 public void ConfigureServices (IServiceCollection services ){ services.AddQuartz(typeof (QuartzJob)); } public void Configure (IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory ){ QuartzService.StartJobs<QuartzJob>(); }
或者在 HostedService
中注册代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 public class HostedService : IHostedService { private JobManager _jobManager; public HostedService ( JobManager jobManager ) { _jobManager = jobManager; } public Task StartAsync (CancellationToken cancellationToken ) { return Task.Factory.StartNew(async () => { await _jobManager.Start(); }, cancellationToken); } public Task StopAsync (CancellationToken cancellationToken ) { return Task.Factory.StartNew(async () => { await _jobManager.Stop(); }, cancellationToken); } }
参考:
https://www.cnblogs.com/MicroHeart/p/9402731.html https://www.cnblogs.com/dangzhensheng/p/10496278.html