2017-02-28 3 views
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 + " * * * ?"; 

} 

} 

그래서 여기에 내가 (한 매 분마다 5 기타를 실행하는 실제 경우) 각 분을 실행하는 두 개의 작업을 예약하고 작업 클래스

@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."); 
    } 

} 

이며, 이것은 출력 나는 점점 오전 :

Job started 
Job sleeping 30s... 
Job started 
Job sleeping 30s... 
Job finished. 
Job finished. 

그래서 모두 작업이 병렬로 실행되고, 작업 1의 대기 작업 2가 실행 전에 완료하기 위해 순차적 인 순서가 나에게이 순서를 줄 것이기 때문에

Job started 
Job sleeping 30s... 
Job finished. 
Job started 
Job sleeping 30s... 
Job finished. 

는 왜이 발생되지 않는 이유는 무엇입니까? 문서에서

답변

0

:

@DisallowConcurrentExecution :

인스턴스 기반-에있는 JobDetail의 정의 동시에 실행되는 여러 인스턴스가 없어야합니다 하나 (같은 작업 클래스를 표시 주석은 - 또는 다른 말로 JobKey에 기초하여).

JobKey에는 이름과 그룹 당신의 예에서

이름이 동일하지 않습니다 모두 구성되어 있습니다, 그래서 이것은 두 개의 서로 다른 작업입니다.

DisallowConcurrentExecution는 다른 작업 1 번호의 thread2를 실행하기 전에 작업 1 #의 thread1이 완료되는 것을 보장한다.