Preamble:
1. Spring Task is a lightweight timing task tool provided by Spring, which means that there is no need to add third-party dependencies, which is more convenient and easy to use than other third-party class libraries.
2. spring task is a timer class introduced after spring 3.0, which can be regarded as a lightweight quartz. Due to its simple configuration and complete functions, it is often used in practical projects. spring task supports xml configuration, annotation configuration, and java configuration.
The implementation of annotation configuration:
The first step is to create a new configuration class SpringTaskConfig, and add the @EnableScheduling annotation to enable Spring Task.
@Configuration @EnableScheduling public class SpringTaskConfig { }
You can also add the @EnableScheduling annotation directly to the main class without creating a new configuration class.
@SpringBootApplication @EnableScheduling public class CodingmoreSpringtaskApplication { public static void main(String[] args) { SpringApplication.run(CodingmoreSpringtaskApplication.class, args); } }
The second step is to create a new scheduled task class CronTask, and use the @Scheduled annotation to register Cron expressions to execute scheduled tasks.
@Slf4j @Component public class CronTask { @Scheduled(cron = "0/1 * * ? * ?") public void cron() { log.info("timed execution{}", DateUtil.now()); } }
The third step, start the server, and find that the log will be printed every second, which proves that the cron expression form of Spring Task has taken effect, as shown in the following figure.
About Cron expressions:
1. Meaning: The word Cron comes from the Greek chronos, which means time.
Example | illustrate |
---|---|
0 15 10 ? * * | Execute the task every day at 10:15am |
0 0 10,14,16 * * ? | Perform tasks at 10:00, 14:00, 16:00 every day |
0 0 12 ? * 3 | Execute the task every Wednesday at 12 noon |
0 15 10 15 * ? | Execute the task on the 15th of each month at 10:15 am |
2. The syntax format of Cron can be summarized as:
English: Seconds Minutes Hours DayofMonth Month DayofWeek
Chinese: second minute hour day of the month month day of the week
3. The value range of each time element and the special characters that can appear, as shown in the following table:
time element | Ranges | Special characters that can appear |
---|---|---|
second | [0,59] | *,-/ |
minute | [0,59] | *,-/ |
Hour | [0,59] | *,-/ |
date | [0,31] | *,-/?LW |
month | [1,12] | *,-/ |
Week | [1,7] | *,-/?L# |
4. The meaning and examples of special characters are shown in the following table:
Special characters | meaning | Example |
---|---|---|
* | all possible values | Used to represent all values, it can be understood that all values are executed, that is, the meaning of "every", for example, entering * in the second position means that it runs every second |
, | the value of the enumeration | It can be used for the union of multiple time points separated by commas. For example, if you enter 1,2 in the position of the month, it means that it will be executed in both January and February. |
- | scope | Used to indicate a time period, a range of continuous triggering, for example, entering 10-30 in the second position means executing once per second between 10 and 30 seconds |
/ | Increment of the specified value | 0/15 in the minute field means that it is executed every 15 minutes |
? | no value specified | It is only used in the fields of week and day to avoid conflicts between week and day, and is used for mutual exclusion between week and day. Usually one value is set for week and day, and the other is indicated by ? for no value. For example, to trigger an action on the 1st of the month, but ignore the day of the week, we can set it to 0 0 0 1 * ? |
L | first letter of the word Last | Date field and week field support, indicating the last day of the month or the last day of the week |
W | weekdays except weekends | Only date fields are supported |
# | the day of the month | Only the week field is supported, 4#2 means the second Thursday of a month |
5. Example:
*/5 * * * * ? Execute every 5 seconds 0 */1 * * * ? Execute every 1 minute 0 0 2 1 * ? * Executed once every month at 2:00 AM 0 15 10 ? * MON-FRI Execute the job at 10:15 a.m. every day, Monday through Friday 0 15 10 ? 6L 2002-2006 2002 Last Friday of every month through 2006 10am:15 execute job 0 0 23 * * ? Execute once a day at 23:00 0 0 1 * * ? Execute once a day at 1AM 0 0 1 1 * ? Executed once every month at 1:00 AM 0 0 23 L * ? Executed at 23:00 on the last day of every month 0 0 1 ? * L Execute every Sunday at 1AM 0 26,29,33 * * * ? Do it once at 26, 29, 33 0 0 0,13,18,21 * * ? Execute once a day at 0:00, 13:00, 18:00, and 21:00 0 0 10,14,16 * * ? Execute once a day at 10am, 2pm, 4pm 0 0/30 9-17 * * ? Executed every half hour during 9 to 5 working hours 0 0 12 ? * WED Executed every Wednesday at 12 noon 0 0 12 * * ? Trigger every day at 12 noon 0 15 10 ? * * 10 a.m. every day:15 trigger 0 15 10 * * ? 10 a.m. every day:15 trigger 0 15 10 * * ? * 10 a.m. every day:15 trigger 0 15 10 * * ? 2005 2005 10 am every day of the year:15 trigger 0 * 14 * * ? 2pm to 2pm daily:59 Fires every 1 minute during 0 0/5 14 * * ? 2pm to 2pm daily:55 Fires every 5 minutes during 0 0/5 14,18 * * ? 2pm to 2pm daily:55 Period and 6pm to 6pm:55 Fires every 5 minutes during 0 0-5 14 * * ? 2pm to 2pm daily:05 Fires every 1 minute during 0 10,44 14 ? 3 WED Every March Wednesday at 2pm:10 and 2:44 trigger 0 15 10 ? * MON-FRI Monday-Friday 10 a.m.:15 trigger 0 15 10 15 * ? 10am on the 15th of every month:15 trigger 0 15 10 L * ? 10 a.m. on the last day of the month:15 trigger 0 15 10 ? * 6L Last Friday of every month 10am:15 trigger 0 15 10 ? * 6L 2002-2005 2002 Last Friday of every month through 2005 at 10am:15 trigger 0 15 10 ? * 6#3 Triggered at 10:15 am on the third Friday of every month