1 Demo
package Com.test.domi.config;import Org.springframework.beans.factory.annotation.configurable;import Org.springframework.scheduling.annotation.enablescheduling;import Org.springframework.scheduling.annotation.scheduled;import Org.springframework.stereotype.component;import Java.text.simpledateformat;import java.util.Date; @Component @configurable@enableschedulingpublic class Scheduledtasks {//every 30 seconds @Scheduled (fixedrate = $ *) public void Reportcurrenttime () {System.out. println ("Scheduling Tasks examples:the time is now" + dateformat (). Format (new Date ())); }//execute @Scheduled at fixed time (cron = "0 */1 * * * *") public void Reportcurrentbycron () {System.out.println ("S Cheduling Tasks Examples by cron:the time was now "+ dateformat (). Format (new Date ())); } private SimpleDateFormat DateFormat () {return new SimpleDateFormat ("HH:mm:ss"); }}
Scheduling Tasks examples:the time is now 11:55:5411:56:0011:56:2411:56:54 11:57:00
2 explanation
http://tramp.cincout.cn/2017/08/18/spring-task-2017-08-18-spring-boot-enablescheduling-analysis/
Cron expression: HTTPS://WWW.ZHYD.ME/ARTICLE/43
3 Summary
The default Concurrenttaskscheduler plan executor implements a single-threaded actuator with executors.newsinglethreadscheduledexecutor (). Therefore, execution of the same dispatch task is always the same thread. If the execution time of the task exceeds the next execution time of the task, a task is lost and the task is skipped for that period of time. The above problems have the following solutions:
Executes the dispatch task asynchronously, configures the Spring @EnableAsync, and marks the @Async configuration task execution pool on the method of the task execution, using Threadpooltaskscheduler.setpoolsize (n). The number of n is the time required for a single task to execute/task execution interval
Spring comes with a timed task function @enablescheduling