背景

应用中我们通常采用定时任务不断监听配置文件或者采集一些应用状态信息等等,这些任务通常间隔比较短,频次比较高。随着定时任务增多,定时任务偶尔出现了积压问题,即:定时任务总是顺序执行的,无法并行执行,此时当某个定时任务卡住时,会影响其他定时任务的执行,这样的结果不是我们想看到的。

现象

创建了两个定时任务

  • 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 结束后把积压的任务一下子执行了。

解决方法

创建一个线程池,让定时任务在线程池中之行。这里先列出方案,具体实现挖个坑下次说。

  1. 创建一个线程池,并交给spring 管理;
  2. 定时任务上添加@Async注解,指定线程池名字;

Springboot @Schedule 多个定时任务积压解决相关推荐

  1. 玩转 SpringBoot 2 之整合定时任务篇

    前言 通过本文你将了解到如何在 SpringBoot 2 中整合定时任务使用教程,具体详细内容如下: SpringBoot 自带定时任务使用教程 SpringBoot 集成 JDK 定时任务使用教程 ...

  2. java quartz 动态执行,浅谈SpringBoot集成Quartz动态定时任务

    SpringBoot自带schedule 沿用的springboot少xml配置的优良传统,本身支持表达式等多种定时任务 注意在程序启动的时候加上@EnableScheduling @Schedule ...

  3. SpringBoot结合Quartz实现定时任务

    <从零打造项目>系列文章 工具 比MyBatis Generator更强大的代码生成器 ORM框架选型 SpringBoot项目基础设施搭建 SpringBoot集成Mybatis项目实操 ...

  4. SpringBoot@Schedule入门基础

    SpringBoot@Schedule入门基础 前言 Schedule是一个任务调度器,SpringBoot中可定时触发执行定时任务. 一.基本概念 在SpringBoot中,使用 @Schedule ...

  5. SpringBoot Schedule的三种使用方式

    SpringBoot Schedule的三种使用方式 静态schedule 结果图 动态schedule schedule代码 结果图 mapper代码 application.yml文件配置 pom ...

  6. springboot springmvc 抛出全局异常解决方法

    springboot springmvc 抛出全局异常解决方法 参考文章: (1)springboot springmvc 抛出全局异常解决方法 (2)https://www.cnblogs.com/ ...

  7. python用schedule模块实现定时任务

    python用schedule模块实现定时任务 import schedule import timedef test():print("I'm working...") def ...

  8. 案例:Xshell 成功创建定时任务(解决no crontab for root using an empty one问题)- 最新版

    案例:Xshell 成功创建定时任务(解决no crontab for root using an empty one问题)- 最新版 网上众说纷纭: 有说是:select-editor问题? 有说是 ...

  9. SpringBoot使用@Scheduled创建定时任务

    定时任务一般会存在中大型企业级项目中,为了减少服务器.数据库的压力往往会采用时间段性的去完成某些业务逻辑.比较常见的就是金融服务系统推送回调,一般支付系统订单在没有收到成功的回调返回内容时会持续性的回 ...

最新文章

  1. 搜索Idiot就出现特朗普图片,算法无罪!
  2. Windows原生运行Linux的技术细节
  3. 【项目管理】你理解的项目管理是什么样的?
  4. 鼠标的计算机基础知识,2、使用鼠标--电脑基础知识
  5. 如何将dataset中的值赋值给datatable_金融行业实战项目:如何理解业务?
  6. SAP Business One助力洛德集团实现巨大商业价值
  7. python中的单例模式_Python单例模式
  8. Linux内核部件分析 连通世界的list
  9. NetAssist.exe网络调试工具
  10. 【3阶范德蒙行列式计算】
  11. Unity3D 异步加载
  12. C语言实现文件分割功能
  13. 购物网站HTML(首页)
  14. PyTorch学习笔记
  15. k8s学习-CKA考试必过宝典
  16. python获取set中某些元素_取集合中元素_Python Set集合
  17. java uri用法_javaurl类的用法
  18. 运维工程师的职责和前景 1
  19. 解决报错:soundfile.LibsndfileError: Error opening ‘.wav‘: File contains data in an unknown format.
  20. python 不等于None 不等于空_干货 | 健身前后的黄金饮食法则,不懂等于白练!

热门文章

  1. go语言google pay支付验证订单
  2. 人力资源系统开发案例
  3. Typescript(一)
  4. 弘辽科技:淘宝销量数据从哪查?销量怎么提升?
  5. 历届图灵奖得主及研究领域
  6. MFC之路 串口通信篇(之三)
  7. 多个图元合并其中相邻的图元
  8. linux 安装到usb设备,如何通过 USB 设备来安装 CentOS
  9. ?nocache=+new Date().getTime();中的nocache是什么意思
  10. Linux 腾讯云服务器账户创建流程