This is the 26th day of my participation in the August More Text Challenge

SpringBoot supports scheduled tasks. Annotations are easy and quick to use for routine scheduled tasks.

Enable the scheduled task annotation @enablesCheduling

@EnableScheduling
@SpringBootApplication
public class DockerApplication {

  public static void main (String[] args) { SpringApplication.run(DockerApplication.class,args); }}Copy the code

@scheduled Adds Scheduled tasks

@Component
public class Task1 {

  @Scheduled(cron = "0/1 * * * * ?" )
  public void doTask1(a){
    System.out.println("doTask1 "+System.currentTimeMillis()); }}Copy the code

Timed tasks can be implemented in two steps.

Cron expression

Cron syntax, consisting of seven parts: Seconds Minutes Hours DayofMonth Month DayofWeek Year

The last year may not be specified, and the CRON expression is usually represented by six paragraphs

The characters that can appear in each field are as follows: Seconds: “, – * /”, Minutes: “, – * /”, Hours: “, – * /”, An integer ranging from 0 to 23 DayofMonth: “, – * /? Month: an integer ranging from 0 to 31 “, – * /”. DayofWeek: an integer ranging from 1 to 12 “, – * /? L C #”, valid range 1-7 integer or SUN-SAT(starting on Sunday) two ranges Year: can appear “, – * /”

1) The following characters may appear in each field, and their meanings are respectively:

  • * : matches any value of the field.

  • ? : Applies only to DayofMonth and DayofWeek. DayofMonth and DayofWeek affect each other. After setting one of these values, you need to set the other value to “? “to avoid collisions.

  • -: indicates the range. It is triggered once in a certain time range

  • / : indicates that the alarm starts at the start time and is triggered every fixed time. For example, if 5/10 is used in the minute domain, the alarm is triggered every 5 minutes, 15,25, and so on. 0/15 in (minutes) indicates that the alarm is triggered every 15 minutes starting from minute 0

  • ,: indicates multiple

  • L: last indicates the last day of a month. In the day/day (month) domain, L indicates the last day of a month. In the day (week) domain, L indicates the last day of a week

  • W: work indicates a valid working day (Monday to Friday). It can only be displayed in the DayofMonth field. The system triggers an event on the latest valid working day to the specified day. For example, if 5W is used in DayofMonth, if the 5th falls on a Saturday, it will be triggered on the nearest working day: Friday, the 4th. If the 5th falls on a Sunday, the 6th (Monday) is triggered; If the 5th falls on a day between Monday and Friday, it is triggered on the 5th. In addition, the recent search for W does not cross months

  • LW: The two characters can be used together to indicate the last working day of a month, i.e. the last Friday

  • #: used to determine the day of the month, such as on 4#2, indicating the second Wednesday of a month

  • “C” stands for canlendar, calendar, for example “1C” in the week field includes the Sunday of the calendar. Basic useless

Note: Dayofweek and dayofmouth must have a value of?

0 * * * *? 0 0 * * *? 0 0 10 * *? 0 * 14 * * once a day at 10:00? 0 30 10 1 *? Is executed every 1 minute between 14pm and 14:59pm. 0 10 10 10 *? Run 0/5 * * * * at 10:10 am on 10th of each month? 0 */1 * * every 5 seconds? 0 0 12-15 * *? Every day from 12 to 15 o 'clock on the hour 0/5 * * *? 0 0-5 15 * *? Every minute between 15pm and 15:05pm is executed. Every 10 minutes between 15 p.m. and 15:50 p.m. each day. 0/10 15,18 * *? 0/30 10-15 * *? Is executed every 10 minutes between 15pm and 15:50pm and 18pm to 18:50pm each day. 0 0 10,12,14 * * every half hour between 10 a.m. and 15:30 p.m. every day? Every day at 10 a.m., 12 p.m., 14 p.mCopy the code

Online Cron tools

qqe2.com/cron

To adapt to the scene

Scheduling service is a single-node task. When a micro-service is used to start multiple nodes, each node will start the same scheduled task. Therefore, Scheduling is not suitable for multi-node micro-service scenarios.