Refire quartz.net trigger after 15 minutes if job fails with exception

后端 未结 4 1476
心在旅途
心在旅途 2020-12-29 10:44

I have searched for an answer on how to retrigger a job after a ceratin amount of time, if the job throws an exception. I cannot see any simple way of doing this.

if

4条回答
  •  误落风尘
    2020-12-29 11:26

    Actually, its not necessary to create a new JobDetail like described by LeftyX. You can just schedule a new trigger that is connected to the JobDetail from the current context.

    public void Execute(JobExecutionContext context) {
        try {
            // code
        } catch (Exception ex) {
            SimpleTriggerImpl retryTrigger = new SimpleTriggerImpl(Guid.NewGuid().ToString());      
            retryTrigger.Description = "RetryTrigger";
            retryTrigger.RepeatCount = 0;
            retryTrigger.JobKey = context.JobDetail.Key;   // connect trigger with current job      
            retryTrigger.StartTimeUtc = DateBuilder.NextGivenSecondDate(DateTime.Now, 30);  // Execute after 30 seconds from now
            context.Scheduler.ScheduleJob(retryTrigger);   // schedule the trigger
    
            JobExecutionException jex = new JobExecutionException(ex, false);
            throw jex;
        }
    }
    

    This is less error prone than creating a new JobDetail. Hope that helps.

提交回复
热议问题