Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On quartz scheduler, clarification needed

Few separate questions on Quartz

  1. If i want event to be executed once, is it sufficient to

    trigger.setRepeatCount(0);
    
  2. Considering this snippet. Event runs, when scheduled time is "before now" and fails to be executed when scheduled time is in the future

    JobDetail job = new JobDetail();
    job.setName(eventType.toString() + " event");
    job.setJobClass(Action.class);
    
    SimpleTrigger trigger = new SimpleTrigger();
    trigger.setStartTime(new Date(momentInTime.inMillis()));
    trigger.setName("trigger");
    
    trigger.setRepeatInterval(repeatFrequency.inMillis());
    trigger.setRepeatCount(SimpleTrigger.REPEAT_INDEFINITELY);
    
    scheduleManager.getScheduler().scheduleJob(job, trigger);
    

Using the code above, i observe the following

Time now         : 1352410780356
Will execute at  : 1352410840356  // 1 min interval
Execution starts : 1352410840368

The difference between now and the time it was scheduled to execute is exactly 1 minute ..

Ok .. trying a bigger interval

Time now         : 1352411061156
Will execute at  : 1352411301156 // 3 min interval
Execution starts : 1352411301165

Again .. the difference is precisely 3 minutes, as

Execution should at a moment in time

   new MomentInTime(new DayOfMonth(8), new HourOfDay(15), new MinuteOfHour(48));

It actually starts (appears to start, i should say) as an offset (even offset at that) between current time and requested time.

It appears that if it's 10:43:25 and i want a job to start at 10:45:00, it would figure out that there are 2 even minutes difference and would schedule the job at 10:45:25.

What's causing this?

Meanwhile,

public MomentInTime(DayOfMonth day, HourOfDay hour, MinuteOfHour min) {
    calendarInstance = Calendar.getInstance();

    // Get current year and month
    int year = calendarInstance.get(Calendar.YEAR);
    int month = calendarInstance.get(Calendar.MONTH);

    calendarInstance.set(year, month, day.getValue(), hour.getValue(), min.getValue());
    System.out.println("Time now       : " + System.currentTimeMillis());
    System.out.println("Will execute at: " + calendarInstance.getTimeInMillis());
}

public long inMillis() {
    return calendarInstance.getTimeInMillis();
}
like image 989
James Raitsev Avatar asked Nov 03 '22 11:11

James Raitsev


1 Answers

A 1 line fix

    calendarInstance.set(Calendar.MILLISECOND, 0);

Done.

like image 58
James Raitsev Avatar answered Nov 09 '22 13:11

James Raitsev