来自jobkey的Quartz jobdetail为null

时间:2018-01-20 06:33:19

标签: java quartz-scheduler

我有以下两种方法,

initialJob();
otherJobs();

其他工作只需要在初始工作完成后启动。

这是最初的工作方法。

private void initialJob(){
JobDetail jobDetail = JobBuilder.newJob(RestTriggerForQuartzJobs.class)
                          .withIdentity("INITIAL_JOB_NAME", TRIGGER_GROUP)
                          .usingJobData("PARAM", "")
                          .build();               
   Trigger trigger = TriggerBuilder.newTrigger().forJob(jobDetail).startNow().build();
   scheduler.scheduleJob(jobDetail, trigger);
}

我在这里检查初始工作是否完成。如果没有,则递归调用相同的方法直到完成

private void otherJobs(){
   if(isJobComplete("INITIAL_JOB_NAME")){
   JobDetail jobDetail = JobBuilder.newJob(RestTriggerForQuartzJobs.class)
                          .withIdentity("JOB_NAME", TRIGGER_GROUP)
                          .usingJobData("PARAM", "")
                          .build();               
   Trigger trigger = TriggerBuilder.newTrigger().forJob(jobDetail).startNow().build();
   scheduler.scheduleJob(jobDetail, trigger);
   }else{
   otherJobs();
   }
}

在isComplete方法中,对于JobDetail jobDetail = scheduler.getJobDetail(jobKey),jobDetail变为null,尽管我可以在quartz触发器表中看到DEFAULT.INITIAL_JOB_NAME。

private Boolean isJobComplete(String jobName) throws SchedulerException {
        JobKey jobKey = new JobKey(jobName);
        JobDetail jobDetail = scheduler.getJobDetail(jobKey);
        List<? extends Trigger> triggers = scheduler.getTriggersOfJob(jobDetail.getKey());
        for (Trigger trigger : triggers) {
            TriggerState triggerState = scheduler.getTriggerState(trigger.getKey());
            if (TriggerState.COMPLETE.equals(triggerState)) {
                return true;
            }
        }
        return false;
    }

1 个答案:

答案 0 :(得分:1)

方法scheduler.getJobDetail(jobKey)返回null,因为JobKey由名称和组组成,以便唯一地标识作业。

这意味着您必须使用名称和发起JobKey

private Boolean isJobComplete(String jobName) throws SchedulerException {
    JobKey jobKey = new JobKey(jobName, TRIGGER_GROUP);
    JobDetail jobDetail = scheduler.getJobDetail(jobKey);
    ...

顺便提一下:方法otherJobs以递归方式调用未定义的时间可能会导致StackOverflowError。也许listeners对您的用例很有用。