Time expression setting in quartz ----- corn expression

Source: Internet
Author: User

Time expression setting in quartz ----- corn expression

Time Format: <! -- S m h d m W (?) Y (?) -->, Corresponding to: Second> minute> hour> day> month> week> year,

Example:

1. When to run every day:

<Value> 0 59 23 **? </Value>: for example, run at 23:59:00 every day.

<Value> 0 1, 2, 3 11, 12 **? </Value>: run the task at, and every day.

2. Run at intervals of time:

Summary:

In addition to agreeing to set a value, the time field of the cron expression can also use some special characters to provide functions such as list, range, and wildcard:

● Asterisk (*): it can be used in all fields to indicate each time point in the corresponding time field. For example, * indicates "every minute" in the minute field ";

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

● Minus sign (-): represents a range. For example, if "10-12" is used in the hour field, the value ranges from 10 to 12, that is, 10, 11, 12;

● Comma (,): represents a list value. For example, if "mon, wed, Fri" is used in the week field, it indicates Monday, Wednesday, and Friday;

● Slash (/): x/y represents an equi-step sequence. X is the starting value, and Y is the incremental step value. For example, if 0/15 is used in the minute field, it is represented as 5/15 seconds, 30 seconds, and 45 seconds. If is used in the minute field, it is represented as 5 seconds, 20 seconds, 35 seconds, and 50 seconds. You can also use */y, it is equivalent to 0/y;

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

The quartz cron expression format is very similar to the Unix cron format, but there is still a slight obvious difference. One of the differences is that the quartz format supports the second-level plan, while the Unix cron plan only supports the minute-level plan. Many of our trigger plans are incremental in seconds (for example, every 45 seconds), so this is a good difference.

In UNIX Cron, the job (or command) to run is stored in the cron expression and located in the sixth domain. Quartz uses the cron expression to store the running 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? Agreed 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 not separated in uppercase and lowercase. 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 run 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 the month field means that the trigger is triggered every month.

Expression example:

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 the same time on the two domains. What do you 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 for not specifying values in the two domains 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 it. Assume that an expression has a value of 11 in the daily domain, at the same time, wed is specified on the weekly domain. So is Trigger only on the 11th 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 these two domains at the same time.

Just remember, if you specify a value for one of these two fields, you must put one? On another word value ?.

Expression example:

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 example:

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 this 0/15.

Expression example:

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. "The value of the field does not agree with the rollback, so the value like 50-10 does not agree.

Expression example:

0 45 3-8? **

Meaning: trigger at AM to AM.

L letters

L indicates the last value agreed 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 every month, can you 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 do not agree that you 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 (Friday), because Thursday is closer to the 15th (17 in this example. (Translator unmi Note: it will not be triggered on the 17th. If it is 15 W, it may be triggered on the 14th (Saturday 15) or 15th (Sunday 15, that is to say, it can only appear on the nearest day. If the day 15 is normal, it will run on the same day ). W can only be 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.

The cron expression cookbook here is designed to provide a solution for frequently used running requirements. Although it is not possible to list all expressions, the following should provide sufficient examples to meet your business needs.

· Minute cron expression

 

Table 5.1. cron expressions that contain minute-level task plans
Usage Expression
Triggered every minute from PM to pm every day 0*17 **?
Triggered every five minutes from PM to pm every day 0 0/5 23 **?
Triggered every five minutes from PM to PM and PM to pm every day 0 0/5 **?
Triggered every minute from AM to am every day 0 0-5 5 **?


· Cron expression of explain


Table 5.2. cron expressions for task plans based on the daily frequency
Usage Expression
Am every day 0 0 3 **?
Am every day (another way) 0 0 3? **
Every day (noon) 0 0 12 **?
At am every day in 2005 0 15 10 **? 2005


· Cron expressions for weeks and months

Table 5.3. cron expressions for task plans based on the week/month frequency
Usage Expression
AM on every Monday, Thursday, and Thursday 0 15 10? * MON-FRI
Am on the 15th of every month 0 15 10 15 *?
Am on the last day of each month 0 15 10 L *?
Am on the last Friday of every month 0 15 10? * 6l
At am on the last Friday of 2002,200, 2004, and 2005 0 15 10? * 6l 2002-2005
Am on the third Friday of every month 0 15 10? * 6 #3
Every five days from the first day of every month (noon) 0 0 12 1/5 *?
Every November 11 AM 0 11 11 11 11?
And every Wednesday in March 0 10, 44 14? 3 wed


8. Create an immediate trigger

Sometimes you need to run a job immediately. For example, imagine that you are building a GUI program and agree that the user can run it immediately. In another example, you may have checked that a job is not running successfully, so you want to re-run it immediately. In quartz 1.5, how many methods are added? In the triggerutils class, it is very easy to implement those things. Code 5.4 demonstrates how to deploy a job and just run it once.

Time expression setting in quartz ----- corn expression

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.