SpringBoot定时任务@EnableScheduling

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());
    }

但是@Scheduled中还有一种属性fixedDelay,两者有什么区别,区别就是如果执行任务中有阻塞的情况,fixedDelay执行间隔会加上阻塞的时间代码如下:

 @Scheduled(fixedDelay=2000)
    public void fixedDelay(){
        try
        {
            Thread.currentThread().sleep(1000);//毫秒
        }
        catch(Exception e){}
        System.out.println("fixedDelay:"+new Date());
    }

可以看出后一次执行的时间是fixedDelay的时间加阻塞时间;

fixedRate阻塞后的情况

而fixedRate则是按其规定的时间来执行的,犹如时刻表一般,当阻塞结束后按时刻表来执行,代码如下:

 @Scheduled(fixedRate=2000)
    public void fixedRate(){
    try
        {
            Thread.currentThread().sleep(1000);//毫秒
        }
        catch(Exception e){}
        System.out.println("fixedRate:"+new Date());
    }

图中可以看到,执行结果中时间间隔为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 ?")
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值