验证代码:
1 class Program 2 { 3 public static IScheduler scheduler = null; 4 static void Main(string[] args) 5 { 6 scheduler = StdSchedulerFactory.GetDefaultScheduler(); 7 scheduler.Start(); 8 string GroupName = "DateRules"; 9 string RunCorn = "0 " + DateTime.Now.AddMinutes(1).Minute + " * * * ?"; 10 for (int i = 0; i < 30; i++) 11 { 12 string Identity = "DataRule_" + i; 13 14 JobDataMap JobData = new JobDataMap(); 15 JobData["Index"] = i; 16 IJobDetail job = JobBuilder.Create<MyJob>() 17 .WithIdentity(Identity, GroupName) 18 .SetJobData(JobData) 19 .Build(); 20 21 ITrigger trigger = TriggerBuilder.Create() 22 .WithIdentity(Identity, Identity) 23 .WithCronSchedule(RunCorn) 24 .Build(); 25 26 scheduler.ScheduleJob(job, trigger); 27 } 28 Console.ReadLine(); 29 }
public class MyJob : IJob { public void Execute(IJobExecutionContext context) { var FireTimeAfter = context.Trigger.GetFireTimeAfter(context.ScheduledFireTimeUtc.Value.ToLocalTime());
string Index = context.JobDetail.JobDataMap["Index"].ToString(); Thread.Sleep(30000); Console.WriteLine( FireTimeAfter.Value.ToLocalTime() + " " + context.ScheduledFireTimeUtc.Value.ToLocalTime() + " " + Index); } }
quartz本身包含一个线程池,让线程池中所有线程都Sleep30秒后输出ScheduledFireTimeUtc为示例:2019-09-17 09:00:00 ,当Sleep超过1分钟再启动的Job的ScheduledFireTimeUtc变为了:2019-09-17 09:01:00
这时使用使用GetFireTimeAfter重新取得应执行时间。