Springboot's timed task @scheduled

Source: Internet
Author: User

1. Import the necessary dependencies in Pom.xml:

<parent>        <groupId>org.springframework.boot</groupId>        <artifactId> spring-boot-starter-parent</artifactid>        <version>2.0.  1. release</version>    </parent>    <dependencies>        <!--springboot Core Components--        < dependency>            <groupId>org.springframework.boot</groupId>            <artifactId> spring-boot-starter-web</artifactid>        </dependency>        <dependency>            <groupid >org.springframework.boot</groupId>            <artifactid>spring-boot-starter-tomcat</artifactid >        </dependency>    </dependencies>

2. Write a Springboot startup class:

Import Org.springframework.boot.springapplication;import Org.springframework.boot.autoconfigure.enableautoconfiguration;import Org.springframework.context.annotation.componentscan;import org.springframework.scheduling.annotation.EnableScheduling; @ComponentScan (Basepackages= {"Com.xwj.tasks"}) @EnableScheduling//turn on timed tasks@EnableAutoConfiguration Public classApp { Public Static voidMain (string[] args) {Springapplication.run (App.class, args); }}

Note that it is important to add @EnableScheduling annotations to enable scheduled tasks

3. Start writing Scheduled tasks:

Import Org.springframework.scheduling.annotation.scheduled;import org.springframework.stereotype.component;@ Componentpublicclass  ScheduleTask {    $)    // @Scheduled (cron = "0 23-25 * *?")     Public void Testschedule () {        System. out. println (" timed task:" + System.currenttimemillis ());}    }

Explain:

  @Scheduled Notes:

1, fixedrate at a fixed rate of execution. The above means to execute every 1 seconds

2, Fixeddelay and above a task start time is the baseline, from the previous task started after the execution of the call

3, cron expression. Can implement a timed call, the expression configuration can refer to the corn expression--for setting the timing of any

  

In the use of the process, the landlord felt that if there is only one scheduled task,fixedrate and fixeddelay effect is the same

Springboot's 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.