简体   繁体   中英

Quartz CronTrigger is not firing at the specified startAt() time

I have a scenario where I need to schedule a job which has to be execute daily at a specific time. When I schedule it with specific time as the start time for scheduler the quartz won't trigger the job at the set start time instead it would trigger at the next cycle ie after 24 hrs delay.Even on checking the the nextFireTime, we get a day's delay.

For Eg: I need to schedule a job daily to run at 6 pm in the evening. And start it at 5 pm Today (27th March 2018).The job doesn't start and nextFireTime is 6pm 28th March 2018.


Code snippet :


Date startDateTime = new Date(scheduler.getStartDateTime());
Calendar calendar = GregorianCalendar.getInstance();
calendar.setTime(startDateTime);
int hours = calendar.get(Calendar.HOUR_OF_DAY);
int minutes = calendar.get(Calendar.MINUTE);
CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(scheduleTriggerName, schdeuleGroupName).startAt(startDateTime).withSchedule(dailyAtHourAndMinute(hours, minutes)).build();
JobDetail jobDetail = this.getJobDetail(schdeuleJobName, schdeuleGroupName);

Scheduler configuration for spring


SchedulerFactoryBean schedulerFactoryBean= new SchedulerFactoryBean();

QuartzAutowireBeanFactory jobFactory = new QuartzAutowireBeanFactory();
jobFactory.setApplicationContext(applicationContext);
schedulerFactoryBean.setJobFactory(jobFactory);
schedulerFactoryBean.scheduleJob(jobDetail, trigger)// scheduling the job

Solution

One liner:

Cron only handles 1-minute resolutions

The starttime that which was passed to the startAt() function was a timestamp till milliseconds and cron does support till minutes.

so the simple solution was to use the calendar to set the minutes and seconds as zero.

calendar.setTime(startDateTime);
            calendar.set(Calendar.SECOND, 0); // this was the solution
            calendar.set(Calendar.MILLISECOND, 0); // this was the solution
            int hours = calendar.get(Calendar.HOUR_OF_DAY);
            int minutes = calendar.get(Calendar.MINUTE);
            trigger = TriggerBuilder.newTrigger();
            trigger.startAt(calendar.getTime()).withSchedule(dailyAtHourAndMinute(hours, minutes));

Detailed One: Finally I got the reason for the behavior, while debugging I saw that it was setting the delay of 24 hours but when one would print the time it would be in hh:mm:00 format I mean the output would set the seconds parts as 00 as default , so the problem was the starttime that was passed as parameter was a timestamp through the UI consisting of seconds and milliseconds so after reading on the Cron format I came to know that it supported till minutes resolutions so wherever it use to get the timestamp the startAt(startDateTime) in the

CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(scheduleTriggerName, schdeuleGroupName).startAt(startDateTime).withSchedule(dailyAtHourAndMinute(hours, minutes)).build();

it would calculate the next run by skipping the seconds and milliseconds part.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM