前言
在springboot中使用定时定时任务可以说是一种享受
依赖
不需要引入其他的依赖,springboot框架的默认依赖就足以满足
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.0</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
开启注解
在启动类上需要加入@EnableScheduling注解,然后在需要定时执行的方法上面加上@Scheduled注解,常用参数类型如下
- cron 使用cron表达式,格式为
0 * * * * MON-FRI,文档解释为- second
- minute
- hour
- day of month
- month
- day of week
@Scheduled(cron = "0/5 * * * * ?")
public void test(){
System.out.println("test开始执行时间:"+ LocalDateTime.now().toLocalTime());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test执行完成时间:"+ LocalDateTime.now().toLocalTime());
}
运行结果
test开始执行时间:18:03:20.003
test执行完成时间:18:03:30.004
test开始执行时间:18:03:35
test执行完成时间:18:03:45.002
test开始执行时间:18:03:50.001
test执行完成时间:18:04:00.005
test开始执行时间:18:04:05.002
test执行完成时间:18:04:15.006
test开始执行时间:18:04:20.005
test执行完成时间:18:04:30.006
test开始执行时间:18:04:35
test执行完成时间:18:04:45.005
test开始执行时间:18:04:50
test执行完成时间:18:05:00.005
test开始执行时间:18:05:05.001
test执行完成时间:18:05:15.004
test开始执行时间:18:05:20.001
test执行完成时间:18:05:30.001
test开始执行时间:18:05:35.003
test执行完成时间:18:05:45.006
test开始执行时间:18:05:50.001
test执行完成时间:18:06:00.003
test开始执行时间:18:06:05.003
- fixedDelay 官方解释
Execute the annotated method with a fixed period in milliseconds between the end of the last invocation and the start of the next.
翻译过来是差不多这个意思:在上一次调用结束和下一次调用开始之间延迟指定的以毫秒为单位的固定时间段,执行带注解的方法。特殊例子,设置的fixedDelay=5000毫秒,而执行此方法需要10s,假如第一次执行时间为00:00:00,那么第二次执行的时间为00:00:15,第三次执行的时间为00:00:30
@Scheduled(fixedDelay = 5000)
public void test1() {
System.out.println("test开始执行时间:" + LocalDateTime.now().toLocalTime());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test执行完成时间:" + LocalDateTime.now().toLocalTime());
}
执行结果:
test开始执行时间:18:10:07.093
test执行完成时间:18:10:17.096
test开始执行时间:18:10:22.100
test执行完成时间:18:10:32.101
test开始执行时间:18:10:37.103
test执行完成时间:18:10:47.107
test开始执行时间:18:10:52.111
- fixedRate 官方解释
Execute the annotated method with a fixed period in milliseconds between invocations.
每隔指定的毫秒数执行此方法,如果上一次的任务没有执行完毕,那么下一次执行的时间是在上一次执行完之后,立即执行,举个例子,设置的fixedRate=5000毫秒,而执行此方法需要10s,假如第一次执行时间为00:00:00,那么第二次执行的时间为00:00:10,第三次执行的时间为00:00:20
@Scheduled(fixedRate = 5000)
public void test1() {
System.out.println("test开始执行时间:" + LocalDateTime.now().toLocalTime());
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("test执行完成时间:" + LocalDateTime.now().toLocalTime());
}
执行结果
test开始执行时间:18:08:13.248
test执行完成时间:18:08:23.248
test开始执行时间:18:08:23.249
test执行完成时间:18:08:33.251
test开始执行时间:18:08:33.251
test执行完成时间:18:08:43.252
test开始执行时间:18:08:43.252
test执行完成时间:18:08:53.254
test开始执行时间:18:08:53.255
test执行完成时间:18:09:03.258
test开始执行时间:18:09:03.259
test执行完成时间:18:09:13.259
test开始执行时间:18:09:13.259
test执行完成时间:18:09:23.260
Spring Boot中定时任务的使用
本文介绍了在Spring Boot中使用定时任务的方法。无需引入其他依赖,使用框架默认依赖即可。在启动类和需定时执行的方法上添加注解,还介绍了常用参数类型,如cron、fixedDelay、fixedRate,并给出了相应的执行结果示例。
4373

被折叠的 条评论
为什么被折叠?



