Alex Pi
Alex Pi

Reputation: 826

Cron expression for every five minutes in the next n hours?

I know every five minutes is:

0 0/5 * * * *

But how do I limit the number hours for this to happen?

Example: Every five minutes for the next 10 hours.

Upvotes: 16

Views: 54024

Answers (3)

Khurram
Khurram

Reputation: 181

Use the range operator to specify the hours. For example, for running the job every five minutes for 10 hours starting at 2 am:

0 0/5 2-12 * * *

Here is an excellent tutorial on Cron expression and operators: http://www.quartz-scheduler.org/documentation/quartz-2.x/tutorials/crontrigger.html

Edit: 10/3/2016: Updated the link.

Upvotes: 13

darrenmc
darrenmc

Reputation: 1781

If you can programatically access the CronTrigger that is running your cron expression then you can call the methods setStartTime and setEndTime with the computed time range.

Alternatively you could construct the cron expression on the fly and specify a computed hour range.

For example if you are starting your server at 9am you could create this expression at runtime 0 0/5 9-19 * * *

Upvotes: 2

Morfic
Morfic

Reputation: 15508

I think should be able to define a trigger that can repeat every hour until a certain time:

import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

...

Trigger myTrigger = newTrigger()
                    .withIdentity('myUniqueTriggerID")
                    .forJob(myJob)
                    .startAt(startDate)
                    .endAt(endDate)
                    .withSchedule(simpleSchedule().withIntervalInHours(1));

...

scheduler.scheduleJob(myJob, myTrigger);

Upvotes: 6

Related Questions