从零开始 Spring Boot 40:定时任务
定时任务是一种很常见的需求,比如我们可能需要应用定期去执行一些清理工作,再比如可能需要定期检查一些外部服务的可用性等。
fixedDelay
要在 Spring 中开启定时任务相关功能,需要在任意的配置类上添加上@EnableScheduling
:
@Configuration
@EnableScheduling
public class WebConfig {
}
之后就可以在 Spring Bean 中定义一个定时任务对应的方法:
@Component
public class MySchedule {
@Scheduled(fixedDelay = 1000)
public void sayHello() {
LocalDateTime now = LocalDateTime.now();
System.out.println("hello, now is %s".formatted(now));
}
}
在这个示例中,@Scheduled(fixedDelay = 1000)
表示所标记的方法将每隔1000毫秒(1秒)执行一次。fixedDelay
中的值代表下一次任务将在前一次任务执行完毕后的若干毫秒后执行。
所以这个示例执行后能看到类似下面的输出:
hello, now is 2023-06-14T15:27:21.026108300
hello, now is 2023-06-14T15:27:22.034320100
hello, now is 2023-06-14T15:27:23.038852900
hello, now is 2023-06-14T15:27:24.054180300
hello, now is 2023-06-14T15:27:25.065741600
要注意的是,定时任务对应的方法返回值必须是
void
,且不能包含任何参数。
fixedRate
除了上边介绍的方式外,还可以指定“每间隔若干时间后执行定时任务”这种模式:
@Component
public class MySchedule {
@Scheduled(fixedRate = 1000)
public void sayHello() {
LocalDateTime now = LocalDateTime.now();
System.out.println("hello, now is %s".formatted(now));
}
}
@Scheduled
的fixedRate
属性可以设置每次任务执行间隔的毫米数。
fixedDelay vs fixedRate
fixedDelay
和fixedRate
是有区别的,虽然上面的两个例子输出看起来一样,但这是因为任务执行的时间太短造成的,我们将任务执行的时间放长:
@Component
public class MySchedule {
@Scheduled(fixedDelay = 1000)
public void sayHello() throws InterruptedException {
LocalDateTime now = LocalDateTime.now();
System.out.println("hello, now is %s".formatted(now));
Thread.sleep(2000);
}
}
输出:
hello, now