.net core中Quartz的使用
程序员文章站
2022-04-08 19:10:26
原来工作中有用到定时任务Quartz,不过是在MVC项目中,现在net core项目中也要用到,就开始改版。中间发现在网上的教程只有执行定时计划的过程,却很少有人写注册的过程,觉得有点略坑。所以写此文章一是自己做个记录,二是希望能帮助到其他人。后面还把此功能做出了多任务定时执行,网上关于net co ......
原来工作中有用到定时任务quartz,不过是在mvc项目中,现在net core项目中也要用到,就开始改版。中间发现在网上的教程只有执行定时计划的过程,却很少有人写注册的过程,觉得有点略坑。所以写此文章一是自己做个记录,二是希望能帮助到其他人。后面还把此功能做出了多任务定时执行,网上关于net core的教程更是少了,花费了不少时间实现了出来。
quartz版本:3.0.7
1.nuget中下载安装quartz
2.新建quartz帮助类
(1)新建quartzjob类,代码如下:
public class quartzjob : ijob { public async task execute(ijobexecutioncontext context) { var jobkey = context.jobdetail.key;//获取job信息 var triggerkey = context.trigger.key;//获取trigger信息 loghelper.info($"{datetime.now} quartzjob:==>>自动执行.{jobkey.name}|{triggerkey.name}"); await task.completedtask; } }
ps: 上面代码中的jobkey和triggerkey可获取到自己定义的job和trigger的信息,这样做多任务时可区分要执行的是哪个任务. 另loghelper.info是自己封装好的日子记录类,可替换为自己的日志类
(2)新建quartzfactory类,代码如下:
public class quartzfactory : ijobfactory { private readonly iserviceprovider _serviceprovider; public quartzfactory(iserviceprovider serviceprovider) { _serviceprovider = serviceprovider; } public ijob newjob(triggerfiredbundle bundle, ischeduler scheduler) { var jobdetail = bundle.jobdetail; var job = (ijob)_serviceprovider.getservice(jobdetail.jobtype); return job; } public void returnjob(ijob job) { } }
(3)新建quartzservice类,代码如下:
#region 单任务 public static void startjob<tjob>() where tjob : ijob { var scheduler = new stdschedulerfactory().getscheduler().result; var job = jobbuilder.create<tjob>() .withidentity("job") .build(); var trigger1 = triggerbuilder.create() .withidentity("job.trigger") .startnow() .withsimpleschedule(x => x.withinterval(timespan.fromseconds(5)).repeatforever()) .forjob(job) .build(); scheduler.addjob(job, true); scheduler.schedulejob(job, trigger1); scheduler.start(); } #endregion #region 多任务 public static void startjobs<tjob>() where tjob : ijob { var scheduler = new stdschedulerfactory().getscheduler().result; var job = jobbuilder.create<tjob>() .withidentity("jobs") .build(); var trigger1 = triggerbuilder.create() .withidentity("job.trigger1") .startnow() .withsimpleschedule(x => x.withinterval(timespan.fromseconds(5)).repeatforever()) .forjob(job) .build(); var trigger2 = triggerbuilder.create() .withidentity("job.trigger2") .startnow() .withsimpleschedule(x => x.withinterval(timespan.fromseconds(11)).repeatforever()) .forjob(job) .build(); var dictionary = new dictionary<ijobdetail, ireadonlycollection<itrigger>> { {job, new hashset<itrigger> {trigger1, trigger2}} }; scheduler.schedulejobs(dictionary, true); scheduler.start(); } #endregion #region 配置 public static void addquartz(this iservicecollection services, params type[] jobs) { services.addsingleton<ijobfactory, quartzfactory>(); services.add(jobs.select(jobtype => new servicedescriptor(jobtype, jobtype, servicelifetime.singleton))); services.addsingleton(provider => { var schedulerfactory = new stdschedulerfactory(); var scheduler = schedulerfactory.getscheduler().result; scheduler.jobfactory = provider.getservice<ijobfactory>(); scheduler.start(); return scheduler; }); } #endregion
3.在startup.cs中注册代码
public void configureservices(iservicecollection services) { services.addquartz(typeof(quartzjob));
//........ } public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory) { quartzservice.startjobs<quartzjob>(); //........ }
ps: 因addquartz方法定义的是type[]类型,所以可传多个执行类,如:services.addquartz(typeof(quartzjob1), typeof(quartzjob2)); 不过这样做startjob()方法就得另处理下,这里就略过
ok,大功告成,这个东西折腾了两天终于尘埃落定了,执行结果如下: