這一篇將介紹Quartz.NET的啞火策略(Misfire),直接上圖:
WithSimpleSchedule的啞火策略 |
WithMisfireHandlingInstructionFireNow |
不追趕啞火,如果有觸發啞火,立即執行,更新下次執行時間 |
|
WithMisfireHandlingInstructionIgnoreMisfires |
錯過的立即追趕,下次執行時間不變 |
||
WithMisfireHandlingInstructionNext |
WithRemainingCount |
不追趕啞火,正常執行,由於是正常執行下次執行時間不變了(執行次數 = 預計執行次數 - 錯過的次數) |
|
WithExistingCount |
不追趕啞火,正常執行,由於是正常執行下次執行時間不變了(執行次數不變) |
||
WithMisfireHandlingInstructionNow |
WithRemainingCount |
立即執行,但不追趕啞火的,更新下次執行時間(執行次數 = 預計執行次數 - 錯過的次數) |
|
WithExistingCount |
立即執行,但不追趕啞火的,更新下次執行時間(執行次數不變) |
||
WithCronSchedule的啞火策略 |
WithMisfireHandlingInstructionFireAndProceed |
啞火的任務合併到一次執行,下次正常執行 |
|
WithMisfireHandlingInstructionIgnoreMisfires |
追趕執行啞火的任務,下次正常執行 |
||
WithMisfireHandlingInstructionDoNothing |
什麼都不做,下次正常執行 |
具體的例子如下:
任務類MyJob:
public class MyJob : IJob
{
public void Execute(IJobExecutionContext context)
{
Console.WriteLine("本地執行時間:{0},下次執行時間:{1}",
context.ScheduledFireTimeUtc.Value.ToOffset(TimeSpan.FromHours(8)).ToString("yyyy-MM-dd HH:mm:ss"),
context.NextFireTimeUtc.Value.ToOffset(TimeSpan.FromHours(8)).ToString("yyyy-MM-dd HH:mm:ss")
);
}
}
1. WithMisfireHandlingInstructionFireNow(不追趕啞火,如果有觸發啞火,立即執行,更新下次執行時間)
var scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
//將鍵值對傳給定時器
var job = JobBuilder.Create<MyJob>()
.Build();
var trigger = TriggerBuilder.Create()
.StartAt(DateBuilder.DateOf(7,0,0))
.WithSimpleSchedule(m =>
m.WithIntervalInHours(1)
.WithRepeatCount(100)
//如果有觸發啞火,立即執行,如果之前定義的是12:00觸發,調度時間變成了現在的時間
.WithMisfireHandlingInstructionFireNow()
).Build();
scheduler.ScheduleJob(job, trigger);
執行結果如下:
2. WithMisfireHandlingInstructionIgnoreMisfires(錯過的立即追趕,下次執行時間不變)
var scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
//將鍵值對傳給定時器
var job = JobBuilder.Create<MyJob>()
.UsingJobData("count", 0)
.Build();
var trigger = TriggerBuilder.Create()
.StartAt(DateBuilder.DateOf(7, 0, 0))
.WithSimpleSchedule(m =>
m.WithIntervalInHours(1)
.WithRepeatCount(100)
//錯過的立即追趕,然後正常調度
.WithMisfireHandlingInstructionIgnoreMisfires()
).Build();
scheduler.ScheduleJob(job, trigger);
執行結果如下
3. WithMisfireHandlingInstructionNext(不追趕啞火,正常執行,由於是正常執行下次執行時間不變了)
var scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
//將鍵值對傳給定時器
var job = JobBuilder.Create<MyJob>()
.Build();
var trigger = TriggerBuilder.Create()
.StartAt(DateBuilder.DateOf(7, 0, 0))
.WithSimpleSchedule(m =>
m.WithIntervalInHours(1)
.WithRepeatCount(100)
//正常調度,執行次數 = 預計執行次數 - 錯過的次數
//.WithMisfireHandlingInstructionNextWithRemainingCount()
//正常調度,執行次數不變
.WithMisfireHandlingInstructionNextWithExistingCount()
).Build();
scheduler.ScheduleJob(job, trigger);
執行結果如下:
4. WithMisfireHandlingInstructionNow (立即執行,但不追趕啞火的,更新下次執行時間)
var scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
//將鍵值對傳給定時器
var job = JobBuilder.Create<MyJob>()
.Build();
var trigger = TriggerBuilder.Create()
.StartAt(DateBuilder.DateOf(7, 0, 0))
.WithSimpleSchedule(m =>
m.WithIntervalInHours(1)
.WithRepeatCount(100)
//立即執行,執行次數 = 預計執行次數 - 錯過的次數
//.WithMisfireHandlingInstructionNowWithRemainingCount()
//立即執行,執行次數不變
.WithMisfireHandlingInstructionNowWithExistingCount()
).Build();
scheduler.ScheduleJob(job, trigger);
執行結果如下:
5. WithMisfireHandlingInstructionFireAndProceed(啞火的任務合併到一次執行,下次正常執行)
var scheduler = StdSchedulerFactory.GetDefaultScheduler();
scheduler.Start();
//將鍵值對傳給定時器
var job = JobBuilder.Create<MyJob>()
.Build();
var trigger = TriggerBuilder.Create()
.StartAt(DateBuilder.DateOf(7, 0, 0))
.WithCronSchedule("0 0 7-20 ? * MON-FRI", //7AM - 8PM每小時執行一次
//啞火的任務合併到一次執行,下次正常執行
m => m.WithMisfireHandlingInstructionFireAndProceed()
//追趕執行啞火的任務,下次正常執行
//m => m.WithMisfireHandlingInstructionIgnoreMisfires()
//什麼都不做,下次正常執行
//m => m.WithMisfireHandlingInstructionDoNothing()
)
.Build();
scheduler.ScheduleJob(job, trigger);
執行結果如下:
6. WithMisfireHandlingInstructionIgnoreMisfires(追趕執行啞火的任務,下次正常執行)
將5的 m => m.WithMisfireHandlingInstructionIgnoreMisfires()取消註釋後,運行結果如下:
7. WithMisfireHandlingInstructionDoNothing(什麼都不做,下次正常執行)
將5的 m => m.WithMisfireHandlingInstructionDoNothing()取消註釋後,運行結果如下:
相關鏈接: