失败时石英重试

Ave*_*oes 22 java quartz-scheduler

假设我有一个以这种方式配置的触发器:

<bean id="updateInsBBTrigger"         
    class="org.springframework.scheduling.quartz.CronTriggerBean">
    <property name="jobDetail" ref="updateInsBBJobDetail"/>
    <!--  run every morning at 5 AM  -->
    <property name="cronExpression" value="0 0 5 * * ?"/>
</bean>
Run Code Online (Sandbox Code Playgroud)

触发器必须与另一个应用程序连接,如果有任何问题(如连接失败),它应该每10分钟重试一次任务五次或直到成功.有什么方法可以配置触发器这样工作?

dog*_*ane 16

来源:在Quartz中自动重试失败的作业

如果你想要一个不断尝试的工作直到成功,你所要做的就是抛出一个带有标志的JobExecutionException,告诉调度程序在失败时再次触发它.以下代码显示了如何:

class MyJob implements Job {

    public MyJob() {
    }

    public void execute(JobExecutionContext context) throws JobExecutionException {

        try{
            //connect to other application etc
        }
        catch(Exception e){

            Thread.sleep(600000); //sleep for 10 mins

            JobExecutionException e2 = new JobExecutionException(e);
            //fire it again
            e2.setRefireImmediately(true);
            throw e2;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果您想重试一定次数,它会变得有点复杂.您必须使用StatefulJob并在其JobDataMap中保存retryCounter,如果作业失败,您将增加该值.如果计数器超过最大重试次数,则可以根据需要禁用该作业.

class MyJob implements StatefulJob {

    public MyJob() {
    }

    public void execute(JobExecutionContext context) throws JobExecutionException {
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        int count = dataMap.getIntValue("count");

        // allow 5 retries
        if(count >= 5){
            JobExecutionException e = new JobExecutionException("Retries exceeded");
            //make sure it doesn't run again
            e.setUnscheduleAllTriggers(true);
            throw e;
        }


        try{
            //connect to other application etc

            //reset counter back to 0
            dataMap.putAsString("count", 0);
        }
        catch(Exception e){
            count++;
            dataMap.putAsString("count", count);
            JobExecutionException e2 = new JobExecutionException(e);

            Thread.sleep(600000); //sleep for 10 mins

            //fire it again
            e2.setRefireImmediately(true);
            throw e2;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

  • -1,我不推荐这种方法 - 它将阻止一个Quartz工作线程10分钟.正确的方法是促进现有的Quartz功能 - 告诉它以某种方式在10分钟后重新运行相同的工作 - 毕竟,这就是它的用途.如果我们要运行一些代码并进行睡眠,那么首先使用Quartz是没有意义的. (60认同)
  • JobExecutionContext(至少在Quartz 2.2.1中,不确定其他版本)有一个getRefireCount()方法,可以用来代替count变量 (4认同)

dim*_*sli 7

我建议更多的灵活性和可配置性,以便更好地在数据库中存储两个偏移:repeatOffset将告诉你应该重试作业多长时间,而trialPeriodOffset将保留允许作业的时间窗口的信息改期.然后你可以检索这两个参数,如(我假设你使用的是Spring):

String repeatOffset = yourDBUtilsDao.getConfigParameter(..);
String trialPeriodOffset = yourDBUtilsDao.getConfigParameter(..);
Run Code Online (Sandbox Code Playgroud)

然后,而不是记住计数器的工作,它将需要记住initalAttempt:

Long initialAttempt = null;
initialAttempt = (Long) existingJobDetail.getJobDataMap().get("firstAttempt");
Run Code Online (Sandbox Code Playgroud)

并执行以下检查:

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;
        }
Run Code Online (Sandbox Code Playgroud)

最好为尝试的结果创建一个枚举,如上所述返回到应用程序的核心工作流程.

然后构建重新安排时间:

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;
Run Code Online (Sandbox Code Playgroud)


Flá*_*ira 7

我建议这样的实现在失败后恢复工作:

final JobDataMap jobDataMap = jobCtx.getJobDetail().getJobDataMap();
// the keys doesn't exist on first retry
final int retries = jobDataMap.containsKey(COUNT_MAP_KEY) ? jobDataMap.getIntValue(COUNT_MAP_KEY) : 0;

// to stop after awhile
if (retries < MAX_RETRIES) {
  log.warn("Retry job " + jobCtx.getJobDetail());

  // increment the number of retries
  jobDataMap.put(COUNT_MAP_KEY, retries + 1);

  final JobDetail job = jobCtx
      .getJobDetail()
      .getJobBuilder()
       // to track the number of retries
      .withIdentity(jobCtx.getJobDetail().getKey().getName() + " - " + retries, "FailingJobsGroup")
      .usingJobData(jobDataMap)
      .build();

  final OperableTrigger trigger = (OperableTrigger) TriggerBuilder
      .newTrigger()
      .forJob(job)
       // trying to reduce back pressure, you can use another algorithm
      .startAt(new Date(jobCtx.getFireTime().getTime() + (retries*100))) 
      .build();

  try {
    // schedule another job to avoid blocking threads
    jobCtx.getScheduler().scheduleJob(job, trigger);
  } catch (SchedulerException e) {
    log.error("Error creating job");
    throw new JobExecutionException(e);
  }
}
Run Code Online (Sandbox Code Playgroud)

为什么?

  1. 它不会阻止石英工人
  2. 这样可以避免背压。立即使用setRefire立即解雇该作业,这可能导致背压问题