Spring Boot Schedule定时任务

在项目开发的过程中,可能需要程序定时执行某些操作,比如每个间隔输出、每周每月定时输出等,而在Spring Boot我们可以使用Schedule实现定时的任务。

首先在spring boot的入口类Application.java中,添加@EnableScheduling注解,允许支持schedule

@SpringBootApplication
@EnableScheduling
public class Application{
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }    
}

之后编写定时任务的操作代码
执行类Schedul.java

@Component
public class Schedul {  
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private final static long ONE_Minute = 60 * 1000;

    @Scheduled(fixedDelay=ONE_Minute)
    public void fixedDelay(){
        logger.info("fixedDelay执行...");
    }

    @Scheduled(fixedRate=ONE_Minute)
    public void fixedRate(){
        logger.info("fixedRate执行...");
    }

    @Scheduled(cron="0 0 3 ? * 7")
    public void cron(){
        logger.info("cron定期执行");
    }
}

fixedDelay 每隔多久执行一次 开始-开始
fixedRate 每隔多久执行一次 结束-开始
fixedDelay不管方法进行多久,fixedRate在方法执行完毕后才会计时

自定义cron的配置
这里写图片描述

参考spring boot项目中处理Schedule定时任务

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值