Spring Boot implements timed tasks

In actual project development, it is inevitable to encounter scenarios where tasks are executed regularly; for example, how often do you need to do something; what day does it need to do every month; business needs like this will often be encountered; then How to achieve such business requirements. Just recently using spring  Boot to develop projects, I will briefly describe Spring Boot as an example:

I read through the ready-made timing annotations in Spring Boot, first come to Code: The 
realization goal is: timed execution every 10 seconds 

/** 
* 定时任务 
* @author Alan 
* 2016-07-19 
*/ 
@Component 
@EnableScheduling 
public class ScheduledTask {

/**
 * 目标:实现定时任务;实例中是每隔3秒钟执行一次
 */

private Integer count_first = 1;
private Integer count_second = 1;
private Integer count_three = 1;

@Scheduled(fixedRate = 10000)
public void printCurrentTime() throws InterruptedException {
    System.out.println(String.format("① 第%s次执行,当前时间为:%s", count_first++, dateFormat.format(new Date())));
}

@Scheduled(fixedDelay = 10000)
public void printCurrentTimeAfterSleep() throws InterruptedException {
    System.out.println(String.format("② 第%s次执行,当前时间为:%s", count_second++, dateFormat.format(new Date())));
}

@Scheduled(cron = "*/10 * * * * *")
public void printCurrentTimeCron() throws InterruptedException {
    System.out.println(String.format("③ 第%s次执行,当前时间为:%s", count_three++, dateFormat.format(new Date())));
}

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

1. The Spring Boot implementation is so simple, among which:

@Scheduled add 
@Component and @EnableScheduling on the method to be executed regularly: mark the start of the scheduled task

It is not difficult to find that the expressions in @Scheduled are different, but achieve the same function

1) fixedRate = 10000 means that every 10,000ms, Spring scheduling will call the method, regardless of the execution time of the method 
2) fixedDelay = 10000 means that when the method finishes executing 10,000 times, Spring scheduling will call the method again 
3) cron = " /10  * * * * *" provides a general timing task expression, which means that it is executed every 10 seconds. For more detailed information, please refer to the cron expression.

2. cron expression (extract)

Introduce a cron Expression expression.

This part is excerpted:

Field Allowed Values ​​Allowed Special Characters
Second 0-59 , - * /
Minute 0-59 , - * /
Hour 0-23 , - * /
date 1-31 , - * / L W C
month 1-12 or JAN-DEC , - * /
Week 1-7 or SUN-SAT , - * / L C #
year (optional) Leave blank, 1970-2099 , - * /

As the above expression shows:

The " " character is used to specify all values. Such as:" "In the field of minutes, it means "every minute".

The "-" character is used to specify a range. For example: "10-12" means "10 o'clock, 11 o'clock, 12 o'clock" in the hour field.

The "," character is used to specify additional values. For example: "MON, WED, FRI" means "Monday, Wednesday, Friday" in the week field.

The "?" character is only used in date and week fields. It is used to specify "non-explicit values". It is useful when you need to specify something via one of these two fields. Look at the example below and you will understand.

The "L" character specifies the day (last day) of the month or week. That is "Last" abbreviation. However, "L" in the week and month means different meanings, for example: in the month subsection, "L" refers to the last day of the month - January 31, February 28, if it is in the week field, it is simply expressed as "7" or "SAT". If it is after a certain value in the week field, it means "the last week value of a certain month", such as "6L" means the last Friday of a certain month.

The "W" character can only be used in the month field, which specifies the Sunday closest to the specified date.

The "#" character can only be used in the week field, which specifies which week the value is in a month

Each element can explicitly specify a value (such as 6), a range (such as 9-12), a list (such as 9, 11, 13) or a wildcard (such as *). The "day of month" and "day of week" elements are mutually exclusive, so you should indicate which field you do not want to set by setting a question mark (?). Some examples of cron expressions and their meanings are shown in Table 7.1:

expression meaning
“0 0 12 * * ?” Trigger every day at 12 noon
“0 15 10 ? * *” Trigger every day at 10:15am
“0 15 10 * * ?” Trigger every day at 10:15am
“0 15 10 * * ? *” Trigger every day at 10:15am
“0 15 10 * * ? 2005” Fired at 10:15 am every day in 2005
“0 * 14 * * ?” Fires every 1 minute between 2pm and 2:59pm every day
“0 0/5 14 * * ?” Fires every 5 minutes between 2pm and 2:55pm every day
“0 0/5 14,18 * * ?” Fires every 5 minutes between 2pm and 2:55pm and every 5 minutes between 6pm and 6:55pm
“0 0-5 14 * * ?” Fires every 1 minute between 2pm and 2:05pm every day
“0 10,44 14 ? 3 WED” Every March Wednesday at 2:10pm and 2:44pm
“0 15 10? * MON-FRI” Triggered at 10:15 a.m. Monday-Friday
“0 15 10 15 * ?” Triggered at 10:15 am on the 15th of every month
“0 15 10 L * ?” Triggered at 10:15 AM on the last day of the month
“0 15 10 ? * 6L” Fires on the last Friday of the month at 10:15 AM
“0 15 10 ? * 6L 2002-2005” Fired at 10:15 am on the last Friday of every month from 2002 to 2005
“0 15 10 ? * 6#3” Fires at 10:15 AM on the third Friday of the month
0 6 * * * every morning at 6 am
/2 * * every two hours
0 23-7/2,8 * * * Every two hours between 11pm and 8am, at 8am
0 11 4 * 1-3 4th of every month and every Monday to Wednesday at 11:00 am
0 4 1 1 * January 1st at 4 am

 

3.quartz implements timed tasks

Triggers in quartz, if designed to trigger execution at a certain time 
, add maven dependencies in Spring Boot

<!-- https://mvnrepository.com/artifact/org.quartz-scheduler/quartz -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.2.3</version>
</dependency>

An example of tbschedule will be made in subsequent chapters

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327039234&siteId=291194637