Scheduled tasks are generally required in the project and can be used to handle special tasks on a regular basis. The use of timing tasks in spirngboot is particularly straightforward, no need to write as many configurations as SPRINGMVC, just add a @enablescheduling annotation on the startup class.
Start class to turn on timed tasks
1 //turn on timed tasks2 @EnableScheduling3 @RestController4 @SpringBootApplication5 //set the scanned package name6@ComponentScan (basepackages = {"Com.preach.controller")})7 Public classinchlifcapplication {8 Public Static voidMain (string[] args) {9Springapplication.run (inchlifcapplication.class, args);Ten } One}
Using annotations on classes that use timed tasks
@Compoment
, using annotations on methods
@Scheduled
1 @Component2 Public classIndexcontroller {3 4 /**5 * time interval, executed every 5 seconds6 */7@Scheduled (fixedrate = 5000)8 Public voidtask () {9System.out.println ("Now Time is:" +NewSimpleDateFormat ("Yyyy-mm-dd HH:mm:ss"). Format (NewDate ()));Ten } One}
@Compoment
To indicate that this is a bean managed by spring.
@Scheduled
is a method annotation, the method that adds the annotation is a single scheduled task, which can be defined in two ways:
@Scheduled(fixedRate = 3000)
@scheduled declares that this method is a scheduled task that is executed once every fixed time using the Fixedrate property
@Scheduled(cron = "0 0/10 * * * ?")
Cron expressions can be executed at a specified time, which is performed every 10 minutes;
Results
Springboot Integrating timed Tasks