在项目开发过程中,经常会使用到定时任务(跑批),springboot默认已经实现了,只需要添加相应的注解就可以实现

在启动类上加入注解,开启定时任务

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

创建跑批任务并注册到spring中管理,并在方法上加上跑批注解,配置core表达式

@Component
public class SchedulingTask1 { private int count = 0; @Scheduled(cron = "*/5 * * * * ?")//每5s执行一次
public void process(){
System.out.println("SchedulingTask1 is "+ count+"times run");
count++;
}
}
@Component
public class SchedulingTask2 { SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMMdd hh:mm:ss"); @Scheduled(fixedRate = 5000)//每5s执行一次
public void process(){
System.out.println("process->This time is "+ sdf.format(new Date()) +"times run");
} @Scheduled(fixedDelay = 10000)//上一次执行完毕时间点之后10秒再执行
public void process2(){
System.out.println("process2->This time is "+ sdf.format(new Date()) +"times run");
} @Scheduled(initialDelay=10000, fixedRate=5000) //第一次延迟10秒后执行,之后按fixedRate的规则每5秒执行一次
public void process3(){
System.out.println("process3->This time is "+ sdf.format(new Date()) +"times run");
}
}

启动项目即可运行,运行结果:

process->This time  is  20200510 11:43:37times run
process2->This time is 20200510 11:43:37times run
SchedulingTask1 is 0times run
process->This time is 20200510 11:43:42times run
SchedulingTask1 is 1times run
process->This time is 20200510 11:43:47times run
process3->This time is 20200510 11:43:47times run
process2->This time is 20200510 11:43:47times run
SchedulingTask1 is 2times run
process->This time is 20200510 11:43:52times run
process3->This time is 20200510 11:43:52times run
SchedulingTask1 is 3times run
process->This time is 20200510 11:43:57times run
process3->This time is 20200510 11:43:57times run
process2->This time is 20200510 11:43:57times run
SchedulingTask1 is 4times run
process->This time is 20200510 11:44:02times run
process3->This time is 20200510 11:44:02times run
SchedulingTask1 is 5times run
process->This time is 20200510 11:44:07times run
process3->This time is 20200510 11:44:07times run
process2->This time is 20200510 11:44:07times run
SchedulingTask1 is 6times run
process->This time is 20200510 11:44:12times run
process3->This time is 20200510 11:44:12times run
SchedulingTask1 is 7times run
process->This time is 20200510 11:44:17times run
process3->This time is 20200510 11:44:17times run
process2->This time is 20200510 11:44:17times run
SchedulingTask1 is 8times run

常用表达式如下:

0 * * * * ? 每1分钟执行一次
0 0 * * * ? 每天每1小时执行一次
0 0 10 * * ? 每天10点执行一次
0 * 14 * * ? 在每天下午14点到下午14:59期间的每1分钟执行一次
0 30 10 1 * ? 每月1号上午10点30执行一次
0 10 10 10 * ? 每月10日上午10:10执行一次 */5 * * * * ? 每隔5秒执行一次
0 */1 * * * ? 每隔1分钟执行一次
0 0 12-15 * * ? 每天12-15点整点执行一次
0 0/5 * * * ? 每5分钟执行一次
0 0-5 15 * * ? 在每天下午15点到下午15:05期间的每1分钟执行一次
0 0/10 15 * * ? 在每天下午15点到下午15:50期间的每10分钟执行一次
0 0/10 15,18 * * ? 在每天下午15点到15:50期间和下午18点到18:50期间的每10分钟执行一次
0 0/30 10-15 * * ? 在每天上午10点到下午15:30每半小时执行一次
0 0 10,12,14 * * ? 每天上午10点,下午12点,14点执行一次

表达式生成、解析、反解析地址https://cron.qqe2.com/  https://qqe2.com/cron 示例如下

最新文章

  1. nodejs events模块
  2. Eclipse搭建Python开发环境+Python中文处理
  3. Linux-QT 开发环境搭建以及编译镜像
  4. JS的词法作用域
  5. Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical
  6. String中的==与Empty
  7. 基于STM32的USB枚举过程学习笔记
  8. web端/h5端账号密码的安全性问题
  9. blfs(systemd版本)学习笔记-编译安装gnome桌面组件及应用
  10. Nginx配置,413 Request Entity Too Large错误解决
  11. Linux驱动的两种载入方式过程分析
  12. A1037. Magic Coupon
  13. ClassThird
  14. Guava Files 源码分析(二)
  15. Python学习笔记(十)匿名函数
  16. Java Mail(一):telnet实现发送收取邮件
  17. xgboost系列之应用xgboost的注意事项
  18. apue.3e源码下载及编译
  19. js判断是否是用微信浏览器打开
  20. [电子书] 《Android编程兵书》PDF

热门文章

  1. mysql InnoDB架构
  2. 使用fdopen对python进程产生的文件进行权限最小化配置
  3. uni-app 开发随笔(踩坑记录)
  4. Jmeter的Cookie管理器调试与参数化
  5. By default, the connection will be closed if the proxied server does not transmit any data within 60 seconds.
  6. 长连接开发踩坑之netty OOM问题排查实践
  7. tricks - 思维
  8. 请你尽量全面的说一个对象在 JVM 内存中的结构?
  9. okhttp踩坑
  10. JavaWeb——过滤器及监听器