public class CronTrigger extends java.lang.Object implements ZonedTrigger
Cron-based Trigger
implementation, which supports 5 or 6 fields
delimited by a single space character, plus a ZoneId
.
Basic cron syntax is supported. For more advanced scenarios, you can
subclass this implementation or combine multiple CronTrigger
instances in a Trigger
implementation of your own.
seconds (optional) | 0-59, *. When absent, 0 is assumed |
minutes | 0-59, * |
hours | 0-23, * |
dayOfMonth | 0-31, *, L |
month | 1-12, JAN-DEC, January-December, * |
dayOfWeek | SUN-SAT, Sunday-Saturday, 0-7, *.
0 and 7 both represent Sunday: 0 to designate the first day of the week,
and 7 for consistency with DayOfWeek . |
, |
delimits lists for all fields. For example, MON,WED,FRI or MAY,SEP |
- |
delimits ranges for all fields. For example, MON-FRI or 9-17 |
/ |
specifies a repeating increment for all fields except dayOfWeek.
For example, 6/7 for the hours field equates to 6,13,20 . |
# |
specifies an ordinal day of week. For example,
FRI#1,SAT#L is the first Friday and last Saturday of the month.
# cannot be used within ranges (- ) and increments (/ ). |
* |
indicates any value is permitted. |
L |
indicates the last day of the month.
2L indicates the second-to-last day, and so forth. |
0 * * * * |
every hour at the top of the hour |
0 9-17 * * MON-FRI |
weekdays from 9am to 5pm, at the top of each hour |
0 13/3 * MAY-SEP SAT,SUN |
weekends from May to September, every 3 hours, starting at 1pm |
30 10 * APR,AUG TUE#2,TUE#L |
second and last Tuesdays of April and August at 10:30am |
15 22 4 10,20,L * * |
4:22:15 AM on the 10th, 20th, and last day of every month |
0 8-11,13-16 2L JAN-MAR * |
8AM-11AM and 1PM-4PM on the second-to-last day of January, February, and March |
A constructor is provided that accepts a cron expression such as the above and a timezone id. For example,
trigger = new CronTrigger("0 7 * SEP-MAY MON-FRI", ZoneId.of("America/New_York"));
Another constructor allows cron fields to be specified in a fluent manner, in any order. For example,
trigger = new CronTrigger(ZoneId.of("America/Los_Angeles")) .months(Month.DECEMBER) .daysOfMonth(24) .hours(16, 18);
The getNextRunTime
method of this trigger
determines the next run time based on the cron schedule.
The skipRun
method always returns false
unless overridden by a subclass.
Methods of this class that configure the cron expression fields are not thread safe. It is the
responsibility of the caller to ensure that initialization of the CronTrigger
happens before it is supplied to a ManagedScheduledExecutorService
and that the
CronTrigger
is not subsequently modified.
You can subclass CronTrigger
to provide for more complex logic, such as in the following
example of combining two triggers to schedule twice-a-month payroll on the 15th and last day of month
or the prior Fridays when the former fall on a weekend:
public class PayrollTrigger extends CronTrigger { private final CronTrigger fridaysBeforeWeekendPayrollDay; PayrollTrigger() { // Every 15th and last day of the month that is a weekday, super("0 10 15,L * MON-FRI", ZoneId.of("America/Chicago")); // Every 13th, 14th, third-to-last, and second-to-last day of the month that is a Friday, fridaysBeforeWeekendPayrollDay = new CronTrigger( "0 10 13,14,3L,2L * FRI", getZoneId()); } public ZonedDateTime getNextRunTime(LastExecution lastExec, ZonedDateTime scheduledAt) { ZonedDateTime time1 = super.getNextRunTime(lastExec, scheduledAt); ZonedDateTime time2 = fridaysBeforeWeekendPayrollDay.getNextRunTime(lastExec, scheduledAt); return time1.isBefore(time2) ? time1 : time2; } }
Constructor and Description |
---|
CronTrigger(java.lang.String cron,
java.time.ZoneId zone)
Constructor that accepts a cron expression.
|
CronTrigger(java.time.ZoneId zone)
Constructor for the fluent configuration pattern.
|
Modifier and Type | Method and Description |
---|---|
CronTrigger |
daysOfMonth(int... d)
Configure the day-of-month cron field, overwriting any previous value for day-of-month.
|
CronTrigger |
daysOfMonth(java.lang.String d)
Configure the day-of-month cron field, overwriting any previous value for day-of-month.
|
CronTrigger |
daysOfWeek(java.time.DayOfWeek... d)
Configure the day-of-week cron field, overwriting any previous value for day-of-week.
|
CronTrigger |
daysOfWeek(java.lang.String d)
Configure the day-of-week cron field, overwriting any previous value for day-of-week.
|
java.time.ZonedDateTime |
getNextRunTime(LastExecution lastExecutionInfo,
java.time.ZonedDateTime taskScheduledTime)
Using the cron schedule, and based on the end of the most recent execution
(or absent that, the initial scheduling time), retrieve the next time
that the task should run after.
|
java.time.ZoneId |
getZoneId()
Returns the timezone to use for
ZonedDateTime that is supplied to the
getNextRunTime and
skipRun methods. |
CronTrigger |
hours(int... h)
Configure the hours cron field, overwriting any previous value for hours.
|
CronTrigger |
hours(java.lang.String h)
Configure the hours cron field, overwriting any previous value for hours.
|
CronTrigger |
minutes(int... m)
Configure the minutes cron field, overwriting any previous value for minutes.
|
CronTrigger |
minutes(java.lang.String m)
Configure the minutes cron field, overwriting any previous value for minutes.
|
CronTrigger |
months(java.time.Month... m)
Configure the month cron field, overwriting any previous value for month.
|
CronTrigger |
months(java.lang.String m)
Configure the months cron field, overwriting any previous value for months.
|
protected java.time.ZonedDateTime |
next(java.time.ZonedDateTime from)
Advance to the next date/time according to the cron schedule.
|
CronTrigger |
seconds(int... s)
Configure the seconds cron field, overwriting any previous value for seconds.
|
CronTrigger |
seconds(java.lang.String s)
Configure the seconds cron field, overwriting any previous value for seconds.
|
java.lang.String |
toString()
Readable representation of the CronTrigger, which displays fields in list form
or with the * character for brevity.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
getNextRunTime, skipRun, skipRun
public CronTrigger(java.lang.String cron, java.time.ZoneId zone)
cron
- cron expression.zone
- timezone ID to use for ZonedDateTime
that is supplied to
getNextRunTime
and
skipRun
methods.
Null indicates to use the system default.public CronTrigger(java.time.ZoneId zone)
zone
- timezone ID to use for ZonedDateTime
that is supplied to
getNextRunTime
and
skipRun
methods.
Null indicates to use the system default.public java.time.ZonedDateTime getNextRunTime(LastExecution lastExecutionInfo, java.time.ZonedDateTime taskScheduledTime)
getNextRunTime
in interface ZonedTrigger
lastExecutionInfo
- information about the last execution of the task.
This value will be null if the task has not yet run.taskScheduledTime
- the date/time at which the
ManagedScheduledExecutorService.schedule
method was invoked to schedule the task.java.time.DateTimeException
- if a next time cannot be determined from the cron expression.public final java.time.ZoneId getZoneId()
ZonedDateTime
that is supplied to the
getNextRunTime
and
skipRun
methods.getZoneId
in interface ZonedTrigger
public CronTrigger daysOfMonth(int... d)
d
- one or more day numbers ranging from 1 to 31.public CronTrigger daysOfMonth(java.lang.String d)
d
- dayOfMonth cron field. For example, 15,L
.public CronTrigger daysOfWeek(java.time.DayOfWeek... d)
d
- one or more days of the week.public CronTrigger daysOfWeek(java.lang.String d)
d
- dayOfWeek cron field. For example, MON-FRI,SAT#L
.public CronTrigger hours(int... h)
h
- one or more hour values ranging from 0 to 23.public CronTrigger hours(java.lang.String h)
h
- hours cron field. For example, 9-17
for 9am to 5pm.public CronTrigger minutes(int... m)
m
- one or more minute values ranging from 0 to 59.public CronTrigger minutes(java.lang.String m)
m
- minutes cron field. For example, 5/10
for 10 minute intervals
starting at 5 minutes after the hour (:05, :15, :25, :35, :45, :55).public CronTrigger months(java.time.Month... m)
m
- one or more months.public CronTrigger months(java.lang.String m)
m
- months cron field. For example, SEP-NOV,FEB-MAY
.public CronTrigger seconds(int... s)
s
- one or more seconds values ranging from 0 to 59.public CronTrigger seconds(java.lang.String s)
s
- seconds cron field. For example, 30
.public java.lang.String toString()
For example,
CronTrigger@89abcdef seconds 0, minutes 0, hours 9, *, months 3,6,9,12, SAT#2,SAT#4
toString
in class java.lang.Object
protected java.time.ZonedDateTime next(java.time.ZonedDateTime from)
from
- the date/time from which to compute the next time.