无法在石英中运行嵌套作业

时间:2014-03-18 00:35:43

标签: java spring quartz-scheduler jobs job-scheduling

我正在使用Quartz 1.8来编写一些每日/每周/每月运行作业的代码。当该作业运行时,它应该创建一个嵌套作业。但是出于某种原因,嵌套的JobDetail和Trigger都不会结束数据库中的条目,也不会运行。

以下是我正在使用的示例:

public class QuartzSSCCE {
    private static Logger LOG = Logger.getLogger(QuartzSSCCE.class);
    private static long counter = 0L;
    private static Scheduler scheduler;

    public void scheduleOuterJob(){
        try{
            LOG.info("About to try to schedule OuterJob");
            CronTrigger ct = new CronTrigger("OuterJobTrigger" + counter++, "OuterJobGroup", "30 * * ? * *");
            ct.setStartTime((new DateTime(new Date())).plusSeconds(3).toDate());
            DateTime endTime = new DateTime(new Date());
            endTime = endTime.plusMinutes(5);
            ct.setEndTime(endTime.toDate());

            JobDetail jd = new JobDetail("OuterJobDetail" + counter++, "OuterJobGroup", OuterJob.class);

            scheduler.scheduleJob(jd, ct);
        } catch (Exception e){
            LOG.fatal("something went wrong while scheduling the outer job", e);
        }
    }

    public static class OuterJob implements Job {
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            try{
                LOG.info("running OuterJob");
                //Trigger trigger = TriggerUtils.makeImmediateTrigger("InnerTriggerName" + counter++, 1, 1);
                SimpleTrigger simpleTrigger = new SimpleTrigger(
                        "InnerTriggerName" + counter++,
                        "InnerTriggerGroup",
                        (new DateTime(new Date())).plusSeconds(10).toDate(),
                        (new DateTime(new Date()).plusSeconds(50)).toDate(),
                        1,
                        1000
                );
                JobDetail jd = new JobDetail("InnerJobDetail" + counter++, "InnerJobGroup", InnerJob.class);
                scheduler.scheduleJob(jd, simpleTrigger);
            } catch (Exception e){
                LOG.fatal("something went wrong while scheduling the inner job", e);
            }
        }
    }

    public static class InnerJob implements Job {
        @Override
        public void execute(JobExecutionContext context) throws JobExecutionException {
            LOG.info("InnerJob is running. The time at the beep is: " + new Date());
        }
    }

    public void setScheduler(Scheduler scheduler) {
        QuartzSSCCE.scheduler = scheduler;
    }
}

此类从Scheduler获取SchedulerFactoryBean个实例。在我的代码的其他地方,我实例化QuartzSSCCE的实例,如下所示:

QuartzSSCCE quartzSSCCE = new QuartzSSCCE();
quartzSSCCE.scheduleOuterJob();

我最终在日志中看到“关于尝试安排OuterJob”和“运行OuterJob”的条目,但似乎InnerJob永远不会运行。我通过调用TriggerUtils.makeImmediateTrigger()以及SimpleTrigger返回的触发器尝试了此操作。

为什么我的InnerJob不会运行?

1 个答案:

答案 0 :(得分:1)

首先,我的英语很差!

Quartz使用反射创建作业实例,如代码所示:

JobDetail jd = new JobDetail("InnerJobDetail" + counter++, "InnerJobGroup", InnerJob.class);

但是嵌套类不能通过反射直接创建实例,因此不会触发InnerJob。