Setting of time expressions in spring timer time expression Quartz

Source: Internet
Author: User
Tags numeric value ranges

The setting of time expressions in quartz-----corn expressions

Time format: <!--s M h d M W (?) y (?), respectively, corresponding to: sec > Minutes > Hours > Day > Month > Week > year,

Example:

1. When to execute every day:

<value>0 * * </value>: The following is performed 23:59:00 daily

<value>0 1,2,3 11,12 * *? </value>: Daily 11:01,11:02,11:03; 12:01,12:02,12:03 to perform tasks

2. How often to perform:

Summary description:

The time field of a cron expression allows you to set a numeric value, and you can use special characters to provide lists, ranges, wildcard characters, and so on, as follows:

Asterisk (*): Used in all fields to represent every moment of the corresponding time field, for example, * "Per minute" in the minute field;

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

Minus sign (-): expresses a range, such as using "10-12" in the hour field, representing 10 to 12 points, i.e. 10,11,12;

Comma (,): Expresses a list value, such as "Mon,wed,fri" in the Week field, for Monday, Wednesday, and Friday;

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

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

The format of the Quartz cron expression is very similar to that of the UNIX cron format, but there is a slight difference. One of the differences is that the Quartz format supports the plan down to the second level, while the UNIX cron program only supports up to the minute level. Many of our trigger plans are incremented based on the second level (for example, every 45 seconds), so this is a very good difference.

In UNIX Cron, the job (or command) to execute is stored in a cron expression, at the sixth domain location. The Quartz uses a cron expression to store the execution plan. A crontrigger that references a cron expression is associated with the job during the scheduled time.

Another point that differs from a UNIX cron expression is the number of fields that are supported in an expression. UNIX gives five domains (minutes, hours, days, months, and weeks), and Quartz provides seven domains. Table 5.1 lists the seven domains supported by the Quartz cron expression.

Table 5.1. Quartz Cron expression supports to seven domains
Name Whether you must Allowed values Special characters
Seconds Is 0-59 , - * /
Part Is 0-59 , - * /
When Is 0-23 , - * /
Day Is 1-31 , - * ? /L W C
Month Is 1-12 or Jan-dec , - * /
Week Is 1-7 or Sun-sat , - * ? /L C #
Years Whether Empty or 1970-2099 , - * /


The names of the month and week are case-insensitive. FRI and FRI are the same.

Fields are separated by spaces, as is the case with UNIX cron. It's indisputable that the simplest expression we can write seems to be this:

* * * ? * *

This expression will inspire a deployed job every second (every minute, every hour, every day).

• Understanding special characters

Like UNIX cron, Quartz cron expressions support the creation of more complex execution plans with special characters. However, Quartz is richer in support of special characters than standard UNIX cron expressions.

* Asterisk

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

Expression Model Example:

0 * 17 * *?

Significance: Trigger every minute from 5 o'clock in the afternoon to 5:59 every day. It stops at 5:59 because the value is 17 in the hour field, and at 6 o'clock in the afternoon, the hour becomes 18, and no longer cares about the trigger until the next day of 5 o'clock in the afternoon.

Use the * character when you want trigger to be fired on all valid values in the field.

? Question mark

? The number can only be used on the day and week fields, but not in both fields. Can you think? The character is "I don't care what the value is on that field." "This is different from the asterisk, which indicates each value on that field." is to say that you do not specify a value for this field.

The reason why you cannot specify a value on both fields is difficult to explain and even incomprehensible. Basically, assuming that you specify a value at the same time, the meaning becomes ambiguous: consider if an expression has a value of 11 on the day field, and wed is specified on the domain of the week. Then it was to be trigger only 11th a month, and it was the day of Wednesday that was triggered. Or in every Wednesday number 11th is excited. The way to get rid of this uncertainty is not to specify values on both domains at the same time.

Just remember, if you specify a value for one of these two fields, then you have to put one on the other word value.

Expression Model Example:

0 10,44 14? 3 WEB

Meaning: Every Wednesday 2:10 and 2:44 in March is triggered.

, comma

A comma (,) is used to specify a list of values on a domain. For example, using a value 0,15,30,45 on a second field means that a trigger is triggered every 15 seconds.

Expression Model Example:

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

Meaning: Every moment of the clock triggers a trigger.

/Oblique Bar

The slash (/) is incremented for the timesheet. We've just used commas to denote increments every 15 minutes, but we can also write 0/15.

Expression Model Example:

0/15 0/30 * * *?

Meaning: Triggers the trigger every 15 seconds at the hour and the slightest.

-Medium Underline

The underscore (-) is used to specify a range. For example, the 3-8 on the hour field means "3,4,5,6,7 and 8 points." "The value of the field is not allowed to be rolled back, so a value like 50-10 is not allowed."

Expression Model Example:

0 45 3-8? * *

Meaning: In the morning of 3 points to the morning 8 points of 45 time trigger trigger.

L Letters

L describes the last value allowed on a domain. It is supported only by day and week fields. When used on the Day field, represents the last day of the month specified on the month field. For example, when a trigger is specified on the current month, L on the day field causes the trigger to be triggered on January 31. If there is Sep on the moon, then L will herald a trigger on September 30. In other words, no matter which month is specified, the trigger is triggered on the last day of the corresponding month.

Expression 0 0 8 L *? The meaning is to trigger trigger at 8:00 on the last day of the month. The * description on the month field is "every month".

When the L letter is used on the field, it indicates the last day of the week, which is Saturday (or the number 7). So if you need to trigger trigger in the last Saturday afternoon of each month, you can use the expression 0 59 23? L

When used on the week field, you can use a number to connect with L to represent the last one week X of the month. For example, an expression 0 0 12? * 2L says the trigger is triggered on the last one weeks of each month.

Do not allow range and list values to be used with L

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


W Letter

The W character represents the weekday (MON-FRI) and can only be used in the day domain. It is used to specify the most recent weekday from the specified day. Most business processes are based on work week, so W characters can be very important. For example, the 15W in the day field means "the most recent weekday from the month 15th." "If number 15th is Saturday, then trigger will be triggered on number 14th (Friday) because Thursday is closer to 15th than Monday (this example is 17th)." (Translator Unmi Note: Will not be triggered in 17th, if it is 15W, it may be in 14th (15th is Saturday) or 15th (15th is Sunday) triggered, that is, only in the adjacent day, if the day 15th is on weekdays directly on the day of execution. W can only be used in the specified day field as a single day, not as a range or list value.

# Well No.

# characters can only be used in the week field. It is used to specify the day of the week ordinal of the month. For example, if you specify a field with a value of 6#3, it means the third Friday of the month (6= Friday, #3意味着月份中的第三周). Another example 2#1 meaning is the first Monday of a month (2= Monday, #1意味着月份中的第一周). Note that if you specify #5, but there is no 5th week in the month, then the month will not be triggered.

The Cron expression Cookbook here is designed to provide scenarios for commonly used execution requirements. Although it is not possible to enumerate all the expressions, the following should provide enough examples to meet your business needs.

• Cron expression for minutes

Table 5.1. A task scheduling Cron expression that includes the minute frequency
Usage An expression
Every day from 5:00 pm to 5:59 pm to trigger per minute 0 * 17 * *?
Every five minutes per day from 11:00 pm to 11:55 pm 0 0/5 23 * *?
Every five minutes of daily from 3:00 to 3:55 pm and 6:00 pm to 6:55 pm 0 0/5 15,18 * *?
Every minute of every day from 5:00 am to 5:05 am is triggered 0 0-5 5 * *?


• Cron expressions on the day


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


• Weekly and monthly Cron expressions

Table 5.3 Cron expressions for task scheduling based on frequency of week and/or month
Usage An expression
In each of the Monday, two, three and Thursday 10:15 AM 0 15 10? * Mon-fri
10:15 AM at number 15th per month 0 15 10 15 *?
10:15 AM for the last day of the month 0 L *?
The last month of Friday 10:15 AM 0 15 10? * 6L
In 2002, 2003, 2004, and 2005 years of the month of the last Friday 10:15 AM 0 15 10? * 6L 2002-2005
10:15 AM for the third Friday of the month 0 15 10? * 6#3
Monthly 12:00 PM (noon) from the first day of every five days 0 0 12 1/5 *?
Each of the November 11 11:11 AM 0 11 11 11 11?
March 2:10 pm and 2:44 pm for each Wednesday 0 10,44 14? 3 WED


Eight. Create an instantly triggered Trigger

Sometimes, you need to perform a job immediately. For example, imagine that you are building a GUI program that allows users to execute immediately. Another example, you may have detected that a JOB has not been executed successfully, so you want to run it again immediately. In Quartz 1.5, several methods have been added to the Triggerutils class, making it easy to implement those things. Code 5.4 shows how to deploy a job and just let it execute once.


22222222222222222222222222222222222222


Field   allowed value   Special character       sec  0-59 , - * /       minutes  0-59 , - * /      hours  0-23   - * /      Date  1-31 , - * ? / l  W C      month  1-12  or  jan-dec , - * /       Week  1-7  or  SUN-SAT , - * ? / L  c #      year (optional)   blank, 1970-2099 , - * /       expression meaning       "0 0 12 * * ?"   Daily 12 o'clock Noon trigger       "0 15 10 ? * *"   daily 10:15 trigger        "0&NBSP;15&NBSP;10&NBSP;*&NBSP;*&NBSP;?"   Daily 10:15 triggers       "0 15 10 * * ?  * "  10:15 daily Trigger      " 0 15 10 * * ? 2005 "  2005 every day 10:15 trigger       "0&NBSP;*&NBSP;14&NBSP;*&NBSP;*&NBSP;?"   Triggers       "0 0/5 14 * *&nbsp" every 1 minutes from 2 o'clock in the afternoon to 2:59 every day   Triggers       "0 0/5 14,18 * *&nbsp" every 5 minutes from 2 o'clock in the afternoon to 2:55 every day   Triggers       "0 0-5 14 * *&nbsp" every 5 minutes between 2 o'clock in the afternoon to 2:55 and 6 o'clock in the afternoon to 6:55 every day   triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 every day       "0 10,44 14 ? 3 wed"   Every March Wednesday 2:10 and 2:44 triggers       "0 15 10 ? * mon-fri"   Monday to Friday 10:15 trigger       "0&NBSP;15&NBSP;10&NBSP;15&NBSP;*&NBSP;?"   Every month 15th 10:15 trigger       "0&NBSP;15&NBSP;10&NBSP;L&NBSP;*&NBSP;?"   The last day of the month 10:15 trigger       "0 15 10 ? *  6L "  Last Friday 10:15 trigger      " 0 15 10 ? * 6l  2002-2005 " 2002 year to 2005 month's last Friday 10:15 trigger      " 0 15 10 ? *  6#3 "  Monthly third Friday 10:15 trigger       every morning 6 points       0 6  * * *      every two hours       0 */2 * *  *      Night 11 o'clock to 8 a.m. every two hours, morning eight points       0 23-7/2,8  * * *      number 4th per month and Monday to Monday three in the morning 11       0 11.  4 * 1-3      January 1 morning 4 o ' 0 4 1        1 *         Some of the subexpression can contain ranges or lists      For example: subexpression (Days (weeks)) can be   "Mon-fri", "Mon,wed,fri", "Mon-wed,sat"         "*": characters represent all possible values    &nBsp Therefore, "*" denotes the meaning of each month in the subexpression, "*" in the subexpression (Day (week)), each day of the week         "/": the character is used to specify the increment of the value      

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.