SpringBoot-定时任务

JDK

Timer,Java自带定时任务类,使用较少。

SpringBoot

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter</artifactId>
</dependency>

启动类上加注解@EnableScheduling即可开启SpringBoot定时任务。
定时任务配置

@Component
public class ScheduledTask {
    @Scheduled(cron="${cron.schedule}") //cron表达式
    public void run1(){ ... }
	
    @Scheduled(fixedDelay = 2000) //上一次执行完毕时间点之后多长时间再执行(ms)
    public void run2(){ ... }
	
    @Scheduled(fixedDelayString = "${time.fixedDelay}") //与fixedDelay相同,区别是:1.时间是字符串;2.支持占位符
    public void run3(){ ... }
}

Quartz

SpringBoot V2.0以后版本,直接引入Quartz调度框架启动器

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-quartz</artifactId>
</dependency>
posted @ 2020-04-16 13:08  万箭穿心,习惯就好。  阅读(191)  评论(0编辑  收藏  举报