SpringBoot combination timer @Scheduled

Reference article: https://www.cnblogs.com/ityouknow/p/6132645.html
Reference article: https://www.cnblogs.com/dyppp/p/7498457.html

In our development process, often need to regularly mission to help us to do something, Spring Boot default has helped us to implement, and only need to add the appropriate annotation can be achieved.

1.pom package configuration

pom incorporated inside the package only needs to Spring Boot Starter package.

<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>
</dependencies>

2. Start class enables the timing

In the above category plus @EnableScheduling to start opening timing.

@SpringBootApplication
@EnableScheduling
public class Application {

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

3. Create a scheduled task implementation class

A scheduled task:

@Component
public class SchedulerTask {

    private int count=0;

    @Scheduled(cron="*/6 * * * * ?")
    private void process(){
        System.out.println("this is scheduler task runing  "+(count++));
    }

}

Timing Task Two:

@Component
public class Scheduler2Task {

    private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");

    @Scheduled(fixedRate = 6000)
    public void reportCurrentTime() {
        System.out.println("现在时间:" + dateFormat.format(new Date()));
    }

}

The results are as follows:

this is scheduler task runing  0
现在时间:09:44:17
this is scheduler task runing  1
现在时间:09:44:23
this is scheduler task runing  2
现在时间:09:44:29
this is scheduler task runing  3
现在时间:09:44:35

Parameter Description

@Scheduled parameter can take two kinds of timing provided:

  • One is our common cron = "* / 6 * * * *?"
  • One is fixedRate = 6000

Both are represented at every six seconds to print content.

fixedRate Description
  • @Scheduled (fixedRate = 6000): begin after the last point of time of 6 seconds and then perform
  • @Scheduled (fixedDelay = 6000): after a point execution completion time 6 seconds and performing
  • @Scheduled (initialDelay = 1000, fixedRate = 6000): After performing the first 1 second delay followed by the rules fixedRate performed once every 6 seconds
Description cron

Cron expression is a string of 5 or 6 separated by a space, is divided into six or seven domains, each domain representing a meaning, Cron following two syntax:

  1. Seconds Minutes Hours DayofMonth Month DayofWeek Year
  2. Seconds Minutes Hours DayofMonth Month DayofWeek

Each character domain can appear as follows:

  • Seconds The fast- : there may be "- * /" four characters, valid integer in the range of 0-59
  • Minutes : there may be "- * /" four characters, valid integer in the range of 0-59
  • Hours : there may be "- * /" four characters, valid integer in the range of 0-23
  • DayOfMonth : there may be ", - * / LW C? " Eight characters, valid integer in the range 0-31
  • Month : there may be "- * /" four characters, the effective range or an integer of 1 to 12 JAN-DEc
  • DayofWeek : there may be ", - * / LC #? " Four characters, valid integer in the range or two ranges of 1-7 SUN-SAT. 1 for Sunday, 2 for Monday, and so on
  • Year : there may be "- * /" four characters, the effective range of 1970- 2099

Each domain numbers, but can also appear as special characters, their meanings are:

  1. * : Means match any value of the domain, if used in the Minutes field , it means that every minute a trigger event.
  2. ?: It can only be used in DayofMonth and DayofWeek two domains. It also matches any value of the field, but actually not. Because DayofMonth and DayofWeek affect each other. For example, you want to trigger scheduled on the 20th of each month, regardless of the 20 in the end of the week, you can only use the following wording:? 13,131,520 *, which can only be the last one? , * Can not be used, no matter if you use * Indicates day of the week will be triggered, in fact not the case.
  3. -: indicates a range, for example, in the domain Minutes 5-20, represents 20 minutes from 5 assigned to the trigger once per minute
  4. /: Start trigger indicating a start time, and then trigger a fixed time intervals, for example, 5/20 in Minutes domain, it means 5 minutes a trigger, and a trigger 25, 45, respectively.
  5. ,: Indicates lists enumeration value. For example: the use of 5,20 in Minutes field, it means that the trigger once per minute at 5 and 20 minutes.
  6. L: represents the last, and only appeared in DayofWeek DayofMonth domain, if 5L in DayofWeek domain, meaning that triggered last Thursday.
  7. W: represents the effective working days (Monday to Friday), can only appear in DayofMonth domain, the system will trigger events in recent days from the effective date specified. For example: the use of 5W in DayofMonth, if the 5th is a Saturday, then in recent days: Friday that the 4th trigger. If the 5th is a Sunday, then on the 6th (Zhouyi) is triggered; if, in the Friday day, it triggers on Monday 5th in the 5th. Another point, W recently looking for does not cross the month.
  8. LW: These two characters can be used in conjunction, represent the last working day in a month, and last Friday.
  9. #: Means for determining a month the first few days of the week, only appear in DayofMonth domain. For example, in 4 # 2, it represents the second Wednesday of the month.
A few examples:
  • Executed once every 5 seconds: "? * / 5 * * * *"
  • Executed once every minute: "? 0 * / 1 * * *"
  • 23:00 once a day to perform: "0023 * *?"
  • 1:00 executed once a day: "001 * *?"
  • Monthly No. 1 1:00 executed once: "0011 *?"
  • The last day of each month 23:00 executed once: "0 0 23 L *?"
  • Weekly Sunday 1:00 to implement once: "? 0 0 1 * L"
  • In 26 points, 29 points, 33 points is performed once: "? 0 26,29,33 * * *"
  • Daily 0:00, 13:00, 18:00, 21:00 executed once: "? 00 0,13,18,21 * *"
  • Said in a monthly two one-day scheduling tasks: "0021 * *?"
  • She said on Monday through Friday at 10:15 am every day execution of the job: "? 0 15 10 * MON-FRI"
  • He represents 2002-- 10:15 am last Friday of each month to perform in 2006: "0 15 10 6L 2002-2006?"

Note: As the "day of the month" and "date of the week," these two elements are mutually exclusive, and must be for one of the settings?.

Published 33 original articles · won praise 9 · views 8696

Guess you like

Origin blog.csdn.net/Serena0814/article/details/96830518