spring boot timer task

Project structure

Create a scheduled task class

package com.example.sbtask.task;
import java.util.Date;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Controller;
@Controller
public class TimeTask {

    @Scheduled(cron="0/5 * * * * ?")
    public void timeRemark() {
        System.out.println("你真美"+new Date().getTime()/1000);
    }
}

Start writing class

package com.example.sbtask;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling// 开启定时任务功能
public class SbtaskApplication {

    public static void main(String[] args) {
        SpringApplication.run(SbtaskApplication.class, args);
    }

}

Run Launcher

Some of the configuration task

Field

allowance

Allow special characters

second

0-59

, - * /

Minute

0-59

, - * /

hour

0-23

, - * /

date

1-31

, - * ? / L W C

month

1-12 or JAN-DEC

, - * /

week

1-7 or SUN-SAT

, - * ? / L C #

Year (Optional)

Blank, 1970-2099

, - * /

Note: - refers to the range

      * Wildcard

      ? Fields do not want to set

CRON expression

CRON expression

meaning

0 0 12 * * ?

Every day 12:00 Trigger

0 15 10  ? * *

Daily 10:15 Trigger

0 15 10 * * 2019

2019 day 10:15 Trigger

0 * 14 * * ?

2:00 every afternoon to 2:59 trigger once every minute

0 0/5 14 * * ?

2:00 every afternoon to 2:55 the end, the trigger once every 5 minutes

0 0/5 14,18 * * ?

Daily 14:00 to 2:55 and 6:00 to 6:55, trigger every 5 minutes

0 0-5 14 * * ?

2:00 to 2:05 pm every day, every minute, triggering a

0 10,44 14 ? 3 WED

March three weekly 2:10 pm and 2:44 pm trigger points

0 15 10th? * MON-FRI

Every weekday 10:15 Trigger

Here you can also see cron expression summary

https://blog.csdn.net/qq_43560721/article/details/104801797

When configuring:

  1. The starter class to add @EnableScheduling comment
  2. The regular tasks like to add @Controller comment
  3. The class method to add @Scheduled comment

Full format CronTrigger configuration is as follows:

Seconds, minutes, hours, days, months, weeks, years. In which can be empty, the other can not be empty

 

* All values ​​represent, for example on the sub-field * indicates trigger every minute

? Said they did not specify a value, for example, we set the month, not on the field week, then we can use? Representation.

- representing the interval. As provided on February 1-2, 1 refers, February triggers.

Representing a plurality of values, such as 1,3,5,6 disposed on month, it refers to 1, 3, 5, June triggers.

/ Is used to increment the trigger, as provided in 5/15 seconds, 5 seconds represents the beginning, every 15 seconds once triggered, corresponding to (5,20,35,50)

L represents the last meaning, plus L on the date field that indicates the last day of the current trigger, if coupled with data, such as 7L, said that the last Sunday of the month triggered.

W represents the weekday closest to the specified date (Monday to Friday), for example, coupled with 10W on the date field, then the nearest working day 10 triggered if the 10th is Saturday, the 9th trigger, if No. 10 is a Sunday, the trigger 11, if the number is correct in 10 working days, the current trigger.

# Serial number (the first few weeks of every month a few), for example: Set 5 # 3 on the week field, then the third week Friday triggered. If set to # 5, then the fifth week Saturday trigger, and if not, is not performed.

L and W can be used at the same time, if you set LW on the date field, then the last working day of the month of execution.

 

 

 

Published 141 original articles · won praise 33 · views 50000 +

Guess you like

Origin blog.csdn.net/qq_43560721/article/details/104960484