Quartz retry when failure

后端 未结 3 1338
礼貌的吻别
礼貌的吻别 2020-12-07 18:12

Let\'s say I have a trigger configured this way:



        
3条回答
  •  悲&欢浪女
    2020-12-07 18:25

    I would suggest for more flexibility and configurability to better store in your DB two offsets: the repeatOffset which will tell you after how long the job should be retried and the trialPeriodOffset which will keep the information of the time window that the job is allowed to be rescheduled. Then you can retrieve these two parameters like (I assume you are using Spring):

    String repeatOffset = yourDBUtilsDao.getConfigParameter(..);
    String trialPeriodOffset = yourDBUtilsDao.getConfigParameter(..);
    

    Then instead of the job to remember the counter it will need to remember the initalAttempt:

    Long initialAttempt = null;
    initialAttempt = (Long) existingJobDetail.getJobDataMap().get("firstAttempt");
    

    and perform the something like the following check:

    long allowedThreshold = initialAttempt + Long.parseLong(trialPeriodOffset);
            if (System.currentTimeMillis() > allowedThreshold) {
                //We've tried enough, time to give up
                log.warn("The job is not going to be rescheduled since it has reached its trial period threshold");
                sched.deleteJob(jobName, jobGroup);
                return YourResultEnumHere.HAS_REACHED_THE_RESCHEDULING_LIMIT;
            }
    

    It would be a good idea to create an enum for the result of the attempt that is being returned back to the core workflow of your application like above.

    Then construct the rescheduling time:

    Date startTime = null;
    startTime = new Date(System.currentTimeMillis() + Long.parseLong(repeatOffset));
    
    String triggerName = "Trigger_" + jobName;
    String triggerGroup = "Trigger_" + jobGroup;
    
    Trigger retrievedTrigger = sched.getTrigger(triggerName, triggerGroup);
    if (!(retrievedTrigger instanceof SimpleTrigger)) {
                log.error("While rescheduling the Quartz Job retrieved was not of SimpleTrigger type as expected");
                return YourResultEnumHere.ERROR;
    }
    
            ((SimpleTrigger) retrievedTrigger).setStartTime(startTime);
            sched.rescheduleJob(triggerName, triggerGroup, retrievedTrigger);
            return YourResultEnumHere.RESCHEDULED;
    

提交回复
热议问题