Spring Boot中使用自带的定时任务特别简单,只需要在启动类上使用@EnableScheduling开启定时任务、@Scheduled设置定时任务即可
一、启动类开启定任务
启动类上使用@EnableScheduling注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package net.zuze;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling;
@SpringBootApplication @EnableScheduling public class ScheduledDemoApplication { public static void main(String[] args) { SpringApplication.run(ScheduledDemoApplication.class,args); System.out.println("--- --- 启动完成时间:"+System.currentTimeMillis()/1000); } }
|
二、@Scheduled设置定时任务
在方法上使用@Scheduled设置定时任务、定时任务设置有三种类型
1、cron方式
从第0秒开始每10秒执行一次、zone可以指定时序(也可以省略、使用服务器的默认时区)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package net.zuze.job;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
@Component public class CronJob {
@Scheduled(cron = "0/10 * * * ? *", zone = "Asia/Shanghai") public void cronSchedule(){ System.out.println("--- --- cron 方 式:"+System.currentTimeMillis()/1000); } }
|
效果
1 2 3 4 5 6 7
| --- --- 启动完成时间:1671018307 --- --- cron 方 式:1671018310 --- --- cron 方 式:1671018320 --- --- cron 方 式:1671018330 --- --- cron 方 式:1671018340 --- --- cron 方 式:1671018350 --- --- cron 方 式:1671018360
|
2、fixedRate(单位毫秒) 方式
固定频率(启动后立马执行第一次)、程序执行的耗时、不影响下一次定时任务的执行
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package net.zuze.job;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
@Component public class FixedRateJob {
@Scheduled(fixedRate = 5000) public void fixedRateSchedule() throws InterruptedException { Thread.sleep(3000); System.out.println("----fixedDelay方式:" + System.currentTimeMillis() / 1000); }
}
|
效果
1 2 3 4 5 6 7
| --- --- 启动完成时间:1671018638 -----fixedRate方式:1671018641 -----fixedRate方式:1671018646 -----fixedRate方式:1671018651 -----fixedRate方式:1671018656 -----fixedRate方式:1671018661 -----fixedRate方式:1671018666
|
3、fixedDelay(单位毫秒) 方式
固定间隔方式(启动后立马执行第一次)、程序的执行的耗时影响定时任务的下一次执行(下一次执行需要再定时任务执行结束后重新计算间隔时间)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| package net.zuze.job;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
@Component public class FixedRateJob {
@Scheduled(fixedRate = 5000) public void fixedRateSchedule() throws InterruptedException { Thread.sleep(3000); System.out.println("-----fixedRate方式:" + System.currentTimeMillis() / 1000); }
}
|
效果
1 2 3 4 5 6 7
| --- --- 启动完成时间:1671018412 ----fixedDelay方式:1671018415 ----fixedDelay方式:1671018423 ----fixedDelay方式:1671018431 ----fixedDelay方式:1671018439 ----fixedDelay方式:1671018447 ----fixedDelay方式:1671018455
|
4、带初始延时的定时任务(单位毫秒)
initialDelay 程序启动后第一次定时任务执行的延迟时间(initialDelay默认值为0、程序启动立马执行)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| package net.zuze.job;
import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component;
@Component public class InitialDelayJob {
@Scheduled(fixedDelay = 5000,initialDelay = 3000) public void initialDelaySchedule(){ System.out.println("--initialDelay方式:"+System.currentTimeMillis()/1000); } }
|
效果
1 2 3 4 5 6 7 8
| --- --- 启动完成时间:1671018770 --initialDelay方式:1671018772 --initialDelay方式:1671018777 --initialDelay方式:1671018782 --initialDelay方式:1671018787 --initialDelay方式:1671018792 --initialDelay方式:1671018797 --initialDelay方式:1671018802
|