Processing schedule timed tasks in the Spring boot project

Source: Internet
Author: User

After initialization, we allow support for schedule in the Spring Boot entry class Application.java

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

Then, create a new execution class Jobs.java

@Componentpublic class Jobs {    public final static long ONE_Minute =  60 * 1000;        @Scheduled(fixedDelay=ONE_Minute)    public void fixedDelayJob(){        System.out.println(Dates.format_yyyyMMddHHmmss(new Date())+" >>fixedDelay执行");    }        @Scheduled(fixedRate=ONE_Minute)    public void fixedRateJob(){        System.out.println(Dates.format_yyyyMMddHHmmss(new Date())+" >>fixedRate执行");    }    @Scheduled(cron="0 15 3 * * ?")    public void cronJob(){        System.out.println(Dates.format_yyyyMMddHHmmss(new Date())+" >>cron执行");    }}

This is the simplest of 2 ways, how many minutes to execute once, Fixeddelay and fixedrate, the unit is milliseconds, so 1 minutes is 60 seconds x1000
The difference is that fixedrate is once every few minutes, no matter how much time you spend doing business. I was executed 1 times in 1 minutes, and Fixeddelay was executed 1 minutes after the task was executed. So depending on the actual business, we will choose different ways.

And there is a class of timed tasks, such as 3:15 per day, so we need another way: cron expressions

Cron expressions, have a special syntax, and feel a bit around people, but in simple terms, we remember some common usage, special grammar can be checked separately.
Cron has a total of 7, but the last one is years and can be left blank, so we can write 6 bits:

    • First digit, indicating seconds, value 0-59
    • Second position, the value of 0-59
    • The third digit, which represents the hour, is valued at 0-23
    • Fourth digit, date day/day, value 1-31
    • Fifth digit, date month, value 1-12
    • Sixth place, week, value 1-7, Monday, Tuesday ..., note: Not 1th week, second week meaning
      In addition: 1 represents Sunday, 2 represents Monday.
    • 7th, year, can be left blank, value 1970-2099
      Cron, there are some special symbols, meaning the following:

() Asterisk: can be understood as each meaning, per second, per minute, daily, monthly, every year ...
(?) Question mark: The question mark can only appear in the date and week two positions, indicating that the value of this position is indeterminate, 3 points per day, so the sixth position of the week, we do not need to pay attention to, that is, indeterminate value. At the same time: the date and week are two mutually exclusive elements, with a question mark indicating that no value is specified. For example, January 10, such as the Week 1, if the location of the week is another designated Tuesday, the conflict conflicts.
(-) minus sign: Expresses a range, such as using "10-12" in the hour field, which means from 10 to 12 points, i.e. 10,11,12
(,) Comma: Expresses a list value, such as "1,2,4" in the week field, which means Monday, Tuesday, Thursday
(/) Slash: such as: X/y,x is the start value, Y is the step, such as in the first bit (seconds) 0/15 is, starting from 0 seconds, every 15 seconds, the last is 0,15,30,45,60 another:
/y, equivalent to 0/y
Here are a few examples for you to verify:

0 0 3 * *? 3-point execution per day
0 5 3 * *? Performed 3:5 daily
0 5 3? * * Daily 3:5, same as above
0 5/10 3 * *? 3 points per day, 5 points, 15 points, 25 points, 35 points, 45 points, 55 points these hours are executed.
0 10 3? * 1 per week Sunday, 3:10 execution, note: 1 = Sunday
0 10 3? * 1#3 The third week of each month, executed in Sunday, #号只能出现在星期的位置

Processing schedule timed tasks in the Spring boot project

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.