我正在使用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;
}
}
此类从 SchedulerFactoryBean
获取 Scheduler
实例 . 在我的代码的其他地方,我实例化 QuartzSSCCE
的实例,如下所示:
QuartzSSCCE quartzSSCCE = new QuartzSSCCE();
quartzSSCCE.scheduleOuterJob();
我最终看到日志中的条目为"About to try to schedule OuterJob"和"running OuterJob"但看起来InnerJob永远不会运行 . 我已尝试使用从 TriggerUtils.makeImmediateTrigger()
调用返回的触发器以及 SimpleTrigger
.
为什么我的InnerJob不会运行?
1 回答
首先,我的英语很差!
Quartz使用反射来创建作业实例,如您的代码所示:
但是嵌套类不能通过反射直接创建实例,因此不会触发InnerJob .