[英]How to handle Quartz trigger when Computer is turned off during sending push notification
為了每天24小時發送一次推送通知,我使用了石英庫。 但是問題是當我的電腦關閉或網絡斷開連接時,也會觸發這種代碼安全性,但我的推送通知未發送。 我的問題是當我的電腦關閉或網絡斷開時如何管理觸發器? 我想在我的電腦打開或連接到互聯網后發送推送通知。
ITrigger trigger = TriggerBuilder.Create()
.WithDailyTimeIntervalSchedule
(s =>
s.WithIntervalInHours(24)
.OnEveryDay()
.StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(11, 00))
)
.Build();
scheduler.ScheduleJob(job, trigger);
您需要Misfire指令 , JobExecutionException和DisallowConcurrentExecutionAttribute的組合
在作業上設置DisallowConcurrentExecutionAttribute
,這將防止您的作業同時執行多次。
[DisallowConcurrentExecution] public class MyJob : IJob { .... }
將“ Misfire Instruction
設置為您的觸發器,這將告訴您的觸發器立即觸發以防萬一它錯過了執行。
ITrigger trigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule (s => s.WithIntervalInHours(24) .OnEveryDay() .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(11, 00)) .WithMisfireHandlingInstructionFireAndProceed() ) .Build();
在執行工作期間(例如,無法訪問Internet時)捕獲任何異常。 然后拋出JobExecutionException
並告訴Quartz它應該再次觸發觸發器。
public class MyJob : IJob { public void Execute(IJobExecutionContext context) { try { // connect to internet or whatever..... } catch(Exception) { throw new JobExecutionException(true); } } }
下次,請先閱讀文檔 ;)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.