SpringBoot创建定时任务

简介

自己写代码的随便记录,只会写一写自己想要的东西,防止忘记

启用定时任务功能

使用 @EnableScheduling 开启定时任务功能

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
public class XXXXApiApplication {

	public static void main(String[] args) {
		SpringApplication.run(XXXXApiApplication .class, args);
	}
}

任务

创建一个类,写个方法加上 @Scheduled(cron = “0/10 * * * * ?”)

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author chentf
 */
@Slf4j
@Component
public class ProjectSummaryScheduled {
    
    @Scheduled(cron = "0/10 * * * * ?",fixedDelay = 1000 * 10,initialDelay=1000*10)
    public void excute() {
        log.info("执行项目汇总任务开始");
        long startTime = System.currentTimeMillis();
        //TODO 写业务代码
        long endTime = System.currentTimeMillis();
        long cost = endTime - startTime;
        log.info("执行项目汇总任务结束,耗时:{}ms", cost);
    }
}

相关属性

属性说明
cron按cron规则执行
fixedRate以固定速率执行
fixedDelay上次执行完毕后延迟再执行
initialDelay第一次延时执行,第一次执行完毕后延迟后再次执行

参考:https://blog.youkuaiyun.com/x18707731829/article/details/82682914

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值