Springboot Integrating timed Tasks

Source: Internet
Author: User

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}

@CompomentTo indicate that this is a bean managed by spring.
@Scheduledis a method annotation, the method that adds the annotation is a single scheduled task, which can be defined in two ways:

    1. @Scheduled(fixedRate = 3000)
      @scheduled declares that this method is a scheduled task that is executed once every fixed time using the Fixedrate property

    2. @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

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.