Spring @Scheduled Annotation

1.Overview

这里我们将会学习Spring @Scheduled 标签,了解它是如何配置,如何设置定时任务。

关于它的使用,有两点简单的规则需要记住:

※它的方法应该是一个void返回值类型

※它的方法不能接收任何参数

2.配置

如下所示,我们使用注解来进行配置。

 1 @SpringBootApplication
 2 @EnableScheduling                //这个注解开启定时任务的功能
 3 public class AppReadCsv {
 4     @Autowired
 5     JobLauncher jobLauncher;
 6 
 7     @Autowired
 8     Job         job;
 9 
10     public static void main(String[] args) {
11         SpringApplication.run(App.class, args);
12     }
13 
14     /*
15      * batch job will run every one minute after application is started
16      * 
17      * */
18     @Scheduled(cron = "0 */1 * * * ?")
19     public void perform() throws Exception {
20         JobParameters params = new JobParametersBuilder()
21             .addString("JobID", String.valueOf(System.currentTimeMillis()))
22             .toJobParameters();
23         jobLauncher.run(job, params);
24     }

3.Schedule a task at Fixed Delay

上次任务的结束和下次任务的开始,中间的间隔时间是固定的。

1     @Scheduled(fixedDelay = 1000)     // millisecond
2     public void scheduleFixedDelayTask() {
3         System.out.println(
4           "Fixed delay task - " + System.currentTimeMillis() / 1000);
5     }

4.Schedule a task at a Fixed Rate

以固定的间隔时间来执行任务

1     @Scheduled(fixedRate = 1000)      // millisecond
2     public void scheduleFixedRateTask() {
3         System.out.println(
4           "Fixed rate task - " + System.currentTimeMillis() / 1000);
5     }

上一次任务开始后的一秒,下一次任务开始执行。

注意:下次任务的开始必须等到上次任务的结束,如果上次任务没有结束,那么及时到了设置的一秒间隔时间,那么下次任务也不会开始执行。

如果想要任务并行执行,需要使用@Async标签

 1 @EnableAsync
 2     public class ScheduledFixedRateExample {
 3         @Async
 4         @Scheduled(fixedRate = 1000)
 5         public void scheduleFixedRateTaskAsync() throws InterruptedException {
 6             System.out.println(
 7               "Fixed rate task async - " + System.currentTimeMillis() / 1000);
 8             Thread.sleep(2000);
 9         } 
10     }

5.Schedule tasks with initial delay

1 @Scheduled(fixedDelay = 1000, initialDelay = 1000)    // millisecond
2     public void scheduleFixedRateWithInitialDelayTask() {
3      
4         long now = System.currentTimeMillis() / 1000;
5         System.out.println(
6           "Fixed rate task with one second initial delay - " + now);
7     }

6.Cron Expressions

1 @Scheduled(cron = "0 15 10 15 * ?")   // 执行时间 每个月第15天上午10:15执行.
2     public void scheduleTaskUsingCronExpression() {
3      
4         long now = System.currentTimeMillis() / 1000;
5         System.out.println(
6           "schedule tasks using cron jobs - " + now);
7     }

表达式的说明:cron = "0 15 10 15 * ?"

<second> <minute> <hour> <day-of-month> <month> <day-of-week> <year> <command>

cron表达式中共有6个字符(space当做分隔符,不算)。分别对应上一行,红字部分。<year>作为可选项。

* 表示All。在month的位置,表示for every month

?表示any。使用在<day-of-month> and <day-of -week> 这两个选项中。

– 表示range 。例如,“10-11” in <hour> 表示 10点和11点

, 表示分隔多个数值 。例如, “MON, WED, FRI” 在 <day-of-week> 表示“Monday, Wednesday, and Friday”

下面举实例说明:

0 0 12 * * ? 2017          2017年每天的12点执行
0 0/5 13,18 * * ? 每天13点和18点的0,5,10,15,20,25,30,35,40,45,50,55分执行
0 15,45 13 ? 6 Tue 6月的每周二13点的15分和45分执行

参考链接:

https://www.baeldung.com/spring-scheduled-tasks

https://www.baeldung.com/cron-expressions

原文地址:https://www.cnblogs.com/lihao007/p/13766802.html