Karthick88it
Karthick88it

Reputation: 623

quartz scheduler get next scheduler time if the application stopped

Currently i am using quartz scheduler in my application for executing scheduler. My application is running on a tomcat server. If the server was started then the scheduler will start firing the task based on the start time provided.

My problem is if the task was scheduled to execute in interval basis for every 10 minutes and the tomcat was stopped for some reasons and it was resumed back after some two days then the quartz scheduler expression to get the nextvalid time is taking based on the start time provided, hence the scheduler jobs was back dated jobs..

Real time example :

The task was set to interval with the below cron expression for every 10 minutes.

0 0/10 * 1/1 * ? *

The start time for the job was

"2017-04-08 21:46:00"

But the application/tomcat server was started only on 10th april and after executing the scheduler the start time was changed to below format

"2017-04-08 21:56:00"

Only the time was incremented to 10 minutes and the date was not changed to 10th of April(today).

Currently i am using the below code to fetch the next date/time for the scheduler

    CronExpression exp = new CronExpression(schedulerConfig.getCronexpression());

  NextschedulerDate = exp.getNextValidTimeAfter(Currentstartdate);

The same case is not working for weekday option. Kindly assist

Upvotes: 2

Views: 3731

Answers (2)

walen
walen

Reputation: 7273

If Currentstartdate is "2017-04-08 21:46:00" and CronExpression exp is "0 0/10 * 1/1 * ? *", then NextschedulerDate is, of course, "2017-04-08 21:56:00". That is correct, because that is the first valid date that satisfies the cron expression after the date you provided.

If you want to know the first valid date after the current date for the given cron expression, you can do NextschedulerDate = exp.getNextValidTimeAfter(new Date());.

But all the confusion comes from using startDate. That is the date when the job was scheduled to run, not the date it actually run. You probably mean to use getFireTime (for a running job), getNextFireTime and getPreviousFireTime (any job) to get the actual execution date.

Upvotes: 2

John Vottero
John Vottero

Reputation: 925

Call getNextValidTimeAfter in a loop until NextScheduledDate is in the future.

Upvotes: 1

Related Questions