根据官方例子整理,作为笔记备忘,当前使用.netcore3.1引入的是quartz.aspnetcore 3.2.4版,在startup中进行配置;
1、基本设置
public void ConfigureServices(IServiceCollection services)
{
services.AddQuartzServer(options => { options.WaitForJobsToComplete = true; });
services.AddQuartz(q =>
{
q.SchedulerId = "Scheduler-Core";
q.SchedulerName = "WebApi Quartz Demo";
q.UseMicrosoftDependencyInjectionJobFactory(options =>
{
options.CreateScope = false;
}
);
q.UseSimpleTypeLoader();
q.UseInMemoryStore();
q.UseDedicatedThreadPool(tp => { tp.MaxConcurrency = 10; });
}
}
2、此处记录四种添加job和trigger的方式
- 方式一,使用ScheduleJob快速构建
q.ScheduleJob<SampleJob>(trigger => { trigger.WithIdentity("SingleTrigger").StartNow() .WithSimpleSchedule( action => { action.WithIntervalInSeconds(10).WithRepeatCount(3); }).WithDescription("此任务只拥有单一的trigger"); });
- 方式二,使用AddJob和AddTrigger添加,可以同一个job添加多个trigger
var jobKey = new JobKey("apiDemoJob", "apiDemoJobGroup"); q.AddJob<SampleJob>(options => { options.WithIdentity(jobKey).WithDescription("asp.netApi 样例"); }); q.AddTrigger(t => { t.WithIdentity("apiDemoTrigger").ForJob(jobKey).StartNow().WithCronSchedule("0 0/2 * * * ?") .WithDescription("asp.netApi Trigger"); });
- 方式三,通过config配置job和trigger,需引用Quartz.plugins3.2.4
q.UseXmlSchedulingConfiguration(x => { x.Files = new[] { "~/quartz_jobs.config" }; x.ScanInterval = TimeSpan.FromMinutes(1); x.FailOnFileNotFound = true; x.FailOnSchedulingError = true; }); //需要注入job 获取GitHub还没发布的版本测试不需要注入 services.AddTransient<SampleJob>(); q.UseMicrosoftDependencyInjectionJobFactory(options => { //此处不设置会有报错,不过任务也能正常执行,github上最新的已移除此属性 options.AllowDefaultConstructor = true; options.CreateScope = false; } );
<?xml version="1.0" encoding="utf-8"?> <job-scheduling-data xmlns="http://quartznet.sourceforge.net/JobSchedulingData" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"> <processing-directives> <overwrite-existing-data>true</overwrite-existing-data> </processing-directives> <schedule> <job> <name>XML Job</name> <group>XML Job Group</group> <description>Job configured via XML</description> <job-type>QuartzWebApi.SampleJob,QuartzWebApi</job-type> <!-- <job-type>Quartz.Examples.AspNetCore.ExampleJob, Quartz.Examples.AspNetCore</job-type> --> <durable>true</durable> <recover>false</recover> <job-data-map> <entry> <key>key0</key> <value>value0</value> </entry> <entry> <key>key1</key> <value>value1</value> </entry> <entry> <key>key2</key> <value>value2</value> </entry> </job-data-map> </job> <trigger> <simple> <name>XML Trigger</name> <description>SimpleTriggerDescription</description> <job-name>XML Job</job-name> <job-group>XML Job Group</job-group> <start-time>1982-06-28T18:15:00.0Z</start-time> <end-time>2040-05-04T18:13:51.0Z</end-time> <misfire-instruction>SmartPolicy</misfire-instruction> <repeat-count>2</repeat-count> <repeat-interval>30000</repeat-interval> </simple> </trigger> </schedule> </job-scheduling-data>
- 方法四,通过QuartzOptions配置
//需要注入job services.AddTransient<SampleJob>(); services.Configure<SampleOptions>(Configuration.GetSection("Sample")); services.AddOptions<QuartzOptions>().Configure<IOptions<SampleOptions>>((options, dep) => { if (!string.IsNullOrEmpty(dep.Value.CronSchedule)) { var jobKey = new JobKey("optionsCustomJob", "customGroup"); options.AddJob<SampleJob>(j => j.WithIdentity(jobKey)); options.AddTrigger(trigger => { trigger.WithIdentity("optionsTrigger", "customGroup").ForJob(jobKey).WithCronSchedule(dep.Value.CronSchedule); }); } }); //"Sample": { // "CronSchedule": "0/25 * * * * ?" //}
配置好之后直接运行就可以了,不需要其它额外设定,真的好方便,部署到IIS中还是要注意资源回收的问题。