1
您好我有兩個石英工作實例,我不想並行運行,我簡化了下面的示例中的代碼,以顯示與我的期望不符的代碼。石英@DisallowConcurrentExecution不能正常工作
public class QuartzTest {
public static void main(String[] args) throws SchedulerException {
SchedulerFactory schedulerFactory = new StdSchedulerFactory();
Scheduler scheduler = schedulerFactory.getScheduler();
scheduler.start();
JobDetail job1 = newJob(TestJob.class).withIdentity("job1", "group1").build();
CronTrigger trigger1 = newTrigger().withIdentity("trigger1", "group1").startAt(new Date()).withSchedule(cronSchedule(getCronExpression(1))).build();
scheduler.scheduleJob(job1, trigger1);
JobDetail job2 = newJob(TestJob.class).withIdentity("job2", "group1").build();
CronTrigger trigger2 = newTrigger().withIdentity("trigger2", "group1").startAt(new Date()).withSchedule(cronSchedule(getCronExpression(1))).build();
scheduler.scheduleJob(job2, trigger2);
for (int i = 0; i < 5; i++) {
System.out.println(trigger1.getNextFireTime());
System.out.println(trigger2.getNextFireTime());
try {
Thread.sleep(1 * 60 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private static String getCronExpression(int interval) {
return "0 */" + interval + " * * * ?";
}
}
這是作業類
@DisallowConcurrentExecution
public class TestJob implements Job {
@Override
public void execute(JobExecutionContext context) throws JobExecutionException {
System.out.println("Job started");
System.out.println("Job sleeping 30s...");
try {
Thread.sleep(30 * 1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Job finished.");
}
}
所以我在這裏調度兩個作業運行每分鐘(在一個正在運行的每一分鐘對方每隔5的實際情況),這是輸出我得到:
Job started
Job sleeping 30s...
Job started
Job sleeping 30s...
Job finished.
Job finished.
因此,無論工作並行運行,因爲連續的序列,其中JOB1等待作業2跑步前完成會給我這個序列
Job started
Job sleeping 30s...
Job finished.
Job started
Job sleeping 30s...
Job finished.
那麼,爲什麼這不會發生?