Quartz expressions

Setting of time expression in Quartz -----corn expression
Time format: <!-- smhdmw(?) y(?) -->, corresponding to: second>minute>hour>day>month>week>year ,

for example:

1. When does it execute every day:

       <value>0 59 23 * * ?</value>: The following is to execute every day at 23:59:00

      <value>0 1,2,3 11,12 * * ? < /value>: Execute tasks



at



11:01 , 11:02, 11:03; 12:01, 12:02, 12:03 every

day In addition to numerical values, some special characters can also be used to provide functions such as lists, ranges, wildcards, etc. The details are as follows:

Asterisk (*): can be used in all fields, indicating every moment in the corresponding time domain, for example, *in When the minute field is used, it means "every minute";

● Question mark (?): This character is only used in date and week fields, and it is usually specified as "meaningless value", which is equivalent to the period character;

● Minus sign (-) : Express a range, such as using "10-12" in the hour field, it means from 10 to 12 o'clock, that is, 10, 11, 12;

Comma (,): Express a list value, such as using " in the week field MON, WED, FRI", it means Monday, Wednesday and Friday;

●Slash (/): x/y expresses a sequence of equal steps, x is the starting value, y is the incremental step value. If using 0/15 in the minute field means 0, 15, 30 and 45 seconds, and 5/15 in the minute field means 5, 20, 35, 50, you can also use */y which is equivalent to 0/y;

=====================================================

The format of Quartz cron expressions is very similar to the UNIX cron format, with a few notable differences. One of the differences is that the Quartz format supports schedules down to the second level, while the UNIX cron schedule only supports schedules down to the minute level. Many of our trigger schedules are based on second increments (e.g. every 45 seconds), so this is a pretty good difference.

In UNIX cron, the job (or command) to be executed is stored in the cron expression, in the sixth field position. Quartz uses cron expressions to store execution plans. The CronTrigger referenced by the cron expression will be associated with the job at the scheduled time.

Another difference from UNIX cron expressions is the number of fields supported in the expression. UNIX provides five fields (minute, hour, day, month, and week), and Quartz provides seven fields. Table 5.1 lists the seven domains supported by Quartz cron expressions.


Table 5.1. Quartz Cron expressions support seven fields Whether the name must allow value special characters
Second is 0-59, -*/
minute is 0-59, -*/
hour is 0-23, -*/
day is 1- 31 , - * ? / LWC
month is 1-12 or JAN-DEC , - * /
week is 1-7 or SUN-SAT , - * ? / LC
#year is not null or 1970-2099 , - * /


month and week The name is not case-sensitive. FRI and fri are the same.

Domains are separated by spaces, just like UNIX cron. Arguably, the simplest expression we can write looks like this:

* * * ? * *

This expression fires a deployed job every second (every minute, hourly, every day) .

·Understanding special

characters Like UNIX cron, Quartz cron expressions support the use of special characters to create more complex execution plans. However, Quartz has richer support for special characters than standard UNIX cron expressions.

* Asterisk

Use an asterisk (*) to indicate that you want to include all valid values ​​on this field. For example, using an asterisk on the month field means that the trigger will fire every month.

Example expression:

0 * 17 * * ?

Meaning: Fires the trigger every minute from 5:00 pm to 5:59 pm every day. It stops at 5:59 pm because the value 17 is on the hour field, at 6 pm the hour becomes 18 and the trigger is ignored until 5 pm the next day.

Use the * character when you want the trigger to fire on all valid values ​​for the field.

? The question mark

? sign can only be used on the day and week fields, but not both. You can think of the ? character as "I don't care what value is on the field." This is different from the asterisk, which indicates every value on the field. ? means do not specify a value for the field.

The rationale for not specifying values ​​on both fields at the same time is difficult to explain or even understand. Basically, assuming both values ​​are specified, the meaning becomes ambiguous: consider an expression that has the value 11 on the day field and WED on the week field. So the trigger is only fired on the 11th of every month, which happens to be Wednesday again? Or is it fired every Wednesday the 11th? The way to remove this ambiguity is not to specify values ​​on both fields at the same time.

Just remember that if you specify a value for one of these two fields, you must put a ? on the other word value.

Example Expression:

0 10,44 14 ? 3 WEB

Meaning: Fired every Wednesday in March at 2:10pm and 2:44pm.

, comma The

comma (,) is used to specify a list of values ​​for a field. For example, using the values ​​0,15,30,45 on the seconds field means that a trigger is fired every 15 seconds.

Expression example:

0 0,15,30,45 * * * ?

Meaning: trigger a trigger every quarter of an hour.

/ slash The

slash (/) is used for timetable increments. We just used commas for increments every 15 minutes, but we could also write 0/15 like this.

Expression example:

0/15 0/30 * * * ?

Meaning: trigger trigger every 15 seconds on the hour and half hour.

- dash

A dash (-) is used to specify a range. For example, 3-8 on the hour field means "3,4,5,6,7 and 8 o'clock." The value of the field does not allow wrapping, so values ​​like 50-10 are not allowed.

Expression example:

0 45 3-8 ? * *

Meaning: Trigger is fired at 45:00 AM to 8 AM.

L The letter

L specifies the last value allowed on a field. It is only supported by the day and week fields. When used in the day field, it indicates the last day of the month specified in the month field. For example, an L on the day field causes the trigger to fire on January 31st when JAN is specified on the month field. If the moon is SEP, then L will indicate that it will be triggered on September 30th. In other words, no matter which month is specified, the trigger is triggered on the last day of the corresponding month.

The expression 0 0 8 L * ? means to trigger the trigger at 8:00 am on the last day of each month. The * description on the month field is "every month".

When the L letter is used on the week field, it indicates the last day of the week, which is Saturday (or the number 7). So if you needed to trigger the trigger at 11:59 PM on the last Saturday of every month, you could use the expression 0 59 23 ? * L .

When used on the week field, you can use a number with L to represent the last week of the month X. For example, the expression 0 0 12 ? * 2L says to fire the trigger on the last Monday of every month.


Do not use range and list values ​​with L

Although you can use week numbers (1-7) with L, you are not allowed to use a range and list value with L. This can produce unpredictable results.


W letter

The W character stands for weekdays (Mon-Fri) and can only be used in the day field. It is used to specify the nearest weekday to the specified day. Most business processing is workweek based, so the W character can be very important. For example, 15W in the day field means "the closest weekday to the 15th of the month." If the 15th is a Saturday, then the trigger will fire on the 14th (Friday) because Thursday is longer than Monday (the 17th in this example). ) is closer to the 15th. (Translator Unmi Note: It will not be triggered on the 17th, if it is 15W, it may be triggered on the 14th (the 15th is Saturday) or the 15th (the 15th is Sunday), that is, it can only appear on the adjacent day. , if the 15th is a weekday, it will be executed directly on the same day). W can only be used when the specified day field is a single day, and cannot be a range or list value.

The # pound

# character can only be used in the perimeter field. It is used to specify the day of the week of the month. For example, if you specify a value of 6#3 for the week field, it means the third Friday of the month (6=Friday, #3 means the third week of the month). Another example 2#1 means the first Monday of the month (2=Monday, #1 means the first week of the month). Note that if you specify #5 and there is no 5th week in the month, then the month will not fire.

The Cron expression cookbook here is designed to provide scenarios for common execution needs. Although it is impossible to list all expressions, the following should provide enough examples to meet your business needs.

· Minute Cron Expressions


Table 5.1. Task Schedule Cron Expressions Including Minute Frequency Usage Expressions
Fired every minute from 5:00 PM to 5:59 PM every day 0 * 17 * * ?
Every day from 11 Trigger every five minutes from :00 PM to 11:55 PM 0 0/5 23 * * ?
Triggers every five minutes from 3:00 to 3:55 PM and 6:00 PM to 6:55 PM, daily 0 0/5 15,18 * * ?
Daily from 5:00 AM to 5:05 AM Trigger every minute in 0 0-5 5 * * ?


· Cron expressions on the day

Table 5.2. Cron expressions for task scheduling based on the frequency of the day Usage expressions
Every day at 3:00 AM 0 0 3 * * ?
3:00 AM every day (another way of writing) 0 0 3 ? * *
12:00 PM (noon) every day 0 0 12 * * ?
10:15 AM every day in 2005 0 15 10 * * ? 2005


· Weekly and Monthly Cron Expressions

Table 5.3. Cron Expressions Usage Expressions for Task Scheduling Based on Weekly and/or Monthly Frequency Expressions
Every Monday, Tuesday, Wednesday and Thursday at 10:15 AM 0 15 10 ? * MON-FRI
10:15 AM on the 15th of each month 0 15 10 15 * ?
10:15 AM on the last day of each month 0 15 10 L * ?
10:15 AM on the last Friday of each month 0 15 10 ? * 6L
On the last Friday of every month in 2002, 2003, 2004, and 2005 at 10:15 AM 0 15 10 ? * 6L 2002-2005
at 10:15 AM on the third Friday of every month 0 15 10 ? * 6 #3
12:00 PM (noon) every five days from the first day of
each
month 2:10 PM and 2:44 PM 0 10,44 14 ? 3 WED


8. Create an Instantly Trigger

Sometimes, you need to execute a job immediately. For example, imagine that you are building a GUI program that the user can execute immediately. As another example, you might have detected that a Job did not execute successfully, so you want to rerun it immediately. In Quartz 1.5, several methods were added to the TriggerUtils class to make it easy to implement those things. Listing 5.4 shows how to deploy a job and have it execute only once immediately.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327088083&siteId=291194637