简体   繁体   English

发送推送通知期间关闭计算机时如何处理Quartz触发

[英]How to handle Quartz trigger when Computer is turned off during sending push notification

To send push notification daily with the interval of 24 hour, i've use quartz library. 为了每天24小时发送一次推送通知,我使用了石英库。 But the issue is when my pc is turned off or my network is disconnected, this peace of code is also triggered but my push notification is not send. 但是问题是当我的电脑关闭或网络断开连接时,也会触发这种代码安全性,但我的推送通知未发送。 My question is how can i manage trigger when my pc is turned off or network disconnected? 我的问题是当我的电脑关闭或网络断开时如何管理触发器? I want to send push notification after my pc is turned on or connected to the internet. 我想在我的电脑打开或连接到互联网后发送推送通知。

 ITrigger trigger = TriggerBuilder.Create()
                .WithDailyTimeIntervalSchedule
                  (s =>
                     s.WithIntervalInHours(24)
                    .OnEveryDay()
                    .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(11, 00))
                  )
                .Build();

 scheduler.ScheduleJob(job, trigger);

You need a combination of Misfire Instructions , a JobExecutionException and the DisallowConcurrentExecutionAttribute 您需要Misfire指令JobExecutionExceptionDisallowConcurrentExecutionAttribute的组合

  1. Set the DisallowConcurrentExecutionAttribute on your Job, this will prevent that your job will executed multiple times at the same time. 在作业上设置DisallowConcurrentExecutionAttribute ,这将防止您的作业同时执行多次。

     [DisallowConcurrentExecution] public class MyJob : IJob { .... } 
  2. Set the Misfire Instruction to your Trigger, this will tell your trigger to fire immediately in case it missed a execution. 将“ Misfire Instruction设置为您的触发器,这将告诉您的触发器立即触发以防万一它错过了执行。

     ITrigger trigger = TriggerBuilder.Create() .WithDailyTimeIntervalSchedule (s => s.WithIntervalInHours(24) .OnEveryDay() .StartingDailyAt(TimeOfDay.HourAndMinuteOfDay(11, 00)) .WithMisfireHandlingInstructionFireAndProceed() ) .Build(); 
  3. Catch any exception during the execution of your job eg when the internet is not reachable. 在执行工作期间(例如,无法访问Internet时)捕获任何异常。 Then throw a JobExecutionException and tell Quartz that it should fire the trigger again. 然后抛出JobExecutionException并告诉Quartz它应该再次触发触发器。

     public class MyJob : IJob { public void Execute(IJobExecutionContext context) { try { // connect to internet or whatever..... } catch(Exception) { throw new JobExecutionException(true); } } } 
  4. Next time, read the documentation first ;) 下次,请先阅读文档 ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM