Springboot @Schedule 多个定时任务积压解决
背景
应用中我们通常采用定时任务不断监听配置文件或者采集一些应用状态信息等等,这些任务通常间隔比较短,频次比较高。随着定时任务增多,定时任务偶尔出现了积压问题,即:定时任务总是顺序执行的,无法并行执行,此时当某个定时任务卡住时,会影响其他定时任务的执行,这样的结果不是我们想看到的。
现象
创建了两个定时任务
- TestSchedule1 每 3 秒执行一次
- TestSchedule2 每 2 秒执行一次
@Scheduled(cron = "0/3 * * * * ?")public void TestSchedule1(){log.info("我是定时任务1");}@Scheduled(fixedRate = 2000)public void TestSchedule2(){log.info("我是定时任务2");}
此时日志输出
2022-03-26 22:02:00.547 INFO 35776 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
2022-03-26 22:02:02.549 INFO 35776 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
2022-03-26 22:02:03.002 INFO 35776 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务1
2022-03-26 22:02:04.549 INFO 35776 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
2022-03-26 22:02:06.005 INFO 35776 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务1
2022-03-26 22:02:06.547 INFO 35776 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
2022-03-26 22:02:08.551 INFO 35776 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
看着非常合理,两个定时任务都是按照既定时间间隔开始执行。
现在我们调整一下任务,让任务1的每次执行耗时 5 秒
@Scheduled(cron = "0/3 * * * * ?")public void TestSchedule1(){try {TimeUnit.SECONDS.sleep(5);} catch (InterruptedException e) {log.error("中断异常:{}", e);}log.info("我是定时任务1");}
2022-03-26 22:16:41.009 INFO 84071 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务1
2022-03-26 22:16:41.010 INFO 84071 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
2022-03-26 22:16:41.010 INFO 84071 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
2022-03-26 22:16:41.493 INFO 84071 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
2022-03-26 22:16:47.001 INFO 84071 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务1
2022-03-26 22:16:47.002 INFO 84071 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
2022-03-26 22:16:47.002 INFO 84071 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
2022-03-26 22:16:47.494 INFO 84071 --- [ scheduling-1] com.wozaizhe55.demo.service.TestService : 我是定时任务2
从日志中我们发现,定时任务2 在定时任务1结束之后连续执行了3次(间隔非常短),按照设想它的时间间隔应该是2秒才对,这样连续执行不符合咱们的设计预期。
原因
定时任务2之所以连续执行了3次是因为定时任务产生了积压,从日志中可以看到执行定时任务的线程为:scheduling-1
,且任务1 和任务 2 都是这个线程,所以当任务1执行的5秒中任务 2只能等着,任务1 结束后把积压的任务一下子执行了。
解决方法
创建一个线程池,让定时任务在线程池中之行。这里先列出方案,具体实现挖个坑下次说。
- 创建一个线程池,并交给spring 管理;
- 定时任务上添加@Async注解,指定线程池名字;
Springboot @Schedule 多个定时任务积压解决相关推荐
- 玩转 SpringBoot 2 之整合定时任务篇
前言 通过本文你将了解到如何在 SpringBoot 2 中整合定时任务使用教程,具体详细内容如下: SpringBoot 自带定时任务使用教程 SpringBoot 集成 JDK 定时任务使用教程 ...
- java quartz 动态执行,浅谈SpringBoot集成Quartz动态定时任务
SpringBoot自带schedule 沿用的springboot少xml配置的优良传统,本身支持表达式等多种定时任务 注意在程序启动的时候加上@EnableScheduling @Schedule ...
- SpringBoot结合Quartz实现定时任务
<从零打造项目>系列文章 工具 比MyBatis Generator更强大的代码生成器 ORM框架选型 SpringBoot项目基础设施搭建 SpringBoot集成Mybatis项目实操 ...
- SpringBoot@Schedule入门基础
SpringBoot@Schedule入门基础 前言 Schedule是一个任务调度器,SpringBoot中可定时触发执行定时任务. 一.基本概念 在SpringBoot中,使用 @Schedule ...
- SpringBoot Schedule的三种使用方式
SpringBoot Schedule的三种使用方式 静态schedule 结果图 动态schedule schedule代码 结果图 mapper代码 application.yml文件配置 pom ...
- springboot springmvc 抛出全局异常解决方法
springboot springmvc 抛出全局异常解决方法 参考文章: (1)springboot springmvc 抛出全局异常解决方法 (2)https://www.cnblogs.com/ ...
- python用schedule模块实现定时任务
python用schedule模块实现定时任务 import schedule import timedef test():print("I'm working...") def ...
- 案例:Xshell 成功创建定时任务(解决no crontab for root using an empty one问题)- 最新版
案例:Xshell 成功创建定时任务(解决no crontab for root using an empty one问题)- 最新版 网上众说纷纭: 有说是:select-editor问题? 有说是 ...
- SpringBoot使用@Scheduled创建定时任务
定时任务一般会存在中大型企业级项目中,为了减少服务器.数据库的压力往往会采用时间段性的去完成某些业务逻辑.比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回 ...
最新文章
- 搜索Idiot就出现特朗普图片,算法无罪!
- Windows原生运行Linux的技术细节
- 【项目管理】你理解的项目管理是什么样的?
- 鼠标的计算机基础知识,2、使用鼠标--电脑基础知识
- 如何将dataset中的值赋值给datatable_金融行业实战项目:如何理解业务?
- SAP Business One助力洛德集团实现巨大商业价值
- python中的单例模式_Python单例模式
- Linux内核部件分析 连通世界的list
- NetAssist.exe网络调试工具
- 【3阶范德蒙行列式计算】
- Unity3D 异步加载
- C语言实现文件分割功能
- 购物网站HTML(首页)
- PyTorch学习笔记
- k8s学习-CKA考试必过宝典
- python获取set中某些元素_取集合中元素_Python Set集合
- java uri用法_javaurl类的用法
- 运维工程师的职责和前景 1
- 解决报错:soundfile.LibsndfileError: Error opening ‘.wav‘: File contains data in an unknown format.
- python 不等于None 不等于空_干货 | 健身前后的黄金饮食法则,不懂等于白练!