我们需要明白 Quartz 的几个核心概念,这样理解起 Quartz 的原理就会变得简单了。
void execute(JobExecutionContext context)
JobDetail 表示一个具体的可执行的调度程序,Job 是这个可执行程调度程序所要执行的内容,另外 JobDetail 还包含了这个任务调度的方案和策略。
触发器常用的有两种:SimpleTrigger触发器和CronTrigger触发器。
SimpleTrigger:能是实现简单业务,如每隔几分钟,几小时触发执行,并限制执行次数。
var trigger = TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(2).WithRepeatCount(5))//间隔2秒 执行6次 .UsingJobData("key1", 321) .WithIdentity("trigger", "group") .Build();
CronTrigger:Cron表达式包含7个字段,秒 分 时 月内日期 月 周内日期 年(可选)。
举例
var trigger = TriggerBuilder.Create() .WithCronSchedule("0 0 0 1 1 ?")// 每年元旦1月1日 0 点触发 .UsingJobData("key1", 321) .UsingJobData("key2", "trigger-key2") .WithIdentity("trigger4", "group14") .Build();
Trigger 代表一个调度参数的配置,什么时候去调。
Scheduler 代表一个调度容器,一个调度容器中可以注册多个 JobDetail 和 Trigger。当 Trigger 与 JobDetail 组合,就可以被 Scheduler 容器调度了。
以WebApi项目举例,用VS脚手架功能新建WebApi项目。
注册ISchedulerFactory的实例
// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddSingleton<ISchedulerFactory, StdSchedulerFactory>();//注册ISchedulerFactory的实例。 }
Controller中
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() { //1、通过调度工厂获得调度器 _scheduler = await _schedulerFactory.GetScheduler(); //2、开启调度器 await _scheduler.Start(); //3、创建一个触发器 var trigger = TriggerBuilder.Create() .WithSimpleSchedule(x => x.WithIntervalInSeconds(2).RepeatForever())//每两秒执行一次 .Build(); //4、创建任务 var jobDetail = JobBuilder.Create<MyJob>() .WithIdentity("job", "group") .Build(); //5、将触发器和任务器绑定到调度器中 await _scheduler.ScheduleJob(jobDetail, trigger); return await Task.FromResult(new string[] { "value1", "value2" }); } } }
MyJob
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//创建IJob的实现类,并实现Excute方法。 { 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中输出结果
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增加参数
var trigger3 = TriggerBuilder.Create() .WithSimpleSchedule(x =>x.WithIntervalInSeconds(2).RepeatForever())//间隔2秒 一直执行 .UsingJobData("key1", 321) //通过在Trigger中添加参数值 .UsingJobData("key2", "123") .WithIdentity("trigger2", "group1") .Build();
IJobDetail job = JobBuilder.Create<MyJob>() .UsingJobData("key1", 123)//通过Job添加参数值 .UsingJobData("key2", "123") .WithIdentity("job1", "group1") .Build();
在job中获取参数
public class MyJob : IJob { public Task Execute(IJobExecutionContext context) { var jobData = context.JobDetail.JobDataMap;//获取Job中的参数 var triggerData = context.Trigger.JobDataMap;//获取Trigger中的参数 var data = context.MergedJobDataMap;//获取Job和Trigger中合并的参数 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中的值。
[PersistJobDataAfterExecution]//更新JobDetail的JobDataMap的存储副本,以便下一次执行这个任务接收更新的值而不是原始存储的值 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);//这里面给key赋值,下次再进来执行的时候,获取的值为更新的值,而不是原始值 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}"); } //context.Scheduler.DeleteJob(context.JobDetail.Key); //context.Scheduler.Shutdown(); }); } }
上面的例子是在api中使用Quartz
你也可以在 startup.cs中注册代码
public void ConfigureServices(IServiceCollection services) { services.AddQuartz(typeof(QuartzJob)); //........ } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { QuartzService.StartJobs<QuartzJob>(); //........ }
或者在 HostedService
中注册代码
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