Spring @Scheduled 定时任务 周设置的坑
@Scheduled在spring-context包里。
具体位于org.springframework.scheduling.annotation.Scheduled
其cron字段的注释如下:
/*** A cron-like expression, extending the usual UN*X definition to include triggers* on the second as well as minute, hour, day of month, month and day of week.* <p>E.g. {@code "0 * * * * MON-FRI"} means once per minute on weekdays* (at the top of the minute - the 0th second).* <p>The special value {@link #CRON_DISABLED "-"} indicates a disabled cron trigger,* primarily meant for externally specified values resolved by a ${...} placeholder.* @return an expression that can be parsed to a cron schedule* @see org.springframework.scheduling.support.CronSequenceGenerator*/
String cron() default "";
再看 org.springframework.scheduling.support.CronSequenceGenerator的注释:
/*** Date sequence generator for a* <a href="https://www.manpagez.com/man/5/crontab/">Crontab pattern</a>,* allowing clients to specify a pattern that the sequence matches.** <p>The pattern is a list of six single space-separated fields: representing* second, minute, hour, day, month, weekday. Month and weekday names can be* given as the first three letters of the English names.** <p>Example patterns:* <ul>* <li>"0 0 * * * *" = the top of every hour of every day.</li>* <li>"*/10 * * * * *" = every ten seconds.</li>* <li>"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.</li>* <li>"0 0 6,19 * * *" = 6:00 AM and 7:00 PM every day.</li>* <li>"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day.</li>* <li>"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays</li>* <li>"0 0 0 25 12 ?" = every Christmas Day at midnight</li>* </ul>** @author Dave Syer* @author Juergen Hoeller* @author Ruslan Sibgatullin* @since 3.0* @see CronTrigger*/
public class CronSequenceGenerator {
}
cron表达式的pattern需要查看 https://www.manpagez.com/man/5/crontab/
field | allowed values |
---|---|
minute | 0-59 |
hour | 0-23 |
day of month | 1-31 |
month | 1-12 (or names, see below) |
day of week | 0-7 (0 or 7 is Sun, or use names) |
0和7是周日,那就是:
数字 | 周几 |
---|---|
0 | 周日 |
1 | 周一 |
2 | 周二 |
3 | 周三 |
4 | 周四 |
5 | 周五 |
6 | 周六 |
7 | 周日 |