.Net Core创建QuartZ定时任务
Quartz框架的一些基础概念解释:
Scheduler 作业调度器。
IJob 作业接口,继承并实现Execute, 编写执行的具体作业逻辑。
JobBuilder 根据设置,生成一个详细作业信息(JobDetail)。
TriggerBuilder 根据规则,生产对应的Trigger
时间表达式参考博客:https://www.cnblogs.com/GarfieldTom/p/3746290.html
一、创建项目
二、安装Quartz插件
三、创建QuartZ作业类
四、创建任务过程
1、控制台程序 (Program)
1 static void Main(string[] args) 2 { 3 Console.WriteLine("开始了:Hello Quart"); 4 Console.WriteLine("---------------------------------------------"); 5 QuartzExtensions.QuartzStart("Y").Wait(); 6 Console.WriteLine("系统进入Pending状态。输入 exit 退出....."); 7 Console.WriteLine("---------------------------------------------"); 8 while (true) 9 { 10 if (Console.ReadLine() == "exit") 11 break; 12 } 13 }
2、Quartz扩展创建任务(QuartzExtensions)
1 using Quartz; 2 using Quartz.Impl; 3 using System; 4 using System.Collections.Specialized; 5 using System.Threading.Tasks; 6 7 namespace CoreQuartDemo 8 { 9 public class QuartzExtensions 10 { 11 /// <summary> 12 /// Quart启动 13 /// </summary> 14 /// <param name="isImmediately">是否立即执行,此处只用于测试(默认N:否,Y:是)</param> 15 /// <returns></returns> 16 public static async Task QuartzStart(string isImmediately = "N") 17 { 18 try 19 { 20 // 从工厂中获取调度程序实例 21 NameValueCollection props = new NameValueCollection 22 { 23 { "quartz.serializer.type", "binary" } 24 }; 25 StdSchedulerFactory factory = new StdSchedulerFactory(props); 26 IScheduler scheduler = await factory.GetScheduler(); 27 // 开启调度器 28 await scheduler.Start(); 29 if (isImmediately == "Y") 30 { 31 /**动态同步信息,每天一次(可以根据自己需求设置时间)**/ 32 IJobDetail job2; 33 ITrigger trigger2; 34 int[] len = { 1, 2, 3, 4, 5 }; 35 for (int i = 1; i <= len.Length; i++) 36 { 37 string name = Guid.NewGuid().ToString("N") + i; 38 string group = Guid.NewGuid().ToString("N") + "group" + i; 39 //绑定任务 40 job2 = JobBuilder.Create<QuartzJob>() 41 .WithIdentity(name, group) 42 .Build(); 43 job2.JobDataMap.Put("param", "自定义执行参数" + i); 44 45 //创建执行时间 46 trigger2 = TriggerBuilder.Create() 47 .WithIdentity($"" + name + ".trigger", group + "1") 48 .StartNow() 49 .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(DateTime.Now.Hour, DateTime.Now.AddMinutes(i).Minute))//隔几分钟执行一次 50 .Build(); 51 //创建任务 52 scheduler.ScheduleJob(job2, trigger2).Wait();//作业,触发器加入调度器。异步执行 53 } 54 } 55 else 56 { 57 /**定时同步信息,每天重复一次,无限循环**/ 58 // 定义同步信息工作,并将其绑定到我们的IJob实现类 59 IJobDetail job = JobBuilder.Create<QuartzJob>() 60 .WithIdentity("QuartzJob", "QuartzJobGroup") 61 .Build(); 62 job.JobDataMap.Put("param", "定期执行参数1"); 63 // 触发作业 64 ITrigger trigger = TriggerBuilder.Create() 65 .WithIdentity("QuartzJobtRigger", "QuartzJobGroup") 66 .StartNow() 67 .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(11, 9))//24小时制 68 .Build(); 69 await scheduler.ScheduleJob(job, trigger); 70 71 // 定义获取信息工作,并将其绑定到我们的IJob实现类 72 IJobDetail job1 = JobBuilder.Create<QuartzJob>() 73 .WithIdentity("QuartzJob1", "QuartzJobGroup1") 74 .Build(); 75 job.JobDataMap.Put("param", "定期执行参数2"); 76 // 触发作业 77 ITrigger trigger1 = TriggerBuilder.Create() 78 .WithIdentity("QuartzJobtRigger1", "QuartzJobGroup1") 79 .StartNow() 80 .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(11, 10))//24小时制 81 .Build(); 82 await scheduler.ScheduleJob(job1, trigger1); 83 } 84 } 85 catch (System.Exception) 86 { 87 88 throw; 89 } 90 } 91 } 92 }
3、作业接口(QuartzJob)
1 using Quartz; 2 using System; 3 using System.Threading.Tasks; 4 5 namespace CoreQuartDemo 6 { 7 /// <summary> 8 /// 任务类,继承Quartz中IJob接口,创建任务 9 /// </summary> 10 public class QuartzJob : IJob 11 { 12 public Task Execute(IJobExecutionContext context) 13 { 14 string param = context.JobDetail.JobDataMap.GetString("param"); 15 try 16 { 17 Console.Write("执行时间" + DateTime.Now + "结果:+" + param + "\n"); 18 } 19 catch (Exception) 20 { 21 Console.Write("catch异常执行时间" + DateTime.Now + "结果:+" + param + "\n"); 22 throw; 23 } 24 25 return Task.CompletedTask; 26 } 27 } 28 }
定期执行结果:
自定义执行