Spring-boot uses @ Scheduled to configure Scheduled tasks,

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.