Spring's @Scheduled annotation

@component (instantiate ordinary pojo into the spring container, which is equivalent to the one in the configuration file 

package com.vrveis.roundTrip.task;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
public class FlightTrainTask {
    @Scheduled(cron = "0/5 * * * * ? ") // Execute every 5 seconds 
    public  void taskCycle() {
        System.out.println( "Configure timed tasks using SpringMVC framework" );
    }
}

Precautions:

1. Spring's @Scheduled annotation needs to be written on the implementation method

2. The task method of the timer cannot have a return value (if there is a return value, spring will tell you that there is an error when initializing, and you need to set a certain value of proxytargetclass to true)

3. The implementation class must have component annotation @Component

Extension: setting of timing time

  Such as: "0/5 * * * * ?"

  The full format of CronTrigger configuration is: [seconds] [minutes] [hours] [days] [months] [weeks] [years]

serial number illustrate Is it required allowed values Wildcards allowed
1 Second Yes 0-59 , - * /
2 Minute Yes 0-59 , - * /
3 Hour Yes 0-23 , - * /
4 day Yes 1-31 , - * ? / L W
5 moon Yes 1-12 or JAN-DEC , - * /
6 week Yes 1-7 or SUN-SAT , - * ? / L W
7 year no empty or 1970-2099 , - * /

 

Wildcard description:

* means all values. For example: setting "*" on the field of minutes means it will be triggered every minute.


? means no value is specified. The use case is that you don't need to care about the value of the current setting of this field.

For example: to trigger an operation on the 10th of each month, but don't care about the day of the week, so the field that needs the week position is set to "?" Specifically set to 0 0 0 10 * ?


- Indicates the interval. For example, setting "10-12" on the hour means that it will be triggered at 10, 11, and 12 o'clock.


, means specifying multiple values, for example, setting "MON,WED,FRI" on the week field means triggering on Monday, Wednesday and Friday


/ for incremental triggering. If "5/15" is set on the second, it starts from 5 seconds and triggers every 15 seconds (5, 20, 35, 50). Set '1/3' on the month field to start on the 1st of each month and trigger every three days.


L means the last. On the day field setting, it means the last day of the current month (according to the current month, if it is February, it will also depend on whether it is a leap year [leap]), and on the week field it means Saturday, which is equivalent to "7" or "SAT". If you add a number before "L", it means the last of the data. For example, setting the format "6L" on the week field means "the last Friday of this month"


W represents the closest working day (Monday to Friday) to the specified date. For example, if you set "15W" on the day field, it means that the trigger will be triggered on the working day closest to the 15th of each month. If the 15th happens to be a Saturday, it will trigger on the nearest Friday (14th), if the 15th is a weekday, it will trigger on the nearest next Monday (16th). If the 15th happens to be on a working day (Monday to Weekly) 5), it will be triggered on that day. If the specified format is "1W", it means that it will be triggered on the nearest working day after the 1st of each month. If the 1st falls on a Saturday, it will trigger on the following Monday, the 3rd. (Note, only specific numbers can be set before "W", and interval "-" is not allowed).


# Serial number (indicates the day of the month), for example, setting "6#3" on the week field means the third Saturday of the month. Note that if "#5" is specified, there is no week in the fifth week Sixth, the configuration will not be triggered (it's perfect for Mother's Day and Father's Day);

Tip:
'L' and 'W' can be used in combination. If "LW" is set on the day field, it means that it will be triggered on the last working day of the month;
the setting of the week field is case-insensitive if English letters are used, that is, MON is the same as mon;

 

A cron expression has at least 6 (and possibly 7) time elements separated by spaces.

in order of

Seconds (0~59)

minutes (0~59)

hour (0~23)

Day (month) (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)

7. Year (1970-2099)

where 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 "day of the month" and "day of the week" are mutually exclusive, it is necessary to set one of them?.

0 0 10,14,16 * * ? Daily 10am, 2pm, 4pm
0 0/30 9-17 * * ? Every half hour during 9 to 5 business hours
0 0 12 ? *WED means each Wednesday at 12:00 PM 
"0 0 12 * * ?" Every day at 12:00 PM 
"0 15 10 ? * *" Every day at 10:15 AM 
"0 15 10 * * ?" Every day at 10:15 AM 
"0 15 10 * * ? *" fires 
"0 15 10 * * ? 2005" every day at 10:15 am 
"0 * 14 * * ?" every day at 10:15 am in 2005 Minute trigger  "0 0/5 14 * * ?" triggers "0 0/5 14,18 * * ?"
every 5 minutes between 2pm and 2:55pm every  day between 2pm and 2:55pm
and triggers "0 0-5 14 * * ?" every 5 minutes between 6pm and 6:55  pm and triggers "0 10,44 14 ? 3 WED"
every 1 minute between 2pm and 2:05pm every day 
Triggers "0 15 10 ? *MON-FRI" every March at 2:10 pm and 2:44 pm on Wednesdays every year  Triggers "0 15 10 15 * ?" every 15th of the month
at 10:15 am Monday to Friday 
15 trigger 
"0 15 10 L * ?" Triggered at 10:15 am on the last day of the month
"0 15 10 ? * 6L" Triggered at 10:15 am on the last Friday of the month 
"0 15 10 ? * 6L 2002-2005" Fires on the last Friday of every month from 2002 to 2005 at 10:15 am 
"0 15 10 ? * 6#3" Fires on the third Friday of every month at 10:15 am 

Some subexpressions can contain ranges or lists

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

The "*" character represents all possible values

Therefore, "*" in the subexpression ( month ) represents the meaning of each month, and "*" in the subexpression ( day (week) ) represents each day of the week

 

The "/" character is used to specify the increment of the value

For example: "0/15" in the subexpression (minutes) means starting from the 0th minute, every 15 minutes

         "3/20" in the subexpression (minutes) means starting from the 3rd minute, every 20 minutes (it has the same meaning as "3, 23, 43")


The "?" character is only used for day (month) and day (week) subexpressions, indicating that no value is specified

When one of the two subexpressions is assigned a value, in order to avoid conflicts, the value of the other subexpression needs to be set to "?"

 

The "L" character is only used in day (month) and day (week) subexpressions, it is an abbreviation for the word "last"

But its meaning is different in the two subexpressions.

In the day (month) subexpression, "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 the "L", it has other meanings

For example: "6L" means the 6th last day of the month, "FRIL" means the last Friday of the month

Note: When using the "L" parameter, do not specify a list or range as this can cause problems

 

 

Field Allowed Values ​​Allowed Special Characters
  0-59   , - * /
  0-59   , - * /
小时   0-23   , - * /
日期   1-31   , - * ? / L W C
月份   1-12 或者 JAN-DEC   , - * /
星期   1-7 或者 SUN-SAT   , - * ? / L C #
年(可选)   留空, 1970-2099   , - * /

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325201573&siteId=291194637