Below is a detailed explanation of all parameters:
second
: seconds, value range: 0~59;
minute
: Minutes, value range: 0~59;
hour
: hour, value range: 0~23;
dayOfMonth
: A certain day in the month, value range: 1~31;
month
: Month, value range: 1~12;
dayOfWeek
: Day of the week, value range: 1~7, where 1 means Sunday, 2 means Monday, and so on;
year
: year, value range: 1970~2099;
persistent
: Whether to persist, the default is true. If true, it means that these scheduled tasks should continue to run even after the application is restarted; if false, it means that these scheduled tasks will no longer run after the application is restarted;
info
: Task description information;
timezone
: Time zone, the default value is "GMT".
Use expressions to control task execution time:
In addition to the above parameters, the @Schedule annotation also supports the use of expressions to control the execution time of tasks. The expression is in the form of a cron expression. This expression defines the time interval and rules for scheduling the execution of the program. The cron expression consists of 6 space-separated time fields. These fields represent, in order, seconds, minutes, hours, date, month, and day of the week. Each field has a set of allowed values, as well as some special characters that can be used to specify different options for execution time. Below is a detailed description of each field:
1. Second (0-59): Indicates which second in one minute the task is executed. For example, cron = "0/5 * * * * *" means starting from 0 seconds and executing the task every 5 seconds.
2. Minute (0-59): Indicates which minute of the hour the task is performed. For example, cron = "0 0/10 * * * *" means that the task will be executed every 10 minutes, starting from 0 minutes.
3. Hour (0-23): Indicates the hour of the day to perform the task. For example, cron = "0 0 12 * * *" means that the task will be executed at 12 noon every day.
4. Date (1-31): Indicates the day of the month to perform the task. For example, cron = "0 0 0 25 12 ?" means that the task is executed at zero o'clock on December 25th every year.
5. Month (1-12): Indicates which month of the year the task is performed. For example, cron = "0 0 0 1 * *" means executing the task on the first day of every month.
6. Day of the week (0-7, Sunday = 0 or 7): Indicates which day of the week the task is performed. For example, cron = "0 0 0 ? * SUN" means executing the task every Sunday.
Note: "*" in a cron expression means all possible values, while "?" means no value is specified (usually used for date and week fields).
Here are some other examples:
0 0 0 * * *
Indicates that the task will be executed at midnight every day0 0 12 * * ?
Indicates that the task will be executed at 12 noon every day0 15 10 ? * *
Indicates that the task will be executed at 10:15 am every day0 0/5 * * * ?
Indicates that starting from 0 seconds, the task will be executed every 5 minutes
Here is an example using the @Schedule annotation:
@Service
public class MyTimer {
@Schedule(second="*/10", minute="*", hour="*")
public void execute() {
System.out.println("定时任务执行了!");
}
}
The above code indicates that a scheduled task will be executed every 10 seconds.