Java - Quartz CronSchedule不适用于开始日期和结束日期

时间:2017-10-20 13:43:02

标签: java quartz-scheduler quartz

当我将startDate和endDate设置为Trigger时,ChronSchedule仅在作业上调用execute方法一次。

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方法中,我放了一个print:

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

    }

但结果令人不快:

Executing job. The next: null

我错了什么?

1 个答案:

答案 0 :(得分: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);