Quartz Scheduler (2.2.1)-Usage of Crontriggers

Source: Internet
Author: User
Tags ranges

Cron is a UNIX tool which has been around for a long time, so it scheduling capabilities is powerful and proven. The Crontrigger class is based on the scheduling capabilities of Cron.

Crontrigger uses "Cron Expressions", which is able to create firing schedules such as: ' at 8:00am every Monday through Friday ' or ' at 1:30am every last Fri Day of the month.

Crontrigger is often more useful than simpletrigger, if you need a job-firing schedule that recurs based on Calendar-like Notions, rather than on the exactly specified intervals of Simpletrigger.

With Crontrigger, you can specify firing-schedules such as "Every Friday at noon", or "every weekday and 9:30am am", or even "Every 5 minutes between 9:00am am and 10:00am am on every Monday, Wednesday and Friday, during January".

Even so, like Simpletrigger, Crontrigger have a startTime which specifies when the schedule was in force, and a (option AL) endTime that specifies when the schedule should is discontinued.

Cron Expressions

Cron-expressions is used to configure instances of Crontrigger. Cron-expressions is strings that is actually made up to seven sub-expressions, that describe individual details of the schedule. These sub-expression is separated with White-space, and represent:

SecondsMinutesHoursDay-of-monthMonthDay-of-weekYear (optional field)

A example of a complete cron-expression is the string "0 0 12?" WED "*-which means" every Wednesday at 12:00:00 pm ".

Individual sub-expressions can contain ranges and/or lists. For example, the day of week field in the previous (which reads "WED") example could is replaced with "Mon-fri", "mon,wed", FRI ", or even" Mon-wed,sat ".

Wild-cards (the "character") can is used to say "every" possible value of this field. Therefore the "character in the" month "field of the previous example simply means" every Month ". A ' * ' in the Day-of-week field would therefore obviously mean "every day of the Week".

All of the fields has a set of valid values that can specified. These values should is fairly obvious-such as the numbers 0 to $ seconds and minutes , and the values 0 to all for hours . day-of-month can is any of the value 1-31 , but the need to is careful about what many days ar E in a given month! months can be specified as values between 0 and one span>, or by using the strings jan, FEB, MAR, April, May, June, JUL, Mar, SEP, OCT, NOV and DEC . Days-of-week can be specified as values between 1 and 7 (1 = S Unday ) or by using the strings sun, MON, TUE, WED, THU, FRI and SAT .

The '/' character can used to specify increments to values. For example, if you put ' 0/15 ' on the Minutes field, it means ' every 15th minute of the hour, starting at Minute zero '. If you used ' 3/20 ' on the Minutes field, it would mean ' every 20th minute of the hour, starting at minute three '-or In other words it was the same as specifying ' 3,23,43 ' in the Minutes field. Note the subtlety "/35" does not* mean "every minutes"-it means "every 35th minute of the hour, starting at Minu Te Zero "-or in other words the same as specifying ' 0,35 '.

The '? ' character is allowed for the Day-of-month and Day-of-week fields. It is used to specify "no specific value". This is useful if you are need to specify something in one of the. See the examples below (and Crontrigger Javadoc) for clarification.

The ' L ' character is allowed for the Day-of-month and Day-of-week fields.This character was short-hand for "last", but it had different meaning in each of the. For example,The value "L" in the Day-of-month field means "The last day of the month"-Day to January, day of February on Non-leap years.If used in the Day-of-week field by itself, it simply means "7" or "SAT". Butif used in the Day-of-week field after another value, it means ' the last xxx day of the month '-for example ' 6L ' or ' Fril "both mean" the last Friday of the month ". You can also specify a offset from the last day of the month, such as "L-3" which would mean the third-to-last day of the Calendar month.When using the ' option, it's important not to specify lists, or ranges of values, as you'll get Confusing/unexpect Ed results.

The ' W ' is used-Specify the weekday (Monday-friday) nearest the given day. As an example, if you were to specify "15W" as the value of the Day-of-month field, the meaning is: "The nearest weekday To the 15th of the month.

The ' # ' is used to specify "the nth" XXX weekday of the month. For example, the value of "6#3" or "fri#3" in the Day-of-week field means "the third Friday of the month".

Format

The fields is as follows:

Field Name Mandatory Allowed Values Allowed special characters
Seconds YES 0-59 , - *
Minutes YES 0-59 , - *
Hours YES 0-23 , - *
Day of month YES 1-31 , - * ? /L W
Month YES 1-12 or Jan-dec , - *
Day of week YES 1-7 or Sun-sat , - * ? L
Year NO Empty, 1970-2099 , - *

So cron expressions can is as simple as this: * * * *? *, or more complex, like THIS:0/5 14,18,3-39,52 *? Jan,mar,sep Mon-fri 2002-2010

Special characters

1. * (All values) -used to select all values within a field. For example, "*" in the minute field means "every minute".

2. ? ("No specific value") For example, if your want your trigger to fire in a particular day of the month (say, the 10th), but don ' t care f The week that happens to IS, you would put "ten" in the Day-of-month field, and "?" in the Day-of-week field. See the examples below for clarification.

3. - -Used to specify ranges. For example, "10-12" in the hour field means "The hours, one and 12."

4. , -Used to specify additional values. For example, ' Mon,wed,fri ' in the Day-of-week field means ' The days Monday, Wednesday, and Friday '.

5.  /-used to specify increments. For example, "0/15" in the Seconds field means "the seconds 0, 45". and "5/15" in the Seconds field means "the seconds 5, +, +," You can also specify '/' after the ' character-i n this case ' was equivalent to have ' 0 ' before the '/'. ' 1/3 ' in the Day-of-month field means "fire every 3 days starting on the first day of the month".

6.  L ("last") -have different meaning in each of the the the both fields in which it is allowed. For example, the value ' L ' in the Day-of-month field means ' The last day of the month '-day to January, day of the Fe Bruary on non-leap years. If used in the Day-of-week field by itself, it simply means "7" or "SAT". If used in the Day-of-week field after another value, it means ' the last xxx day of the month '-for example ' 6L ' mean S "The last Friday of the month". You can also specify a offset from the last day of the month, such as "L-3" which would mean the third-to-last day of the Calendar Month. when using the ' L ' option, it is important not to specify lists, or ranges of Values, as you ll get confusing/unexpected results.

7.  >w ("weekday") -used to specify the weekday (Monday-friday) Nearest the given day. As an example, if you were to specify "15W" as the value of the Day-of-month field, the meaning is: "The nearest weekday To the 15th of the month. So if the 15th is a Saturday, the trigger would fire on Friday the 14th. If the 15th is a Sunday, the trigger would fire on Monday the 16th. If the 15th is a Tuesday and then it'll fire on Tuesday the 15th. However if you specify "1W" as the value of Day-of-month, and the 1st is a Saturday, the trigger would fire on Monday the 3rd, as it won't ' jump ' on the boundary of a month ' s days. The ' W ' character can only be specified when the Day-of-month is a, a, and a range or list of days.

8. # -Used to specify "the nth", XXX Day of the month. For example, the value of ' 6#3 ' in the Day-of-week field means ' The third Friday of the Month ' (Day 6 = Friday and "#3" = The 3rd one in the month). Other examples: "2#1" = the first Monday of the month and "4#5" = The fifth Wednesday of the month. Note that if you specify "#5" and there are not 5 of the given Day-of-week in the month, then no firing would occur that Mon Th.

Examples

Here is some full examples:

Expression Meaning
0 0 12 * *? Fire on 12pm (noon) every day
0 15 10? * * Fire at 10:15am every day
0 15 10 * *? Fire at 10:15am every day
0 15 10 * *? * Fire at 10:15am every day
0 15 10 * *? 2005 10:15AM every day during 2005
0 * 14 * *? Fire every minute starting @ 2pm and ending at 2:59pm, every day
0 0/5 14 * *? Fire every 5 minutes starting @ 2pm and ending at 2:55pm, every day
0 0/5 14,18 * *? Fire every 5 minutes starting @ 2pm and ending at 2:55pm, and fire every 5 minutes starting at 6pm and ending at 6:55pm, Every day
0 0-5 14 * *? Fire every minute starting @ 2pm and ending at 2:05pm, every day
0 10,44 14? 3 WED Fire on 2:10pm and at 2:44pm every Wednesday in the month of March.
0 15 10? * Mon-fri >fire at 10:15am every Monday, Tuesday, Wednesday, Thursday and Friday
0 15 10 15 *? >fire at 10:15am in the 15th day of every month
0 L *? >fire at 10:15am in the last day of every month
0 L-2 *? >fire at 10:15am in the 2nd-to-last day of every month
0 15 10? * 6L >fire at 10:15am in the last Friday of every month
0 15 10? * 6L >fire at 10:15am in the last Friday of every month
0 15 10? * 6L 2002-2005 >fire at 10:15am in every last Friday of every month during the years 2002, 2003, 2004 and 2005
0 15 10? * 6#3 >fire at 10:15am on the third Friday of every month
0 0 12 1/5 *? >fire at 12pm (noon) Every 5 days every month, starting on the first day of the month.
0 11 11 11 11? Fire every November 11th at 11:11am.

Pay attention to the effects of '? ' and ' * ', the Day-of-week and Day-of-month fields.

Notes

1. Support for specifying both a day-of-week and a day-of-month value are not complete (you must currently use the '? ' Char Acter in one of the these fields).

2. Be careful when setting fire times between the hours of the morning when "daylight savings" changes occur in your local E (for US locales, this would typically is the hour before and after the Am-because the time shift can cause a skip or A repeat depending on whether the time moves back or jumps forward. You could find this Wikipedia entry helpful in determining the specifics to your locale: https://secure.wikimedia.org/wi Kipedia/en/wiki/daylight_saving_time_around_the_world

Building crontriggers

Crontrigger instances is built using Triggerbuilder (for the trigger's main properties) and Cronschedulebuilder (for the Crontrigger-specific properties).

Build a trigger that would fire every other minute, between 8am and 5pm, every day
Trigger = Triggerbuilder.newtrigger ()      . Withidentity ("Trigger3", "group1")      . Withschedule ( Cronschedulebuilder.cronschedule ("0 0/2 8-17 * *?") )       . Forjob ("MyJob", "group1")      . Build ();
Build a trigger that'll fire daily at 10:42 am
Trigger = Triggerbuilder.newtrigger ()      . Withidentity ("Trigger3", "group1")      . Withschedule ( Datebuilder.dailyathourandminute)      . Forjob (Myjobkey)      . Build ();

Or-

Trigger = Triggerbuilder.newtrigger ()      . Withidentity ("Trigger3", "group1")      . Withschedule ( Cronschedulebuilder.cronschedule ("0 42 10 * *?") )       . Forjob (Myjobkey)      . Build ();
Build a trigger that'll fire on Wednesdays @ 10:42 am, in a Timezone and other than the system's default
Trigger = Triggerbuilder.newtrigger ()      . Withidentity ("Trigger3", "group1")            . Forjob (Myjobkey)      . Intimezone (Timezone.gettimezone ("America/los_angeles"))      . Build () ;

Or-

Trigger = Triggerbuilder.newtrigger ()      . Withidentity ("Trigger3", "group1")      . Withschedule ( Cronschedulebuilder.cronschedule ("0 42 10? * WED "))      . Intimezone (Timezone.gettimezone (" America/los_angeles ")).      forjob (myjobkey)      . Build ();

Crontrigger Misfire Instructions

The following instructions can be used to inform Quartz what it should does when a misfire occurs for Crontrigger. (misfire situations were introduced in the more about Triggers sections of this tutorial). These instructions is defined as constants on Crontrigger itself (including Javadoc describing their behavior). The instructions include:

Misfire_instruction_ignore_misfire_policy  misfire_instruction_do_nothing  Misfire_instruction_fire_now

All triggers also has the misfire_instruction_smart_policy instruction available for use, and this instruction is also th e default for all trigger types. The ' smart policy ' instruction is interpreted by Crontrigger as Misfire_instruction_fire_now. The Javadoc for the Crontrigger.updateaftermisfire () method explains the exact details of this behavior.

When building crontriggers, you specify the misfire instruction as part of the simple schedule (via Cronschedulerbuilder):

Trigger = Triggerbuilder.newtrigger ()      . Withidentity ("Trigger3", "group1")      . Withschedule ( Cronschedulebuilder.cronschedule ("0 0/2 8-17 * *?")          . Withmisfirehandlinginstructionfireandproceed ())      . Forjob ("MyJob", "group1")      . Build ();

Quartz Scheduler (2.2.1)-Usage of Crontriggers

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.