SpringBoot定时任务@EnableScheduling
一、定时任务作用?
定时任务相当于闹钟
在什么时间做什么事情(执行什么命令/脚本)
二、举例说明
1、pom.xml中导入必要的依赖:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<dependencies>
<!-- SpringBoot 核心组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
2、写一个springboot的启动类:
启动类里面使用@EnableScheduling 注解开启功能,自动扫描
@SpringBootApplication
@EnableScheduling //开启定时任务
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
3、新建一个Job类:
- 要在任务的类上写@Component
- 要在任务方法上写@Scheduled
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.util.Date;
/**
* @ClassName Jobs
* @Author jeffrey
* @Description Jobs
**/
@Component
public class Jobs {
//表示方法执行完成后5秒
@Scheduled(fixedDelay = 5000)
public void fixedDelayJob() throws InterruptedException {
System.out.println("fixedDelay 每隔5秒" + new Date());
}
//表示每隔3秒
@Scheduled(fixedRate = 3000)
public void fixedRateJob() {
System.out.println("fixedRate 每隔3秒" + new Date());
}
//表示每天8时30分0秒执行
@Scheduled(cron = "0 0,30 0,8 ? * ? ")
public void cronJob() {
System.out.println(new Date() + " ...>>cron....");
}
}
执行结果如下:
fixedRate 每隔3秒Thu Jun 20 20:26:41 CST 2019
fixedDelay 每隔5秒Thu Jun 20 20:26:43 CST 2019
fixedRate 每隔3秒Thu Jun 20 20:26:44 CST 2019
fixedDelay 每隔5秒Thu Jun 20 20:26:48 CST 2019
三、总结
总结来说,fixedRate下一次执行时间是本次开始时间加间隔时间;而fixedDelay下一次执行时间是本次结束时间加间隔时间
@Scheduled(fixedRate=2000)
public void fixedRate(){
System.out.println("fixedRate:"+new Date());
}
![](https://gitee.com/dl_shrimp/image/raw/master/img/20191124202607648.png)
但是@Scheduled中还有一种属性fixedDelay,两者有什么区别,区别就是如果执行任务中有阻塞的情况,fixedDelay执行间隔会加上阻塞的时间代码如下:
@Scheduled(fixedDelay=2000)
public void fixedDelay(){
try
{
Thread.currentThread().sleep(1000);//毫秒
}
catch(Exception e){}
System.out.println("fixedDelay:"+new Date());
}
![](https://gitee.com/dl_shrimp/image/raw/master/img/20191124203043115.png)
可以看出后一次执行的时间是fixedDelay的时间加阻塞时间;
fixedRate阻塞后的情况
而fixedRate则是按其规定的时间来执行的,犹如时刻表一般,当阻塞结束后按时刻表来执行,代码如下:
@Scheduled(fixedRate=2000)
public void fixedRate(){
try
{
Thread.currentThread().sleep(1000);//毫秒
}
catch(Exception e){}
System.out.println("fixedRate:"+new Date());
}
![](https://gitee.com/dl_shrimp/image/raw/master/img/2019112420340290.png)
图中可以看到,执行结果中时间间隔为fixedRate定的时间间隔
cron中,还有一些特殊的符号,含义如下:
(*)星号:可以理解为每的意思,每秒,每分,每天,每月,每年...
(?)问号:问号只能出现在日期和星期这两个位置。
(-)减号:表达一个范围,如在小时字段中使用“10-12”,则表示从10到12点,即10,11,12
(,)逗号:表达一个列表值,如在星期字段中使用“1,2,4”,则表示星期一,星期二,星期四
(/)斜杠:如:x/y,x是开始值,y是步长,比如在第一位(秒) 0/15就是,从0秒开始,每15秒,最后就是0,15,30,45,60 另:*/y,等同于0/y
下面列举几个例子供大家来验证:
0 0 3 * * ? 每天3点执行
0 5 3 * * ? 每天3点5分执行
0 5 3 ? * * 每天3点5分执行,与上面作用相同
0 5/10 3 * * ? 每天3点的 5分,15分,25分,35分,45分,55分这几个时间点执行
0 10 3 ? * 1 每周星期天,3点10分 执行,注:1表示星期天
0 10 3 ? * 1#3 每个月的第三个星期,星期天 执行,#号只能出现在星期的位置
/**
* 定时任务,每月 15 号 0 点整进行固定资产折旧,1/1这里表示的是每个月
*/
@Scheduled(cron = "0 0 0 15 1/1 ?")