我想按顺序执行多个作业。我正在尝试以下方式但不知何故它不会被使用cron作业触发。
我不确定它是否是一种正确的实施方法。 有人可以指导我吗?
谢谢..
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobChainingJobListener jobListener =new JobChainingJobListener("ChainListener");
JobDetail job1 = JobBuilder.newJob(MyJob1.class)
.withIdentity(MyJob1.class.getName()).
withDescription(MyJob1.class.getName())
.build();
JobDetail job2 = JobBuilder.newJob(MyJob2.class)
.withIdentity(MyJob2.class.getName()).
withDescription(MyJob2.class.getName())
.build();
JobDetail job3 = JobBuilder.newJob(MyJob3.class)
.withIdentity(MyJob3.class.getName()).
withDescription(MyJob3.class.getName())
.build();
Trigger jobTrigger = TriggerBuilder.newTrigger()
.withIdentity("MyTrigger")
.withSchedule(CronScheduleBuilder.cronSchedule(cronTrigger)).build();
scheduler.scheduleJob(job1, jobTrigger);
scheduler.addJob(job2, true);
scheduler.addJob(job3, true);
jobListener.addJobChainLink(job1.getKey(), job2.getKey());
jobListener.addJobChainLink(job2.getKey(), job3.getKey());
scheduler.getListenerManager().addJobListener(jobListener);
scheduler.start();
答案 0 :(得分:2)
将组名添加到作业并触发可以执行它的标识。 这是正确的代码:
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
JobChainingJobListener jobListener =new JobChainingJobListener("ChainListener");
JobDetail job1 = JobBuilder.newJob(MyJob1.class)
.withIdentity(MyJob1.class.getName(),"BatchJobGroup").
withDescription(MyJob1.class.getName())
.build();
JobDetail job2 = JobBuilder.newJob(MyJob2.class)
.withIdentity(MyJob2.class.getName(),"BatchJobGroup").
withDescription(MyJob2.class.getName())
.build();
JobDetail job3 = JobBuilder.newJob(MyJob3.class)
.withIdentity(MyJob3.class.getName(),"BatchJobGroup").
withDescription(MyJob3.class.getName())
.build();
Trigger jobTrigger = TriggerBuilder.newTrigger()
.withIdentity("MyTrigger","BatchJobGroup").
.withSchedule(CronScheduleBuilder.cronSchedule("* * * * *")).build();
scheduler.scheduleJob(job1, jobTrigger);
scheduler.addJob(job2, true);
scheduler.addJob(job3, true);
jobListener.addJobChainLink(job1.getKey(), job2.getKey());
jobListener.addJobChainLink(job2.getKey(), job3.getKey());
scheduler.getListenerManager().addJobListener(jobListener);
scheduler.start();
答案 1 :(得分:0)
我使用Sachin答案来创建这个答案:
public class Main {
public static void main(String[] args) throws SchedulerException {
JobDetail jobA = JobBuilder.newJob(JobA.class).build();
JobDetail jobB = JobBuilder.newJob(JobB.class).storeDurably(true).build();
JobChainingJobListener jobListener = new JobChainingJobListener("ChainListener");
jobListener.addJobChainLink(jobA.getKey(), jobB.getKey());
Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler();
scheduler.scheduleJob(jobA, TriggerBuilder.newTrigger().startNow().build());
scheduler.addJob(jobB, true);
scheduler.getListenerManager().addJobListener(jobListener);
scheduler.start();
}
public static class JobA implements Job {
@Override
public void execute(JobExecutionContext context) {
System.out.println("Job A running...");
}
}
public static class JobB implements Job {
@Override
public void execute(JobExecutionContext context) {
System.out.println("Job B running...");
}
}}
在GitHub上的工作示例:https://github.com/topera/poc-quartz-chain