In UNIX Cron, the job (or command) to be executed is stored in the cron expression and located in the sixth domain. Quartz uses the cron expression to store the execution plan. The crontrigger that references the cron expression will be associated with the job in the scheduled time.
Another difference from a Unix cron expression is that it supports the number of domains. UNIX provides five domains (minute, hour, day, month, and week), and quartz provides seven domains. Table 5.1 lists the seven fields supported by the quartz cron expression.
Table 5.1. Quartz cron expressions support seven fields
Name |
Required? |
Allowed value |
Special characters |
Seconds |
Yes |
0-59 |
,-*/ |
Minute |
Yes |
0-59 |
,-*/ |
Hour |
Yes |
0-23 |
,-*/ |
Day |
Yes |
1-31 |
,-*? /L W C |
Month |
Yes |
1-12 or JAN-DEC |
,-*/ |
Week |
Yes |
1-7 or sun-Sat |
,-*? /L c # |
Year |
No |
Null or 1970-2099 |
,-*/ |
The names of months and weeks are case-insensitive. Fri and Fri are the same.
Fields are separated by spaces, which is the same as UNIX cron. The simplest expression we can write seems like this:
***? **
This expression inspires a deployed job every second (every minute, every hour, and every day.
· Understanding special characters
Like UNIX Cron, quartz cron expressions support special characters to create more complex execution plans. However, quartz is richer in support of special characters than standard UNIX cron expressions.
* Asterisks
The asterisk (*) indicates that you want to include all valid values in this field. For example, using an asterisk in a month domain means that the trigger is triggered every month.
Expression examples:
0*17 **?
Meaning: trigger is triggered every minute from five o'clock P.M. to PM. It stops at six o'clock P.M. pm because the value 17 is in the small time domain. At five o'clock P.M., the hour changes to 18, and the trigger is ignored until of the next day.
Use the * character when you want the trigger to be fired on all valid values of this field.
? Question mark
? It can only be used in the day and week domains, but cannot be used in both domains. You can think? The character is "I don't care about the value in this field. "Unlike asterisks, asterisks indicate each value in this field .? It means that no value is specified for this field.
The reason why these two fields cannot be specified at the same time is hard to explain or even hard to understand. Basically, if a value is specified at the same time, the meaning will become ambiguous: consider, if an expression has a value of 11 on the daily domain, and wed is specified on the weekly domain. So is Trigger only on the 11th day of every month, and is triggered on Wednesday? Or is it triggered on the 11th of every Wednesday? To remove such ambiguity, you cannot specify values for both domains.
Remember, if you specify a value for one of these two fields, you must put one? on the other ?.
Expression examples:
0 10, 44 14? 3 Web
Meaning: It is triggered at PM and PM on every Wednesday of March 13, March.
, Comma
Comma (,) is used to specify a value list for a domain. For example, if the value 0, 15, 30, and 45 are used, a trigger is triggered every 15 seconds.
Expression examples:
0 0, 15, 30, 45 ***?
Meaning: trigger a trigger within a clock.
/Slash
A slash (/) is used to increase the number of timelines. We just used a comma to indicate the increase every 15 minutes, but we can also write it as 0/15.
Expression examples:
0/15 0/30 ***?
Meaning: trigger every 15 seconds at the hour and half.
-Dashes
Hyphens (-) are used to specify a range. For example, 3-8 in a small time domain means "3, 4, 5, 6, 7, and 8. "Domain values cannot be rolled back, so values like 50-10 are not allowed.
Expression examples:
0 45 3-8? **
Meaning: trigger at AM to AM.
L letters
L indicates the last value allowed on a domain. It is only supported by day and week domains. When used in the day domain, it indicates the last day of the month specified in the month domain. For example, if Jan is specified in the current month, the L in the Japanese domain will trigger the trigger on January 1, January 31. If the monthly domain is SEP, L indicates that the domain will be triggered on March 13, September 30. In other words, no matter which month is specified, trigger is triggered on the last day of the corresponding month.
Expression 0 0 8 L *? Trigger is triggered at AM on the last day of each month. In the monthly domain, * indicates "every month ".
When l is used in the week domain, it indicates the last day of the week, that is, Saturday (or number 7 ). So if you need to trigger a trigger at on the last Saturday of each month, you can use this expression 0 59 23? * L.
When used in the week domain, you can use a number to connect to L to represent the last week X of the month. For example, the expression 0 0 12? * 2L calls trigger on the last Monday of each month.
Do not use range and List Value with L Although you can use the number of weeks (1-7) with L, you are not allowed to use a range value and List Value with L. This produces unpredictable results. |
W letters
The W character represents the day (Mon-Fri) and can only be used in the day domain. It is used to specify a calendar day closest to a specified day. Most of the business processing is based on the work week, So w characters may be very important. For example, 15 W in the Japanese domain means "the last day of the month on the 15th. "If the 15th is Saturday, the trigger will be triggered on the 14th (Thursday), because the last Monday is the 15th, and the example will also be the 17th (unmi note: it will not be triggered on the 17th. If it is 15 W, it may be triggered on the 14th (Saturday on the 15th) or 15th (Sunday on the 15th, that is, it can only appear in the adjacent day. If the day 15 is normal, it will be executed on the same day ). W can only be used for a single day in the specified daily domain, and cannot be a range or list value.
# Well ID
# Characters can only be used in the weekly domain. It is used to specify the day of the week in the month. For example, if you specify the value of the weekly domain as 6 #3, it means the third Friday of a month (6 = Friday, #3 means the third week of the month ). Another example 2 #1 indicates the first Monday of a month (2 = Monday, #1 indicates the first week of the month ). Note: If you specify #5, but there are no 5th weeks in the month, this month will not be triggered.