Spring Boot Learning (eight) timed task @Scheduled

Source: Internet
Author: User

Springboot Timing Task @Scheduled Preface

Sometimes we have the need to have the app perform a certain task at a certain time of day or every other time. In general, you can use multithreading to achieve this function, in the spring framework can be implemented with Quartz, attached note Spring Quartz implementation of multi-tasking timing calls. Under the Springboot framework, we can use Spring scheduling to realize the function of timed tasks.
First, we'll start by creating a Spring Boot project. To create a method:
* (auto-complete initialization) http://blog.csdn.net/u011244202/article/details/54767036
* (Manual completion of initialization) http://blog.csdn.net/u011244202/article/details/54604421

Also note that the Springboot project requires a JDK8 compilation environment!

Then, add annotations to the project main class @EnableScheduling to enable the configuration of the Scheduled tasks

@SpringBootApplication @enableschedulingpublic class Application extends springbootservletinitializer{public    static void Main (string[] args) {        springapplication.run (application.class, args);}    }

Finally, create a timed task implementation class, with notes added@Scheduled

Introduction of the @Scheduled 1. Cron Expressions and Zone

Zone: Represents the time zone that will parse the cron expression.
For cron expressions, refer to the appendix in http://blog.csdn.net/u011244202/article/details/54382466.

2. Explanation of Fixedrate

Invokes a fixed period (in milliseconds) to execute the method. is to defer execution the last time the execution point was started .

3. Explanation of Fixeddelay

The method is executed in milliseconds between the end of the last call and the end of the next call. is deferred execution after the last execution time point .

4. Explanation of InitialDelay

The delay (in milliseconds) before the first execution of the fixedrate () or Fixeddelay () task.

Instance

The scheduled task is called every 5s after the project starts 8s.

@Componentpublic class Scheduledtasks {    //Output time format    private static final SimpleDateFormat format = new SimpleDateFormat ("HH (hh): Mm:ss S");    @Scheduled (InitialDelay = Fixedrate =) public    void Firstscheduledtasks () {        System.out.println (" Timed task execution, now time is: "+format.format (New Date ()));}    }

Result diagram:

Project Reference Address

Project Reference Address: Https://github.com/FunriLy/springboot-study/tree/master/%E6%A1%88%E4%BE%8B4

Attention:

Cron, Fixeddelay, fixedrate can not coexist between the three!!!
Will throw an error:

by: java.lang.IllegalStateException: Encountered invalid @Scheduled method ‘firstScheduledTasks‘: Exactly one of the ‘cron‘, ‘fixedDelay(String)‘, or ‘fixedRate(String)‘ attributes is required.
Official documents

Example document: http://spring.io/guides/gs/scheduling-tasks/
Annotated document: Http://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/scheduling/annotation /scheduled.html

Spring Boot Learning (eight) timed task @Scheduled

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.