2015-04-05 140 views
0

我有一個Quartz作業被設置爲每隔一段時間運行一次。不過,我想阻止這項工作的併發執行。我將@DisallowConcurrentExecution註釋添加到表示此作業的類中,但似乎沒有任何作用。Quartz @DisallowConcurrentExecution註解似乎沒有工作

以下是我創建作業並觸發存儲到JDBC存儲區的方法。觸發器設置爲每5秒運行一次。

JobKey jobKey = new JobKey("sample_job", "default_group"); 
    String triggerIdentity = jobKey.getName() + "_trigger"; 
    Scheduler scheduler = StdSchedulerFactory.getDefaultScheduler(); 
    if (!scheduler.checkExists(jobKey)) { 

     JobDetail jobDetail = JobBuilder.newJob(SampleJob.class).withIdentity(jobKey).build(); 
     Trigger trigger = newTrigger() 
      .withIdentity(triggerIdentity, jobKey.getGroup()) 
      .startAt(new Date()) 
      .withSchedule(simpleSchedule() 
      .withIntervalInSeconds(5) 
      .repeatForever()) 
      .build(); 
     scheduler.scheduleJob(jobDetail, trigger); 

    } 
    scheduler.start(); 

這是我創建的示例作業類。它基本上啓動了一個計時器,它在10秒內完成並打印一條消息。

@DisallowConcurrentExecution 
public class SampleJob implements Job { 

    public SampleJob() { 
    } 

    @Override 
    public void execute(JobExecutionContext jobExcutionContext) { 

     final Date now = new Date(); 
     System.out.println("Starting sample job: " + now); 
     Timer t = new Timer(); 
     t.schedule(new TimerTask() { 
      @Override 
      public void run() { 
       System.out.println("Done: " + now); 
       this.cancel(); 
      } 
     }, 10000L); 

    } 
} 

當我運行此我看到下面的輸出,這對我來說意味着作業觸發,即使以前的執行尚未完成,每5秒關機。我誤解了@DisallowConcurrentExecution的含義,還是我的測試不正確?

Starting sample job: Sat Apr 04 22:45:06 EDT 2015 
Starting sample job: Sat Apr 04 22:45:11 EDT 2015 
Done: Sat Apr 04 22:45:01 EDT 2015 
Starting sample job: Sat Apr 04 22:45:16 EDT 2015 
Done: Sat Apr 04 22:45:06 EDT 2015 
Starting sample job: Sat Apr 04 22:45:21 EDT 2015 
Done: Sat Apr 04 22:45:11 EDT 2015 
Done: Sat Apr 04 22:45:16 EDT 2015 
Starting sample job: Sat Apr 04 22:45:26 EDT 2015 
Starting sample job: Sat Apr 04 22:45:31 EDT 2015 
Done: Sat Apr 04 22:45:21 EDT 2015 

回答

0

原來,我是在反思這個問題。一個計時器看起來像是從一個單獨的線程運行。所以就工作而言,它是在計時器完成之前完成的。一個簡單的Thread.sleep(10000),而不是定時器的技巧。