springboot(五)Scheduling demo
2024-10-19 05:03:29
在项目开发过程中,经常会使用到定时任务(跑批),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 示例如下
最新文章
- nodejs events模块
- Eclipse搭建Python开发环境+Python中文处理
- Linux-QT 开发环境搭建以及编译镜像
- JS的词法作用域
- Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical
- String中的==与Empty
- 基于STM32的USB枚举过程学习笔记
- web端/h5端账号密码的安全性问题
- blfs(systemd版本)学习笔记-编译安装gnome桌面组件及应用
- Nginx配置,413 Request Entity Too Large错误解决
- Linux驱动的两种载入方式过程分析
- A1037. Magic Coupon
- ClassThird
- Guava Files 源码分析(二)
- Python学习笔记(十)匿名函数
- Java Mail(一):telnet实现发送收取邮件
- xgboost系列之应用xgboost的注意事项
- apue.3e源码下载及编译
- js判断是否是用微信浏览器打开
- [电子书] 《Android编程兵书》PDF