首先看源码注释

/*** Annotation that marks a method to be scheduled. Exactly one of the* {@link #cron}, {@link #fixedDelay}, or {@link #fixedRate} attributes must be* specified.* * 将一个方法标记为定时任务的注解。必须明确地指定cron,fixedDelay或fixedRate属性* * * <p>The annotated method must expect no arguments. It will typically have* a {@code void} return type; if not, the returned value will be ignored* when called through the scheduler.** 带注解的方法不能有参数。返回值类型必须是void;否则,调度器调用时会忽略返回值。*** <p>Processing of {@code @Scheduled} annotations is performed by* registering a {@link ScheduledAnnotationBeanPostProcessor}. This can be* done manually or, more conveniently, through the {@code <task:annotation-driven/>}* element or @{@link EnableScheduling} annotation.** 通过注册到ScheduledAnnotationBeanPostProcessor后处理bean中来执行注解标注的程序。可以手动将定时任务注册到ScheduledAnnotationBeanPostProcessor后处理bean中,或者通过<task:annotation-driven/>标签和@EnableScheduling注解。*** <p>This annotation may be used as a <em>meta-annotation</em> to create custom* <em>composed annotations</em> with attribute overrides.** 看不懂(可以作为元注解创建自定义注解?)*** @author Mark Fisher* @author Juergen Hoeller* @author Dave Syer* @author Chris Beams* @since 3.0* @see EnableScheduling* @see ScheduledAnnotationBeanPostProcessor* @see Schedules*/

注释里说@Scheduled注解的方法被注册到ScheduledAnnotationBeanPostProcessor后处理Bean里面了,来看看ScheduledAnnotationBeanPostProcessor源码

public class ScheduledAnnotationBeanPostProcessorimplements ScheduledTaskHolder, MergedBeanDefinitionPostProcessor, DestructionAwareBeanPostProcessor,Ordered, EmbeddedValueResolverAware, BeanNameAware, BeanFactoryAware, ApplicationContextAware,SmartInitializingSingleton, ApplicationListener<ContextRefreshedEvent>, DisposableBean {/*** The default name of the {@link TaskScheduler} bean to pick up: {@value}.* <p>Note that the initial lookup happens by type; this is just the fallback* in case of multiple scheduler beans found in the context.* @since 4.2*/public static final String DEFAULT_TASK_SCHEDULER_BEAN_NAME = "taskScheduler";private final ScheduledTaskRegistrar registrar;@Nullableprivate Object scheduler;/*** Create a default {@code ScheduledAnnotationBeanPostProcessor}.*/public ScheduledAnnotationBeanPostProcessor() {this.registrar = new ScheduledTaskRegistrar();}/*** Create a {@code ScheduledAnnotationBeanPostProcessor} delegating to the* specified {@link ScheduledTaskRegistrar}.* @param registrar the ScheduledTaskRegistrar to register @Scheduled tasks on* @since 5.1*/public ScheduledAnnotationBeanPostProcessor(ScheduledTaskRegistrar registrar) {Assert.notNull(registrar, "ScheduledTaskRegistrar is required");this.registrar = registrar;}/*** Set the {@link org.springframework.scheduling.TaskScheduler} that will invoke* the scheduled methods, or a {@link java.util.concurrent.ScheduledExecutorService}* to be wrapped as a TaskScheduler.* * 设置被调用的方法为TaskScheduler,或将ScheduledExecutorService包装成TaskScheduler。* * * <p>If not specified, default scheduler resolution will apply: searching for a* unique {@link TaskScheduler} bean in the context, or for a {@link TaskScheduler}* bean named "taskScheduler" otherwise; the same lookup will also be performed for* a {@link ScheduledExecutorService} bean. If neither of the two is resolvable,* a local single-threaded default scheduler will be created within the registrar.* @see #DEFAULT_TASK_SCHEDULER_BEAN_NAME* * 设置被调用的方法为TaskScheduler,或将ScheduledExecutorService包装成TaskScheduler。* 如果没有指定,将使用以下两个默认方案:* 一、在容器里查找唯一的TaskScheduler实例。* 二、在容器里查找名字是taskScheduler的实例。* 如果两个方案都无法解析,将会创建一个单线程默认调度器。* * ???* */public void setScheduler(Object scheduler) {this.scheduler = scheduler;}
}

意味着, 如果不配置TaskScheduler, SpringBoot会默认使用一个单线程的scheduler来处理@Scheduled注解标注的定时任务。

发现项目中有一个定时任务延迟6秒启动,然后死循环。(@Scheduled注解各参数详解)

    /*** 延迟60秒继续执行,异步执行;*/@Scheduled(fixedDelay = 6000)public void run() {log.info("init zk register");while (true) {log.info("do something...");Thread.sleep(5000);}}

由此断定是因为线程池里唯一的线程被死循环占用了。

解决方案:

一:注册zk的线程修改为5秒执行一次的定时任务,不睡眠。

    /*** 延迟60秒继续执行,异步执行;*/@Scheduled(cron = "0/5 * * * * ?")public void run() {log.info("init zk register");log.info("do something...");}

二:主动配置TaskScheduler,指定线程池线程数量。

package com.soyoung.ztdata.dataservice.config;import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;/*** @program: zt_service* @description:* @author: Leon* @create: 2021-06-04 22:07**/
@Configuration
public class SoYongSchedulingConfiguration implements SchedulingConfigurer {@Overridepublic void configureTasks(ScheduledTaskRegistrar taskRegistrar) {ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();threadPoolTaskScheduler.setPoolSize(2);threadPoolTaskScheduler.initialize();taskRegistrar.setScheduler(threadPoolTaskScheduler);}
}

三:修改配置文件,指定定时任务线程池线程数量。

spring.task.scheduling.pool.size=2

排查定时任务为什么不执行相关推荐

  1. DataX踩坑2 | 定时任务crontab不执行或报错:/bin/sh: java: command not found

    前面两天写了一个DataX的增量同步脚本,今天检查了一下发现定时任务没有执行成功,数据并没有同步.以下为排查问题和解决方法. 一.定时任务crontab不执行 脚本(测试用的可以设为每分钟一次:*/1 ...

  2. quartz定时任务突然不执行了

    高并发情况下,quartz定时任务突然不执行了, 背景: 整个项目架构,高并发操作比较多, 有多个线程池,周期线程池,和定时任务,占用多个资源 导致现场出现定时任务走一段时间就不走的情况 当前定时任务 ...

  3. scheduled线程池ScheduledExecutorService只执行一次_有个定时任务突然不执行了

    scheduled线程池ScheduledExecutorService只执行一次_有个定时任务突然不执行了 原因 If any execution of the task encounters an ...

  4. java实现每天定时执行任务,Spring Task定时任务每天零点执行一次的操作

    最近根据项目的需求,需要限制用户每天的发送短信数量.这样以来就需要写一个定时任务,每天去置零一次所有用户的发送短信统计数量. 首先,在application.xml文件中添加 接着就是编写自己的业务处 ...

  5. 有个定时任务突然不执行了,别急,原因可能在这

    点击上方「蓝字」关注我们 问题描述 程序发版之后一个定时任务突然挂了! "幸亏是用灰度跑的,不然完蛋了.????" 之前因为在线程池踩过坑,阅读过ThreadPoolExecuto ...

  6. 定时任务每秒执行、每分钟执行、每小时执行、每天执行、每周执行、每月执行、每年执行、定时任务重复执行、循环执行

    在平时生活.系统运维.实验室.学校等场景下,有很多定期循环执行任务的需求.比如:在系统运维过程中,常常会在每天凌晨1点以后执行某些批处理脚本:在实验室做实验过程中,需要每隔10分钟去操作一下设备等等. ...

  7. 定时任务重启后执行策略_quartz定时任务框架调度机制解析

    quartz2.2.1集群调度机制调研及源码分析 引言 quartz集群架构 调度器实例化 调度过程 触发器的获取 触发trigger: Job执行过程: 总结: 附: 引言 quratz是目前最为成 ...

  8. crontab shell 每5秒执行_centos 定时任务按秒执行crontab

    测试环境需要一个脚本,定时让mysql写入一个数据,需要crontab 按秒执行 写的shell脚本 [root@kvm-ovirt shell]# cat mysql.sh #!/bin/sh ## ...

  9. ScheduledThreadPoolExecutor定时任务线程池执行原理分析

    一.示例代码 @Slf4j public class ScheduleThreadPoolTest {private static ScheduledExecutorService executor ...

最新文章

  1. react native 常用学习或查资料网址
  2. 谭浩强《C++程序设计》学习
  3. php5.4dev版本是,ubuntu 编译安装php5.4以上版本
  4. 编程语言python培训-0基础转行IT,编程语言应该学习Java还是Python呢?
  5. eclipse复制代码连接数据库404_再见,Eclipse ...
  6. linux make编译报错 mv,Linux下安装redis
  7. Try Git 译文
  8. java 重复代码优化_利用注解 + 反射消除重复代码(Java项目)
  9. 差分电荷密度 matlab,差分电荷密度
  10. L1-025. 正整数A+B-PAT团体程序设计天梯赛GPLT
  11. 转:linux下挂载移动硬盘
  12. selenium-滚动
  13. java判断是否为5的倍数,如何用编程实现“判断输入的正整数是否既是5又是7的正倍数,若是,则输出yes,否则输出no”?...
  14. YAML语法详细总结
  15. mysql面试题50
  16. 清华现超级“学霸” 15门课程100分4门99分(图)
  17. Java实现 蓝桥杯 算法提高 学霸的迷宫
  18. 键盘按键开关种类简介
  19. IDEA buid Web项目直接报错,Error:Abnormal build process termination:
  20. 2010年4月13日

热门文章

  1. 自定义组件开发六 自定义组件
  2. 二叉树经典问题-通过前序和后序序列重建二叉树
  3. 全志 android 编译,全志A20启动代码流程分析 ——Android
  4. 魔方还原算法(二) 科先巴的二阶段算法
  5. 统计学学习日记:L5-离散趋势分析之异众比率与四分位差
  6. Onedrive如何同步文件夹
  7. MuleSoft知识总结-11.Mule基本组件(Transform Message)
  8. python实现一个简单的图像处理交互界面(tkinter库)
  9. AppImage应用启动报错:Cannot mount AppImage, please check your FUSE setup
  10. JVM垃圾回收——ZGC垃圾收集器