Class CronPattern
- java.lang.Object
-
- com.oracle.coherence.concurrent.executor.util.CronPattern
-
public class CronPattern extends Object
A UNIX crontab-like pattern is a string split in five space separated parts. Each part is intended as:
- Minutes sub-pattern. During which minutes of the hour should the task been launched? The values range is from 0 to 59.
- Hours sub-pattern. During which hours of the day should the task been launched? The values range is from 0 to 23.
- Days of month sub-pattern. During which days of the month should the task been launched? The values range is from 1 to 31. The special value L can be used to recognize the last day of month.
- Months sub-pattern. During which months of the year should the task been launched? The values range is from 1 (January) to 12 (December), otherwise this sub-pattern allows the aliases "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov" and "dec".
- Days of week sub-pattern. During which days of the week should the task been launched? The values range is from 0 (Sunday) to 6 (Saturday), otherwise this sub-pattern allows the aliases "sun", "mon", "tue", "wed", "thu", "fri" and "sat".
The star wildcard character is also admitted, indicating "every minute of the hour", "every hour of the day", "every day of the month", "every month of the year" and "every day of the week", according to the sub-pattern in which it is used.
Once the scheduler is started, a task will be launched when the five parts in its scheduling pattern will be true at the same time.
Some examples:
5 * * * *
This pattern causes a task to be launched once every hour, at the begin of the fifth minute (00:05, 01:05, 02:05 etc.).* * * * *
This pattern causes a task to be launched every minute.* 12 * * Mon
This pattern causes a task to be launched every minute during the 12th hour of Monday.* 12 16 * Mon
This pattern causes a task to be launched every minute during the 12th hour of Monday, 16th, but only if the day is the 16th of the month.Every sub-pattern can contain two or more comma separated values.
59 11 * * 1,2,3,4,5
This pattern causes a task to be launched at 11:59AM on Monday, Tuesday, Wednesday, Thursday and Friday.Values intervals are admitted and defined using the minus character.
59 11 * * 1-5
This pattern is equivalent to the previous one.The slash character can be used to identify step values within a range. It can be used both in the form */c and a-b/c. The subpattern is matched every c values of the range 0,maxvalue or a-b.
*/5 * * * *
This pattern causes a task to be launched every 5 minutes (0:00, 0:05, 0:10, 0:15 and so on).3-18/5 * * * *
This pattern causes a task to be launched every 5 minutes starting from the third minute of the hour, up to the 18th (0:03, 0:08, 0:13, 0:18, 1:03, 1:08 and so on).*/15 9-17 * * *
This pattern causes a task to be launched every 15 minutes between the 9th and 17th hour of the day (9:00, 9:15, 9:30, 9:45 and so on... note that the last execution will be at 17:45).All the fresh described syntax rules can be used together.
* 12 10-16/2 * *
This pattern causes a task to be launched every minute during the 12th hour of the day, but only if the day is the 10th, the 12th, the 14th or the 16th of the month.* 12 1-15,17,20-25 * *
This pattern causes a task to be launched every minute during the 12th hour of the day, but the day of the month must be between the 1st and the 15th, the 20th and the 25, or at least it must be the 17th.Finally it lets you combine more scheduling patterns into one, with the pipe character:
0 5 * * *|8 10 * * *|22 17 * * *
This pattern causes a task to be launched every day at 05:00, 10:08 and 17:22.Hourly 5 * * * *
This pattern causes a task to be launched at 5 minutes past every hour.Daily 0 5 * * *
This pattern causes a task to be launched every day at 05:00.Yearly 0 5 1 1 *
This pattern causes a task to be launched at 1st month, 1st day, 05:00 every year.Every 5 minutes */5 * * * *
This pattern causes a task to be launched every 5 minutes (asterisk followed by slash, followed by the 5 minute interval).- Since:
- 21.12
- Author:
- Adapted from the cron4j scheduler by Carlo Pelliccia, lh, bo
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static interfaceCronPattern.ValueParserDefinition for a value parser.
-
Field Summary
Fields Modifier and Type Field Description protected static CronPattern.ValueParserDAY_OF_MONTH_VALUE_PARSERThe parser for the day of month values.protected static CronPattern.ValueParserDAY_OF_WEEK_VALUE_PARSERThe parser for the day of week values.protected Stringf_sPatternThe pattern as a string.protected static CronPattern.ValueParserHOUR_VALUE_PARSERThe parser for the hour values.protected intm_cMatcherSizeHow many predicate groups in this pattern?protected List<Remote.Predicate<?>>m_listDayOfMonthMatchersThe Predicate list for the "day of month" field.protected List<Remote.Predicate<?>>m_listDayOfWeekMatchersThe Predicate list for the "day of week" field.protected List<Remote.Predicate<?>>m_listHourMatchersThe Predicate list for the "hour" field.protected List<Remote.Predicate<?>>m_listMinuteMatchersThe Predicate list for the "minute" field.protected List<Remote.Predicate<?>>m_listMonthMatchersThe Predicate list for the "month" field.protected static CronPattern.ValueParserMINUTE_VALUE_PARSERThe parser for the minute values.protected static CronPattern.ValueParserMONTH_VALUE_PARSERThe parser for the month values.
-
Constructor Summary
Constructors Constructor Description CronPattern(String sPattern)Builds a CronPattern based on the provided input argument formatted as a crontab-like string.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected Remote.Predicate<?>buildPredicate(String sPattern, CronPattern.ValueParser parser)A Predicate utility builder.intgetNextDayOfMonth(ZonedDateTime zdt, Remote.Predicate<?> predicate)Returns the next dayOfMonth to execute the task.intgetNextDayOfWeek(int nDayOfWeek, Remote.Predicate<?> predicate)Returns the next dayOfWeek (0(Sunday) - 6(Saturday)) to execute the task.longgetNextExecuteTime(long cMillis)Returns the next execution time in milliseconds given timestamp (expressed as a UNIX-era millis value) using the system default time zone.longgetNextExecuteTime(TimeZone timezone, long cMillis)Returns the next execution time in milliseconds from the crontab scheduling pattern, according to the given time zone.intgetNextHour(int nHour, Remote.Predicate<?> predicate)Returns the next hour to execute the task.intgetNextMinute(int nMinute, Remote.Predicate<?> predicate)Returns the next minute to execute the task.intgetNextMonth(int nMonth, Remote.Predicate<?> predicate)Returns the next dayOfWeek to execute the task.protected static intparseAlias(String sValue, String[] asAliases, int cOffset)This utility method changes an alias to an int value.protected List<Integer>parseListElement(String sElement, CronPattern.ValueParser parser)Parses an individual part/element of the crontab configuration.protected List<Integer>parseRange(String sRange, CronPattern.ValueParser parser)Parses a range of values.StringtoString()Returns the pattern as a string.
-
-
-
Field Detail
-
MINUTE_VALUE_PARSER
protected static final CronPattern.ValueParser MINUTE_VALUE_PARSER
The parser for the minute values.
-
HOUR_VALUE_PARSER
protected static final CronPattern.ValueParser HOUR_VALUE_PARSER
The parser for the hour values.
-
DAY_OF_MONTH_VALUE_PARSER
protected static final CronPattern.ValueParser DAY_OF_MONTH_VALUE_PARSER
The parser for the day of month values.
-
MONTH_VALUE_PARSER
protected static final CronPattern.ValueParser MONTH_VALUE_PARSER
The parser for the month values.
-
DAY_OF_WEEK_VALUE_PARSER
protected static final CronPattern.ValueParser DAY_OF_WEEK_VALUE_PARSER
The parser for the day of week values.
-
f_sPattern
protected final String f_sPattern
The pattern as a string.
-
m_listMinuteMatchers
protected List<Remote.Predicate<?>> m_listMinuteMatchers
The Predicate list for the "minute" field.
-
m_listHourMatchers
protected List<Remote.Predicate<?>> m_listHourMatchers
The Predicate list for the "hour" field.
-
m_listDayOfMonthMatchers
protected List<Remote.Predicate<?>> m_listDayOfMonthMatchers
The Predicate list for the "day of month" field.
-
m_listMonthMatchers
protected List<Remote.Predicate<?>> m_listMonthMatchers
The Predicate list for the "month" field.
-
m_listDayOfWeekMatchers
protected List<Remote.Predicate<?>> m_listDayOfWeekMatchers
The Predicate list for the "day of week" field.
-
m_cMatcherSize
protected int m_cMatcherSize
How many predicate groups in this pattern?
-
-
Constructor Detail
-
CronPattern
public CronPattern(String sPattern) throws IllegalArgumentException
Builds a CronPattern based on the provided input argument formatted as a crontab-like string.- Parameters:
sPattern- the pattern as a crontab-like string- Throws:
IllegalArgumentException- if the supplied string is not a valid pattern
-
-
Method Detail
-
getNextExecuteTime
public long getNextExecuteTime(TimeZone timezone, long cMillis)
Returns the next execution time in milliseconds from the crontab scheduling pattern, according to the given time zone.- Parameters:
timezone- a time zonecMillis- the timestamp, as a UNIX-era millis value- Returns:
- the next execute time
-
getNextExecuteTime
public long getNextExecuteTime(long cMillis)
Returns the next execution time in milliseconds given timestamp (expressed as a UNIX-era millis value) using the system default time zone.- Parameters:
cMillis- the timestamp, as a UNIX-era millis value- Returns:
- the next execution time in milliseconds
-
getNextMinute
public int getNextMinute(int nMinute, Remote.Predicate<?> predicate)Returns the next minute to execute the task.- Parameters:
nMinute- the current minutepredicate- the predicate for getting the next minute- Returns:
- the next minute to execute the task
-
getNextHour
public int getNextHour(int nHour, Remote.Predicate<?> predicate)Returns the next hour to execute the task.- Parameters:
nHour- the current hourpredicate- the predicate for getting the next hour- Returns:
- the next hour to execute the task
-
getNextDayOfMonth
public int getNextDayOfMonth(ZonedDateTime zdt, Remote.Predicate<?> predicate)
Returns the next dayOfMonth to execute the task.- Parameters:
zdt- the current ZonedDateTimepredicate- the predicate for getting the next dayOfMonth- Returns:
- the next dayOfMonth to execute the task
-
getNextDayOfWeek
public int getNextDayOfWeek(int nDayOfWeek, Remote.Predicate<?> predicate)Returns the next dayOfWeek (0(Sunday) - 6(Saturday)) to execute the task.- Parameters:
nDayOfWeek- the current dayOfWeekpredicate- the predicate for getting the next dayOfWeek- Returns:
- the next dayOfWeek to execute the task
-
getNextMonth
public int getNextMonth(int nMonth, Remote.Predicate<?> predicate)Returns the next dayOfWeek to execute the task.- Parameters:
nMonth- the current monthpredicate- the predicate for getting the next month- Returns:
- the next month to execute the task
-
toString
public String toString()
Returns the pattern as a string.
-
buildPredicate
protected Remote.Predicate<?> buildPredicate(String sPattern, CronPattern.ValueParser parser) throws Exception
A Predicate utility builder.- Parameters:
sPattern- the pattern part for the Predicate creationparser- the parser used to parse the values- Returns:
- the requested
Remote.Predicate - Throws:
Exception- if the supplied pattern part is not valid
-
parseListElement
protected List<Integer> parseListElement(String sElement, CronPattern.ValueParser parser) throws Exception
Parses an individual part/element of the crontab configuration.
-
parseRange
protected List<Integer> parseRange(String sRange, CronPattern.ValueParser parser) throws Exception
Parses a range of values.
-
parseAlias
protected static int parseAlias(String sValue, String[] asAliases, int cOffset) throws Exception
This utility method changes an alias to an int value.- Parameters:
sValue- the valueasAliases- the aliases listcOffset- the offset applied to the aliases list indices- Returns:
- the parsed value
- Throws:
Exception- if the expressed values doesn't match any alias
-
-