Basic use of 180801-spring's scheduled tasks

Source: Internet
Author: User

Article link: https://liuyueyi.github.io/hexblog/2018/08/01/180801-Spring of the basic use of scheduled Tasks/

Basic use of spring's scheduled tasks

Spring-boot project, want to add a timed task, what can I do?

    • No matter what the project, can be directly with the JDK native timer task to achieve
    • @Scheduleduse Annotations to

This blog post is mainly focused on the Springboot project, how to use timed tasks

I. Basic use 1. Demo

In the Springboot project, the use of timed tasks requires that the corresponding configuration be started, a simple demo is as follows

@EnableScheduling@SpringBootApplicationpublic class QuickMediaApplication {    public static void main(String[] args) {        SpringApplication.run(QuickMediaApplication.class, args);    }    @Scheduled(cron = "0/1 * * * * ?")    public void sc1() throws InterruptedException {        System.out.println(Thread.currentThread().getName() + " | sc1 " + System.currentTimeMillis());    }}

The code above is simple enough, basically there is nothing extra to explain, just note that to use timed tasks, you must add @EnableScheduling annotations

2. Cron expressions

Another interesting thing is @Scheduled how the cron is defined in the annotations, what does the above mean?

Cron is defined as follows

Seconds Minutes Hours DayofMonth Month DayofWeek YearSeconds Minutes Hours DayofMonth Month DayofWeek

Above each pit position, can obtain the value is different, first separately explained several possible symbols

  • *: represents any value that matches the field, such as a pit bit for a minute * , indicating that every minute is triggered
  • ?: Only available in DayOfMonth and DayOfWeek two domains. It also matches any value of the field, but it does not. Because DayOfMonth and DayOfWeek will affect each other. For example, to trigger a schedule on the 20th of each month, regardless of the day of the week on 20th, you can only use the following notation:13 13 15 20 ?
  • -: Represents a range, such as using 5-20 in a minutes domain, that is triggered from 5 minutes to 20 minutes per minute.
  • /: Indicates that the start time starts firing and then fires once every fixed time
    • If you use 5/20 in the minutes domain, it means that 5 minutes are triggered, and 25,45 and so on are triggered separately.
  • ,: Represents a list of enumeration value values.
    • For example, using 5,20 in the Minutes domain means that it is triggered at 5 and 20 minutes per minute.
  • L: Indicates that finally, only the DayOfWeek and DayOfMonth domains can appear,
    • Using 5L in the DayOfWeek domain means that it is triggered in the last Thursday.
  • W: Indicates a valid weekday (Monday to Friday) that can only appear in the DayOfMonth domain, and the system will trigger the event on the most recent active weekday from the specified date
    • For example: Use 5W in DayOfMonth, if 5th is Saturday, it will be triggered on the most recent weekday: Friday, 4th. If 5th is Sunday, it will be triggered on 6th (Monday) and if 5th is one day from Monday to Friday, it is triggered on 5th. Another point, W's recent search will not cross the month.
  • LW: These two characters can be used together, indicating the last working day of the month, that is, the last Friday.
  • #: Used to determine the number of weeks per month and can only appear in the DayOfMonth domain. For example in 4#2, which represents the second Wednesday of a month.

According to the above instructions, the meaning of the preceding Crond expression is quite clear.

0/1 * * * * ?每s种执行一次
3. Example Description

Here are some common examples to illustrate

0 0 10,14,16 * * ? 每天上午10点,下午2点,4点 0 0/30 9-17 * * ? 朝九晚五工作时间内每半小时 0 0 12 ? * WED 表示每个星期三中午12点 "0 0 12 * * ?" 每天中午12点触发 "0 15 10 ? * *" 每天上午10:15触发 "0 15 10 * * ?" 每天上午10:15触发 "0 15 10 * * ? *" 每天上午10:15触发 "0 15 10 * * ? 2005" 2005年的每天上午10:15触发 "0 * 14 * * ?" 在每天下午2点到下午2:59期间的每1分钟触发 "0 0/5 14 * * ?" 在每天下午2点到下午2:55期间的每5分钟触发 "0 0/5 14,18 * * ?" 在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发 "0 0-5 14 * * ?" 在每天下午2点到下午2:05期间的每1分钟触发 "0 10,44 14 ? 3 WED" 每年三月的星期三的下午2:10和2:44触发 "0 15 10 ? * MON-FRI" 周一至周五的上午10:15触发 "0 15 10 15 * ?" 每月15日上午10:15触发 "0 15 10 L * ?" 每月最后一日的上午10:15触发 "0 15 10 ? * 6L" 每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6L 2002-2005" 2002年至2005年的每月的最后一个星期五上午10:15触发 "0 15 10 ? * 6#3" 每月的第三个星期五上午10:15触发
4. Questions

It's just a simple gesture, but there are a few natural questions that need to be verified.

    • When there are multiple scheduled tasks in a project, are they executed in parallel or serially?
    • If the default is serial
      • So is there a sequence of scheduled tasks that have the same crond expression?
      • Does blocking of a task affect subsequent tasks?
      • What can I do if they need to be executed in parallel?
    • If it is executed concurrently.
      • Is it a newly created thread or a line pool reuse?
      • In concurrent execution, suppose there is a task that executes once per second, but when it consumes more than 1s at a time, what about the performance of this task? Do you constantly add new threads to execute or wait for the next execution to finish?

These questions come out first, leaving the answer to the next example.

Ii. other 1. A grey and grey blog:https://liuyueyi.github.io/hexblog

A gray and gray personal blog, recording all the study and work in the blog, welcome everyone to visit

2. Disclaimer

The letter is not as good as, has been on the content, purely opinion, because of limited personal ability, inevitably there are omissions and errors, such as the detection of bugs or better suggestions, welcome criticism, please feel grateful

    • Weibo address: small Gray Gray Blog
    • QQ: A grey/grey/3302797840
3. Scan for attention

Small grey ash blog& public number

Knowledge Planet

Basic use of 180801-spring's scheduled tasks

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.