Perform recurring tasks with spring's task:scheduled-tasks

Source: Internet
Author: User

Ref is a work class

Method is the way to execute in a work class

Initial-delay is the delay before the task is called for the first time, unit milliseconds

Fixed-delay is the delay that is called again after the last call is completed

Fixed-rate is the delay that is called again after the start of the previous call (without waiting for the last call to complete)

Cron is an expression that indicates when a task is scheduled.

Cron-expression

A cron expression is a string of characters separated by 5 or 6 spaces, divided into 6 or 7 fields, each of which represents a meaning, and Cron has the following two syntax formats:

Seconds Minutes Hours dayofmonth Month DayofWeek year or
Seconds Minutes Hours dayofmonth Month DayofWeek

The following characters can appear for each field:
Seconds: Can appear ",-*/" four characters, valid range is 0-59 integer
Minutes: Can appear ",-*/" four characters, valid range is 0-59 integer
Hours: Can appear ",-*/" four characters, valid range is 0-23 integer
DayOfMonth: Can appear ",-*/? L W C "Eight characters, integer with valid range 0-31
Month: Can appear ",-*/" four characters, a valid range of 1-12 integers or jan-dec
DayofWeek: Can appear ",-*/? L C # "Four characters, a valid range of 1-7 integers or Sun-sat two ranges. 1 means Sunday, 2 means Monday, and so on
Year: Can appear ",-*/" four characters, valid range is 1970-2,099 years

Each field uses numbers, but the following special characters can appear, meaning:
(1) *: Indicates any value that matches the field, and if you use * in the Minutes domain, the event will be triggered every minute.

(2)?: can only be used in DayOfMonth and DayOfWeek two domains. It also matches any value of the field, but it does not. Because DayOfMonth and DayOfWeek will affect each other. For example, to trigger a schedule on the 20th of each month, regardless of whether the 20th is the day of the week, you can only use the following: 13 13 15 20 *?, the last one can only be used? , but not with *, if using * means that the day of the week will trigger, in fact it is not.

(3)-: Indicates a range, for example, using 5-20 in the Minutes domain, indicating that it is triggered every minute from 5 minutes to 20 minutes

(4)/: Indicates that the start time starts triggering and then fires once every fixed time, such as using 5/20 in the Minutes domain, which means that 5 minutes is triggered once, and 25,45 is triggered separately.

(5),: Indicates that the enumeration value value is listed. For example, using 5,20 in a minutes domain means firing at 5 and 20 minutes per minute.

(6) L: Indicates that finally, only in the DayOfWeek and dayofmonth domains, if 5L is used in the DayOfWeek domain, it means that it is triggered in the last Thursday.

(7) W: Indicates a valid weekday (Monday to Friday) that can only appear in the DayOfMonth domain, and the system will trigger the event on the most recent active weekday from the specified date. For example: Use 5W in DayOfMonth, if 5th is Saturday, it will be triggered on the most recent weekday: Friday, 4th. If 5th is Sunday, it will be triggered on 6th (Monday) and if 5th is one day from Monday to Friday, it is triggered on 5th. Another point, W's recent search won't cross the month

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

(9) #: Used to determine the number of weeks per month, can only appear in the DayOfMonth domain. For example in 4#2, which represents the second Wednesday of a month.

Example

0 0 10,14,16 * *? 10 o'clock in the morning, 2 o'clock in the afternoon, 4 O ' Day
0 0/30 9-17 * *? Every half hour for nine to five working hours
0 0 12? * WED means every Wednesday noon 12 o'clock
"0 0 12 * *?" Triggered 12 o'clock noon every day
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Triggered 10:15 daily
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 Trigger
"0 * 14 * *?" Triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily
"0 0/5 14 * *?" Triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily
"0 0/5 14,18 * *?" Every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55
"0 0-5 14 * *?" Triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 daily
"0 10,44 14?" 3 WED "2:10 and 2:44 triggers in Wednesday of every March
"0 15 10?" * Mon-fri "Monday to Friday 10:15 trigger
"0 15 10 15 *?" 15th 10:15 per month, triggered
"0 L *?" 10:15 trigger on the last day of the month
"0 15 10?" * 6L "Last month of Friday 10:15 Trigger
"0 15 10?" * 6L 2002-2005 "2002 to 2005 the last of the monthly Friday 10:15 trigger
"0 15 10?" * 6#3 "Monthly third Friday 10:15 trigger

Create a timed task class

Add the following configuration to the spring configuration file

Perform recurring tasks with spring's task:scheduled-tasks

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.