davyjones
davyjones

Reputation: 305

Quartz Cron Expression for running a job every 5 hours from a given time

I am using Quartz library for scheduling. I have to schedule a job every 5 (could be variable) hours from a given time.

I tried using the following expression -

0 0 12/5 1/1 * ? *

I checked the output of future runs at Cronmaker.

Schedule Start Time -> Tuesday, June 21, 2017 10:30 AM

Future runs -

  1. Wednesday, June 21, 2017 12:00 PM
  2. Wednesday, June 21, 2017 5:00 PM
  3. Wednesday, June 21, 2017 10:00 PM
  4. Thursday, June 22, 2017 12:00 PM

The expression does what it is asked but I was hoping that the 4th run would be 5 hours in addition to the 3rd run i.e something along these lines -

  1. Wednesday, June 21, 2017 12:00 PM
  2. Wednesday, June 21, 2017 5:00 PM
  3. Wednesday, June 21, 2017 10:00 PM
  4. Thursday, June 22, 2017 3:00 AM

The 4th run goes to next day's 12:00 PM trigger. I want it to be added to the last run's time. Is there any way via which this can be achieved via the cron-expression?

Upvotes: 2

Views: 2645

Answers (1)

walen
walen

Reputation: 7273

CronSchedule is not the best suited one for what you want to do.

Use SimpleSchedule instead:

trigger = newTrigger()
    .withIdentity("yourJobName", "yourJobGroup")
    .withSchedule(simpleSchedule()
        .withIntervalInHours(5) // every 5 hours
        .repeatForever()) // keep going in intervals of 5h
    .startAt(dateOf(12, 0, 0)) // start at 12:00 PM
    .build();

You can find more examples in the official documentation.

Upvotes: 2

Related Questions