Spring Scheduled implements timed task execution

First we need to configure our spring.xml

 

 

 

xmlns add the following content,

 

 
xmlns:task="http://www.springframework.org/schema/task"

 

 

 

Then add the following to xsi:schemaLocation,

 

 
http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd


Finally, our task task scan annotation

Task Scheduler:

<!-- Task Scheduler-->
 < task :scheduler id = "scheduler"   pool-size = "10" />

 

 
<!-- task executor-->
 < task :executor id = "executor" pool-size = "10" />

<!--开启注解调度支持 @Async @Scheduled -->
<task:annotation-driven executor="executor" scheduler="scheduler" proxy-target-class="true" />


My config scan location is:

 
<!-- Automatically scan annotated beans -->
 < context :component-scan base-package ="com.mywind.eemp" />

 

 

What is scanned is the content under packages such as com.test,

The interface and implementation are required below (my java files are all under the package of com.test)

 

 
  1. publicinterface IMyTestService {   
  2.        publicvoid myTest();   
  3. }  


 

 

 
  1. @Component//import org.springframework.stereotype.Component;    
  2. publicclass MyTestServiceImpl  implements IMyTestService {   
  3.       @Scheduled (cron= "0/5 * * * * ? " )    //Execute every 5 seconds  
  4.       @Override  
  5.       publicvoid myTest(){   
  6.             System.out.println( "Enter test" );  
  7.       }  
  8. }  


After execution, the console will print out Enter the test

 

 

A few points to note:

 

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

 

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, you need to set a certain value of proxytargetclass to true, go to Baidu google for details)

 

3. The implementation class must have component annotation @Component

 

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 * ?" Triggers on the last day of the month at 10:15 am
"0 15 10 ? * 6L" Triggers on the last Friday of the month at 10:15 am 
"0 15 10 ? * 6L 2002-2005" Triggered on the last Friday of every month from 2002 to 2005 at 10:15 am 
"0 15 10 ? * 6#3" Triggered 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=326271530&siteId=291194637
Recommended