我使用quartz jdbcdatastore来安排使用spring的作业。在它的帮助下 @DisallowConcurrentExecution我们可以实现某种类型的并发,但我希望在调度相同作业的2个触发器之间实现并发。
基本上,当触发一个触发器并运行作业时,另一个触发器不能运行相同的作业。
提前致谢。
这是我的代码,
Scheduler scheduler = new StdSchedulerFactory().getScheduler();
JobKey jobKey1 = new JobKey("job1","group1");
JobDetail jobDetail = JobBuilder.newJob(MyJob.class).withIdentity(jobKey1).build();
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity("trigger1", "triggergroup1")
.withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?")).build();
if(scheduler.checkExists(trigger.getKey())) {
scheduler.rescheduleJob(trigger.getKey(), trigger);
}else {
scheduler.scheduleJob(jobDetail, trigger);
}
CronTrigger trigger1 = TriggerBuilder.newTrigger().withIdentity("trigger2", "triggergroup2")
.withSchedule(CronScheduleBuilder.cronSchedule("0/1 * * * * ?")).build();
if(scheduler.checkExists(trigger1.getKey())) {
scheduler.rescheduleJob(trigger1.getKey(), trigger1);
}else {
scheduler.scheduleJob(jobDetail, trigger1);
}
scheduler.start();
,作业类中的代码是,
System.out.println("Thread Id "+Thread.currentThread().getId() +" "+context.getJobDetail().getKey());
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Stop Thread Id "+Thread.currentThread().getId());
输出是:
Thread Id 20 group1.job1
Thread Id 21 group2.job2
Stop Thread Id 20
Stop Thread Id 21