2017-10-20 166 views
0

當我將startDate和endDate設置爲觸發器時,ChronSchedule只對該作業調用execute方法一次。Java - Quartz CronSchedule不適用於開始日期和結束日期

JobDetail job = JobBuilder.newJob(service) 
        .withIdentity(batchService.getName(), CommonConstants.BATCH_JOB_GROUP).build(); 
      Trigger trigger = TriggerBuilder 
        .newTrigger() 
        .withIdentity(batchService.getName(), CommonConstants.BATCH_JOB_GROUP) 
        .startAt(startDate) 
        .endAt(endDate) 
        .withSchedule(
          CronScheduleBuilder.cronSchedule("0/5 * * * * ?")) 
        .build(); 
      System.out.println("##########" + trigger.getEndTime()); 
      System.out.println("##########" + trigger.getStartTime()); 

正如你所看到的,我在觸發器上放了一些打印以獲取開始日期和結束日期。 這似乎是正確的:

##########Fri Oct 20 16:00:00 CEST 2017 
##########Fri Oct 20 15:00:00 CEST 2017 

在execute方法,我把打印:

public void execute(JobExecutionContext context) throws JobExecutionException 
    { 
     System.out.println("Executing job. The next: " + context.getNextFireTime()); 

    } 

但結果是unconfortable:

Executing job. The next: null 

什麼有我錯了?

回答

0

我不知道CronsScheduler或CronsTrigger,但你可以嘗試下面的代替CronScheduleBuilder。

import org.quartz。* Packages。

JobDetail jobDetail = JobBuilder.newJob(MYJOB.class).withIdentity(jobName).withDescription("TryThis").build(); 

CalendarIntervalTriggerImpl trigger = new CalendarIntervalTriggerImpl("triggername", startTime, endTime, 
           DateBuilder.IntervalUnit.valueOf("MINUTE"), frequency); 

// Try to keep scheduler object as singultant object. 
// OR it'll also work fine if you start scheduler it multiple times with diffrebt scheduler object for JDBCJob store. 

SchedulerFactory sf = new StdSchedulerFactory(); 

Scheduler scheduler = sf.getScheduler(); 
scheduler.start(); 

scheduler.scheduleJob(jobDetail, trigger); 
相關問題