Springboot default Scheduled Task--scheduled annotations

Source: Internet
Author: User
Tags dateformat

This blog refers to the post address . 1.pom dependencies:

The introduction of Springboot starter package can

<Dependencies>    <Dependency>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter</Artifactid>    </Dependency>    <Dependency>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-starter-test</Artifactid>        <Scope>Test</Scope>    </Dependency>     <Dependency>        <groupId>Org.springframework.boot</groupId>        <Artifactid>Spring-boot-devtools</Artifactid>        <Optional>True</Optional>    </Dependency></Dependencies>
2. Start the class to enable timed tasks:

Add annotations on the Startup class: @EnableScheduling can be implemented.

@SpringBootApplication @enablescheduling  Public class Application {    publicstaticvoid  main (string[] args) {        Springapplication.run (Application. class , args);}    }
3. Create a timed Task implementation class:

Scheduled Task 1:

@Component  Public class schedulertask {    privateint count=0;    @Scheduled (Cron= "*/6 * * * * *?") private void  process () {        System.out.println ("This is      Scheduler task runing  "+ (count++));}    }

Scheduled Task 2:

  @Component  public  class   Scheduler2task { private  static  final  simpledateformat dateformat = new  simpledateformat (" HH:mm:ss " = 6000 public  void   Reportcurrenttime () {System.out.pri Ntln ( now time: "+ dateformat.format (new   Date    ())); }}

The results are as follows:

 This is scheduler task runing  0 Present time:09:44:17 This is scheduler task runing  1  Present time:09:44:23 This is scheduler task runing  2 Now:09:44:29 this is scheduler Task runing  3 Present time:09:44:35

Parameter description:

The @Scheduled accepts two timing settings:

One is cornexpression.

One is the Rate/delay expression (millisecond value):

@Scheduled (fixedrate = 6000): Executes every 6 seconds after the last time the execution point was started.

@Scheduled (Fixeddelay = 6000): Executes 6 seconds after the last execution time point.

@Scheduled (initialdelay=1000, fixedrate=6000): Executes after the first delay of 1 seconds, followed by the rules of fixedrate every 6 seconds.

Cornexpression expression:

Full field: [Seconds] [min] [hour] [day] [month] [week] [year]

Field allowed values allow special characters

Seconds 0-59,-*/

Sub 0-59,-*/

Hours 0-23,-*/

Day 1-31,-*? /L W C

Month 1-12 or JAN-DEC,-*/

Week 1-7 or Sun-sat,-*? /L C #

Year Blank or 1970-2099,-*/

Note:

* denotes all values, which in minutes indicate every minute triggered. In hours, dates, months, etc., each hour, every day, every January.

? Indicates that no value is specified. Represents a value that does not care about the current location setting.  For example, do not care about the weeks, the location of the week fill? The main reason is that the date and week are duplicated so both must have accesses than either set to?

-Indicates the interval. The hour is set to 10-12 to indicate that the 10,11,12 point is triggered.

, which represents multiple values. Hours are set to 10,12 to indicate that 10 and 12 points are triggered.

/indicates an increment trigger. 5/15 indicates that it is triggered every 15 seconds starting from the 5th second.

L indicate the last meaning. On the last day of the day. The week represents Saturday or 7. L Pre-add data that represents the last of the data.

The week set 6L represents the last Friday. 6 = Friday

W indicates the most recent working day from the specified date. 15W is triggered on the most recent working day from 15th in the month.

#表示每月的第几个周几. 6#3 represents the third Friday of the month.

Example:

"0 0 12 * *?" trigger 12 o'clock noon every day.
"0 15 10?" * * "trigger 10:15 every day"
"0 15 10 * *?" Daily 10:15 Trigger
"0 15 10 * *?" * "10:15 per day" trigger
"0 15 10 * *?" 2005 "2005-year daily 10:15 trigger
"0 * 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:59 daily
"0 0/5 14 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily
"0 0/5 14,18 * *?" triggers every 5 minutes from 2 o'clock in the afternoon to 2:55 daily and from 6 o'clock in the afternoon to 6:55
"0 0-5 14 * *?" triggers every 1 minutes from 2 o'clock in the afternoon to 2:05 daily
"0 10,44 14?" 3 WED "2:10 and 2:44 triggers in Wednesday of every March
"0 15 10?" * Mon-fri "Monday to Friday 10:15 trigger
"0 15 10 15 *?" 15th 10:15 per month
"0 L *?" 10:15 on the last day of the month
"0 15 10?" * 6L "Last month of Friday 10:15 trigger
"0 15 10?" * 6L 2002-2005 "2002 to 2005 the last of the monthly Friday 10:15 trigger
"0 15 10?" * 6#3 "Monthly third Friday 10:15 trigger
every morning 6 pips 0 6 * * * Every two hours 0 */2 * * *
11 o'clock to 8 a.m. every two hours, eight a.m. 0 23-7/2,8 * * *
every month, number 4th and Monday to Sunday, three a.m., 11, 0, 4 * 1-3 .
January 1 4 a.m. 0 4 1 1 *

Springboot default Scheduled Task--scheduled annotations

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.