Spring-boot uses @ Scheduled to configure Scheduled tasks,
Serial scheduled task
@ Component
Public class ScheduledTimer {
Private Logger logger = Logger. getLogger (this. getClass ());
/**
* Scheduled tasks are executed once every minute to update the shared status of potential customers who have timed out.
*/
@ Scheduled (cron = "0 0/1 8-20 **? ")
Public void executeUpdateCuTask (){
Thread current = Thread. currentThread ();
Logger.info ("scheduled Task 1:" + current. getId () + ", name:" + current. getName ());
}
@ Scheduled (cron = "0 0/1 8-20 **? ")
Public void executeGetRepayTask (){
Thread current = Thread. currentThread ();
Logger.info ("scheduled Task 2:" + current. getId () + ", name:" + current. getName ());
}
}
Configuration files need to be added for parallel scheduled tasks
Because the purpose of spring-boot is to delete the configuration file, many of the files I see on the Internet are implemented through the configuration file. Here, the code configuration is implemented:
@ Configuration
Public class ScheduleConfig implements SchedulingConfigurer {
@ Override
Public void configureTasks (ScheduledTaskRegistrar taskRegistrar ){
TaskScheduler taskScheduler = taskScheduler ();
TaskRegistrar. setTaskScheduler (taskScheduler );
}
@ Bean (destroyMethod = "shutdown ")
Public ThreadPoolTaskScheduler taskScheduler (){
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler ();
Schedsize. setPoolSize (20 );
Schedfix. setThreadNamePrefix ("task -");
Schedsecon. setAwaitTerminationSeconds (60 );
Schedshut. setWaitForTasksToCompleteOnShutdown (true );
Return scheduler;
}
}
In the online tutorial, you need to add the @ EnableScheduling annotation to the startup class to find the annotation @ Scheduled task and execute it in the background.
Maybe I didn't start a project through external tomcat through the startup class, so I can implement the scheduled task without adding this annotation.
If you know anything, please advise @ EnableScheduling. Thank you.