Spring Schedule is a scheduled task framework provided by Spring. Compared to Quartz, Schedule is easier to use and can meet most requirements in small and medium sized applications.

Spring Schedule usage demo

Using Spring Schedule in SpringBoot is easy because It is already integrated into SpringBoot’s own starter and doesn’t require any more processing.

Turn on the timing feature with the @enablesCheduling annotation, which can be used on the startup class or the class for the scheduled task. Then use the @scheduled annotation with its parameters to complete the Scheduled task.

For tasks that need to be executed every second, write @scheduled (fixedRate = 1000).

@EnableScheduling @Component public class Task { private static final SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss"); @scheduled (fixedRate = 1000) public void taskOne(){system.out.println (" now time: "+ dateformat.format (new Date()))); }}Copy the code

Start the program, the timing can be executed at intervals of one second.

Using Spring Schedule is that simple, and if you have more requirements, you can probably do it with @Scheduled’s other parameters.

Detailed explanation of @scheduled parameters

cron

Cron expression: [second] [minute] [hour] [day] [month] [week] [year].

location instructions mandatory Allowed value Allowed wildcards
1 seconds is 0-59 – * /
2 points is 0-59 – * /
3 when is 0-23 – * /
4 day is 1-31 – *? / L W
5 month is 1-12 or JAN-DEC – * /
6 weeks is 1-7 or SUN-SAT – *? / L #
7 years no 1970-2099. – * /
  • * represents all values. For example, if you set * in the score field, it will be triggered every minute.
  • ? Indicates that no value is specified. Use scenarios that do not care about the current value of this field. For example, if you want to trigger an operation on the 10th of the month, but you don’t care what day it is, you need to set the field of the week position to “? I’m going to set it to 0, 0, 0, 10 *, okay?
  • – Indicates an interval. For example, if you set hour to 10-12, the clock is triggered at 10,11, and 12.
  • For example, set MON,WED,FRI in the week field to trigger on Monday, Wednesday, and Friday/for incremental triggering. If “5/15” is set above seconds, the trigger will be triggered every 15 seconds starting from 5 seconds (5,20,35,50). Set ‘1/3’ on the month field to start on the 1st of each month as shown and trigger every third day.
  • L for the last meaning. The day field is set to the last day of the month (depending on the current month or leap if it’s February), and the week field is set to Saturday, which is equivalent to “7” or “SAT.” If the “L” is preceded by a number, it indicates the last of the data. For example, if you set the format “6L” on the week field, it means “last Friday of the month”
  • W indicates the latest working day (Monday to Friday) to the specified date. For example, if 15W is set in the day field, it indicates the working day closest to the 15th of each month. If the 15th falls on a Saturday, the next Friday (14th) will be triggered. If the 15th falls on a weekend, the next Monday (16th) will be triggered. If the 15th falls on a weekday (Monday to Friday), it is triggered on that day. If the format is 1W, it is triggered on the latest working day after the first day of each month. If the 1st falls on a Saturday, it will be triggered on Monday the 3rd. (Note, only a specific number can be set before “W”, not a range of “-“).
  • For example, set “6#3” on the week field to the third Saturday of the month. Note that if you specify “#5” and there is no Saturday in the fifth week, this configuration will not trigger (perfect for Mother’s Day and Father’s Day). Tip: ‘L’ and ‘W’ can be used together. If “LW” is set in the day field, it is triggered on the last working day of the month. The setting of the week field is case insensitive when using English letters, that is, MON is the same as MON.
The sample
  • Run this command every 5 seconds:/ 5 *?
  • Run the following command every 1 minute: 0/ 1 ?
  • Run this command at 23:00 every day: 0 0 23 ?
  • Run the following command at 1 am every day: 0 0 1 ?
  • Run this command at 1 am on the first day of each month: 0 0 1 1 *?
  • Run at 23:00 on the last day of each month: 0 0 23 L *?
  • Every Sunday at 1 am: 0, 0, 1? * L
  • Execute once at 26,29,33:0 26,29,33 *?
  • Run the following commands every day: 0, 0,13,18, and 21 ?

zone

Represents the time zone and receives a java.util.TimeZone#ID. The CRON expression is resolved based on that time zone. The default is an empty string that takes the time zone where the server is located.

fixedDelay

How long has it been since the last execution point?

fixedDelayString

Similar to fixedDelay, passing in strings can also support passing in ${} placeholders to read configuration files.

@scheduled (fixedDelayString = "${task.two. FixedDelay}") void taskTwo() {system.out.println (" Scheduled ") " + dateFormat.format(new Date())); }Copy the code

fixedRate

How long has it been since the last execution point?

The difference between fixedRate and fixedDelay is: FixedDelay starts interval timing after the end of the last task, that is, the time difference between two tasks is equal to the interval + task execution time, while fixedRate starts interval timing at the beginning of the last task, which can be regarded as fixed equal to the set interval time.

fixedRateString

As with fixedRate, strings are passed in to support placeholder reading of configuration files.

initialDelay

First task delay time.

initialDelayString

As with initialDelay, placeholders are supported to read configuration information.