简介
自己写代码的随便记录,只会写一写自己想要的东西,防止忘记
启用定时任务功能
使用 @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