Home > Java > javaTutorial > body text

Related content of cron expression based on the simplest scheduled task implementation and configuration based on Spring

PHP中文网
Release: 2017-06-22 11:32:25
Original
1487 people have browsed it

Originally this article was going to be published together with the first two articles in this series. However, I encountered an accident yesterday when I was searching for information and summarizing it, so I postponed it a bit.

The content of this article mainly refers to this blog post: (Baidu’s top search link for cron expressions). Try to write something a little different from others. Although, the content is mostly similar.

Let’s start with an example:

“0 0 10,14,16 * *?” What does it mean?

First of all, because the Cron expression is a string, the string is separated by 5 or 6 spaces and divided into 6 or 7 fields. Each field represents a meaning. Cron has the following two syntax formats :

# Seconds MINUTES HOURS Dayofmonth Month Dayofweek Year or
Seconds MINUTES HOURS DAYOFMONTH MOY Dayofweek

# : " The first two positions are both 0. The third position is the value of the hour, which is 10, 14, and 16. Here "," means listing and enumeration, which means that it will be executed every time it encounters 10, 14, and 16 hours, and The ones that follow are *, which means matching any value in this field. If * is used in the Minutes field, it means that the event will be triggered every minute.

Then the last "?" in "0 0 10,14,16 * * ?" refers to ?: can only be used in the two fields of DayofMonth and DayofWeek. It also matches any value of the domain, but it doesn't. Because DayofMonth and DayofWeek will affect each other. For example, if you want to trigger scheduling on the 20th of each month, no matter what day of the week the 20th falls, you can only use the following writing method: 13 13 15 20 * ?, where the last digit can only be used? , but * cannot be used. If * is used, it means that it will be triggered regardless of the day of the week, which is not actually the case.

So "0 0 10,14,16 * *?" means that it will be triggered at 10 o'clock, 14 o'clock, and 16 o'clock every day.

Then, the next question:

“0 0/30 9-17 * *?”

In this expression, there are two more than the previous one symbols, one is "/" and the other is "-". Their meanings are:

"-": Indicates the range. For example, using 5-20 in the Minutes field means that it triggers every minute from 5 minutes to 20 minutes.

"/": Indicates the starting time. Start triggering, and then trigger it every fixed time. For example, using 5/20 in the Minutes field means that it triggers once every 5 minutes, and triggers once every 25, 45, etc.

So here "0/30" ” means that it will be executed every 30 minutes starting from 0 minutes. And "9-17" marks the range, that is, it will be executed from 9 o'clock to 17 o'clock. Taken together, "0 0/30 9-17 * * ?" This expression means that it is executed every 30 minutes from 9:00 to 17:00 every day.

Continue, next question:

The expression here has an extra "WED", which is the abbreviation of Wednesday

So here " 0 0 12 ? * WED" means every Wednesday at 12 noon, the test results will be at once. (For the convenience of testing, change the time to every 10 seconds)

The remaining cron expressions in this article can be tested and verified in the scheduled tasks in the previous two articles. . The remaining expressions will not be analyzed one by one. The following is, quoting the original text, hoping to answer some questions:

1. Cron expression content analysis

Cron expression is a string , the string is separated by 5 or 6 spaces and divided into 6 or 7 fields. Each field represents a meaning. Cron has the following two syntax formats:

Seconds Minutes Hours DayofMonth Month DayofWeek Year or Seconds Minutes Hours DayofMonth Month DayofWeek

The characters that can appear in each field are as follows:

Seconds: Four characters ", - * /" can appear, and the valid range is 0 Integer of -59
Minutes: Four characters ", - * /" can appear, the valid range is 0-59 Integer
Hours: Four characters ", - * /" can appear, the valid range is 0 -23 integer
DayofMonth: eight characters ", - * / ? L W C" can appear, the valid range is an integer of 0-31
Month: four characters ", - * /" can appear, the valid range It is an integer from 1 to 12 or JAN-DEc
DayofWeek: The four characters ", - * / ? L C #" can appear, and the valid range is an integer from 1 to 7 or the two ranges of SUN-SAT. 1 means Sunday, 2 means Monday, and so on.
Year: Four characters ", - * /" can appear, and the valid range is 1970-2099.

Each field uses numbers, but there are still The following special characters can appear, and their meanings are:

(1)*: Indicates matching any value in this field. If * is used in the Minutes field, it means that the event will be triggered every minute.

(2)?: Can only be used in the DayofMonth and DayofWeek domains. It also matches any value of the domain, but it doesn't. Because DayofMonth and DayofWeek will affect each other. For example, if you want to trigger scheduling on the 20th of each month, no matter what day of the week the 20th falls, you can only use the following writing method: 13 13 15 20 * ?, where the last digit can only be used? , but * cannot be used. If * is used, it means that it will be triggered regardless of the day of the week, which is not actually the case.

(3)-: Indicates the range. For example, using 5-20 in the Minutes field means that it triggers every minute from 5 minutes to 20 minutes.

(4)/: Indicates the start time. Trigger, and then trigger every fixed time. For example, using 5/20 in the Minutes field means that it triggers once every 5 minutes, and triggers once every 25, 45, etc.

(5),: means listing enum value value. For example: using 5,20 in the Minutes field means that it is triggered every minute at 5 and 20 minutes.

(6)L: means last and can only appear in the DayofWeek and DayofMonth fields. If 5L is used in the DayofWeek field, it means it will be triggered on the last Thursday.

(7)W: Indicates valid working days (Monday to Friday), which can only appear in the DayofMonth field. The system will trigger the event on the nearest valid working day to the specified date. For example: using 5W on DayofMonth, if the 5th is a Saturday, it will be triggered on the nearest working day: Friday, which is the 4th. If the 5th is a Sunday, it will be triggered on the 6th (Monday); if the 5th is on one of Monday to Friday, it will be triggered on the 5th. Another point is that the latest search for W will not span months

(8)LW: These two characters can be used together to indicate the last working day of a certain month, that is, the last Friday.

(9)#: Used to determine the day of the week of each month and can only appear in the DayofMonth field. For example, in 4#2, it means the second Wednesday of a certain month.

A few examples:
0 0 2 1 * ? * means scheduling tasks at 2 a.m. on the 1st of each month
0 15 10 ? * MON-FRI means Monday to Friday Execute the job every day at 10:15 am
0 15 10 ? 6L 2002-2006 means execute the job at 10:15 am on the last Friday of each month from 2002 to 2006


A cron expression There are at least 6 (possibly 7) space-separated time elements.
In order, they are
seconds (0~59)
minutes (0~59)
hours (0~23)
days (months) (0~31, but you need to consider The number of days in your month)
month (0~11)
day (week) (1~7 1=SUN or SUN, MON, TUE, WED, THU, FRI, SAT)
year (1970- 2099)

Each element can be a value (such as 6), a continuous interval (9-12), an interval (8-18/4) (/ means every 4 hours), a List (1,3,5), wildcard. Since the two elements "date of month" and "date of week" are mutually exclusive, one of them must be set?


Some subexpressions can include Some ranges or lists

For example: the subexpression (day (week)) can be "MON-FRI", "MON, WED, FRI", "MON-WED,SAT"

The "*" character represents all possible values ​​

Therefore, "*" in the sub-expression (month) represents the meaning of each month, and "*" in the sub-expression (day (week)) represents the day of the week Each day


The "/" character is used to specify the increment of the value
For example: "0/15" in the subexpression (minutes) means that starting from the 0th minute, every 15 minutes
The "3/20" in the subexpression (minutes) means that starting from the 3rd minute, every 20 minutes (it has the same meaning as "3, 23, 43")


The "?" character is only used in the two sub-expressions of day (month) and day (week), indicating that no value is specified
When one of the two sub-expressions is specified with a value, in order to avoid conflicts, it is necessary to Set the value of another subexpression to "?"

The "L" character is only used in the day (month) and day (week) subexpressions, which is the abbreviation of the word "last"
But its meaning in the two subexpressions is different.
In the day (month) sub-expression, "L" represents the last day of the month
In the day (week) self-expression, "L" represents the last day of the week, which is SAT

If there is specific content before "L", it has other meanings

For example: "6L" means the sixth to last day of this month, "FRIL" means the last day of this month One Friday
Note: When using the "L" parameter, do not specify a list or range as this will cause problems

Special characters allowed for field allowed values ​​
Seconds 0-59, - */
Minute 0-59, - * /
Hour 0-23, - * /
Date 1-31, - * ? / L W C
Month 1-12 or JAN-DEC, - * /
Weekday 1-7 or SUN-SAT, - * ? / L C
#Year (optional) Leave blank, 1970-2099, - * /

2. Cron expression example:

"0 0 12 * * ?" Triggered every day at 12 noon
"0 15 10 ? * *" Triggered every day at 10:15 am
"0 15 10 * * ?" Triggered every day at 10:15 am
"0 15 10 * * ? *" Triggered every day at 10:15 AM
"0 15 10 * * ? 2005" Triggered every day at 10:15 AM in 2005
"0 * 14 * * ?" Trigger every 1 minute from 2pm to 2:59pm every day
"0 0/5 14 * * ?" Trigger every 5 minutes from 2pm to 2:55pm every day
"0 0/5 14,18 * * ?" Triggers every 5 minutes between 2pm and 2:55pm and every 5 minutes between 6pm and 6:55pm
"0 0-5 14 * * ?" Triggers every afternoon Triggered every 1 minute from 2:00 to 2:05 pm
"0 10,44 14 ? 3 WED" Triggered every year on Wednesdays in March at 2:10 pm and 2:44 pm
"0 15 10 ? * MON-FRI" Triggers at 10:15 am from Monday to Friday
"0 15 10 15 * ?" Triggers at 10:15 am on the 15th of each month
"0 15 10 L * ?" On the last day of each month Triggered at 10:15 AM
"0 15 10 ? * 6L" Triggered at 10:15 AM on the last Friday of each month
"0 15 10 ? * 6L 2002-2005" Every month from 2002 to 2005 Triggered at 10:15 AM on the last Friday
"0 15 10 ? * 6#3" Triggered at 10:15 AM on the third Friday of each month

The above is the detailed content of Related content of cron expression based on the simplest scheduled task implementation and configuration based on Spring. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template