Use the spring @Scheduled annotation to perform scheduled tasks,

The boss said that this configuration is too troublesome, each scheduling needs to be added to the spring configuration,
can you reduce the amount of configuration to improve development efficiency,
recently I looked at spring's scheduled scheduling method using annotations,
it feels very convenient, at least There are a lot less things to configure,

so leave it as a reminder,

first configure our spring.xml

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


xsi:schemaLocation added
http://www.springframework.org/schema/task  
http://www.springframework.org/schema/task/spring-task-3.1.xsd  


task task scan annotation is enabled
<task:annotation-driven/> 


test code
@Component  //import org.springframework.stereotype.Component;  
public class MyTestServiceImpl  implements IMyTestService {  
      @Scheduled(cron="0/5 * * * * ? ") //Execute every 5 seconds  
      @Override  
      public void myTest(){  
            System.out.println("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 and need to be set during initialization A certain value of a proxytargetclass is true, go to Baidu google for details)
3. There must be component annotations on the implementation class @Component

The rest is the corn expression, the specific use and parameters, please Baidu google,
the following are just a few examples A formula
CRON expression Meaning
"0 0 12 * * ?" Triggered at 12 noon every day
"0 15 10 ? * *" Triggered at 10:15 every morning
"0 15 10 * * ?" Triggered at 10:15 every morning
" 0 15 10 * * ? *" triggers
"0 15 10 * * ? 2005" every day at 10:15 am
"0 * 14 * * ?" every day in 2005 from 2 pm to 2:59 Trigger
"0 0/5 14 * * ?" every minute every day from 2pm to 2:55pm Trigger
"0 0/5 14,18 * * ?" every 5 minutes from 2pm to 2:55 2:
"0 0-5 14 * * ?" is triggered every 5 minutes in the two time periods of 55 and 6:00 to 6:55 every day from 14:00 to 14:05 Trigger
"0 10,44 14 ? 3 WED" every minute every Wednesday at 14:10 and 14:44 in March
"0 15 10 ? * MON-FRI" Triggered at 10:15 every Monday, Tuesday, Wednesday, Thursday, Friday

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326638941&siteId=291194637