
1.简介在详解Spring Boot定时任务的几种实现方案一文中我们详细探讨、总结了平时业务系统开发过程中如何实现定时任务处理逻辑功能不清楚的可跳转文章先入门了解一下。书接上回我们今天来讲讲实战中高频使用定时任务出现的一些问题并给出解决方案最后介绍下实现原理。2.定时任务日志链路追踪我们应该都知道定时任务一大常见使用场景就是在每天晚上凌晨跑定时任务做数据处理这样可以在系统流量少、负载低的时候去做一些复杂的逻辑处理但是一旦定时任务执行失败如果没做好任务执行过程的日志链路查找问题起来也是相当困难的之前就碰到过这么一个问题客户反馈数据不对然后测试介入排查发现对应的定时任务执行了(看到了任务的info输出日志)但是没看到有error日志这就很怪了造成了一种这个代码执行了但是数据不对的现状然后开发又去翻看代码一行一行去解读一下发现没问题呀。一通操作下来之后百思不得其解最终还是觉得有报错了把当天的error日志全部输出出来看看果然发现报错了。。。我还原一下场景Component Slf4j public class ScheduledTask2 { // 使用 cron 表达式, 每10秒执行一次 Async(asyncExecutor) Scheduled(cron 0/10 * * * * ?) public void taskWithCron() { log.info(task2开始执行); int i 1/0; log.info(task2结束执行); } }项目启动之后执行报错如下[common-demo] [] [2025-01-10 14:21:00.005] [INFO] [asyncExecutor-719168] com.shepherd.basedemo.schedule.ScheduledTask2 taskWithCron: task2开始执行 [common-demo] [] [2025-01-10 14:21:00.008] [ERROR] [asyncExecutor-719168] org.springframework.aop.interceptor.SimpleAsyncUncaughtExceptionHandler handleUncaughtException: Unexpected exception occurred invoking async method: public void com.shepherd.basedemo.schedule.ScheduledTask2.taskWithCron() java.lang.ArithmeticException: / by zero很明显这个日志的上下文关联性太低了所以这里我们引入的解决方案是对任务执行进行日志链路追踪简单来说就是加上traceId来解决对日志链路追踪不熟悉的请看看之前我们总结的Spring Boot项目如何实现分布式日志链路追踪。这篇文章主要讲述了一次接口请求怎么设置一个唯一的traceId来链路追踪请求过程出现异常会在Spring MVC层面的使用全局异常捕获机制ControllerAdvice或RestControllerAdvice捕获到异常进行统一输出但是Spring 的Scheduled方法不经过控制器因此ControllerAdvice默认无法捕获Scheduled异常。所以需要通过自定义 AOP切面 或其他方式来设置traceId和实现全局捕获Slf4j Aspect Component public class ScheduledTaskAspect { Pointcut(annotation(org.springframework.scheduling.annotation.Scheduled)) public void trace() {} Around(value trace()) public void doAround(ProceedingJoinPoint joinPoint) throws Throwable { try { String traceId UUID.randomUUID().toString().replace(-, ); MDC.put(traceId, traceId); joinPoint.proceed(); } catch (Exception e) { log.error(task error: , e); } finally { MDC.remove(traceId); } } }再次执行日志输出如下[common-demo] [18d9f9fc64d1423ba4f8d83dd0946bdb] [2025-01-10 14:41:30.006] [INFO] [asyncExecutor-425480] com.shepherd.basedemo.schedule.ScheduledTask2 taskWithCron: task2开始执行 [common-demo] [18d9f9fc64d1423ba4f8d83dd0946bdb] [2025-01-10 14:41:30.008] [ERROR] [asyncExecutor-425480] com.shepherd.basedemo.interceptor.ScheduledTaskAspect doAround: task error: java.lang.ArithmeticException: / by zero是不是就看到成功插入traceId了。这样就不需要再惧怕查找任务执行的上下文日志了。当然还有一种最简单方法直接在Scheduled方法中使用try-catchComponent Slf4j public class ScheduledTask2 { // 使用 cron 表达式, 每10秒执行一次 Async(asyncExecutor) Scheduled(cron 0/10 * * * * ?) public void taskWithCron() { try { log.info(task2开始执行); int i 1/0; log.info(task2结束执行); } catch (Exception e) { log.error(task2 fail: , e); } } }3.定时任务的一些细节思考3.1 串行执行阻塞从详解Spring Boot定时任务的几种实现方案一文中我们知道Spring task默认是单线程串行执行的这就可能造成由于某个任务处理逻辑过于复杂导致执行过慢甚至出现死循环一直执行不结束这些现象都是可能出现的从而导致后续任务得不到执行或者执行时间离触发时间已经很远了甚至到了白天系统流量高高负载的时候才执行最后导致系统性能问题这就有点得不偿失了明明我是定时凌晨执行的结果白天才执行这不是搞笑吗要解决这个问题其实很简单对于核心任务处理建议开启多线程执行这样任务之间就不会互相影响了。假如你就是想单线程串行执行那可以在执行每个任务前先判断下当前时间是不是白天比如说7点了是的话就不执行了免得造成性能问题影响到用户正常使用系统但是这种方式前提是这个任务不执行不会造成数据上的错误功能上的问题等着下次再执行也可以的那种任务。关于多线程执行定时任务的实现方案可以看看前文的总结但是我这里还是要再次强调下Async开启多线程的注意点Async的默认线程池为SimpleAsyncTaskExecutor不是真的线程池这个类不重用线程默认每次调用都会创建一个新的线程。不自定义一个线程池的话可能出现资源耗尽问题。Async实现方式是通过AOP代理实现的和Transactional套路差不多(不是同一个后置处理器实现的哈)这就意味Async失效的场景也挺多的比如说同一个类方法A调用方法B方法B使用了Async是无法开启异步的。3.2 分布式定时任务一般来说实际项目中为了提高服务的响应能力我们一般会通过负载均衡的方式或者反向代理多个节点的方式来进行。通俗点来说我们一般会将项目部署多实例或者说部署多份每个实例不同的启动端口。但是每个实例的代码其实都是一样的。如果我们将定时任务写在我们的项目中就会面临一个麻烦就是比如我们部署了3个实例三个实例一启动就会把定时任务都启动那么在同一个时间点定时任务会一起执行也就是会执行3次这样很可能会导致我们的业务出现错误。一般来说我们有如下两种办法来处理配置文件中增加自定义配置通过开关来进行控制比如增加scheduleenable , scheduledisable这样在我们的实际代码中在进行判断也就是我们可以通过配置达到只有一个实例真正执行定时任务其他的是实例不执行。但是这种做法实际是还是定时任务都启动只是在执行中我们人工来进行判断执行于不执行真正的处理逻辑。引入分布式定时任务框架比如说xxl-job, quartz, power-job等等。项目推荐基于SpringBoot2.x、SpringCloud和SpringCloudAlibaba企业级系统架构底层框架封装解决业务开发时常见的非功能性需求防止重复造轮子方便业务快速开发和企业技术栈框架统一管理。引入组件化的思想实现高内聚低耦合并且高度可配置化做到可插拔。严格控制包依赖和统一版本管理做到最少化依赖。注重代码规范和注释非常适合个人学习和企业使用Github地址 https://github.com/plasticene/plasticene-boot-starter-parentGitee地址 https://gitee.com/plasticene3/plasticene-boot-starter-parent微信公众号Shepherd进阶笔记交流探讨qunShepherd_1264.Scheduled实现原理下面来到知其然知其所以然源码环节谈谈Scheduled实现原理。先从EnableScheduling开启任务调度功能Target(ElementType.TYPE) Retention(RetentionPolicy.RUNTIME) Import(SchedulingConfiguration.class) Documented public interface EnableScheduling { }该注解就是引入了核心配置类SchedulingConfiguration.classConfiguration Role(BeanDefinition.ROLE_INFRASTRUCTURE) public class SchedulingConfiguration { Bean(name TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME) Role(BeanDefinition.ROLE_INFRASTRUCTURE) public ScheduledAnnotationBeanPostProcessor scheduledAnnotationProcessor() { return new ScheduledAnnotationBeanPostProcessor(); } }这个类逻辑也很简单只干了一件事就是注入ScheduledAnnotationBeanPostProcessor后置处理器 会扫描所有的 Spring Bean寻找带有Scheduled注解的方法核心代码逻辑如下Override public Object postProcessAfterInitialization(Object bean, String beanName) { if (bean instanceof AopInfrastructureBean || bean instanceof TaskScheduler || bean instanceof ScheduledExecutorService) { // Ignore AOP infrastructure such as scoped proxies. return bean; } Class? targetClass AopProxyUtils.ultimateTargetClass(bean); if (!this.nonAnnotatedClasses.contains(targetClass) AnnotationUtils.isCandidateClass(targetClass, Arrays.asList(Scheduled.class, Schedules.class))) { MapMethod, SetScheduled annotatedMethods MethodIntrospector.selectMethods(targetClass, (MethodIntrospector.MetadataLookupSetScheduled) method - { SetScheduled scheduledMethods AnnotatedElementUtils.getMergedRepeatableAnnotations( method, Scheduled.class, Schedules.class); return (!scheduledMethods.isEmpty() ? scheduledMethods : null); }); if (annotatedMethods.isEmpty()) { this.nonAnnotatedClasses.add(targetClass); if (logger.isTraceEnabled()) { logger.trace(No Scheduled annotations found on bean class: targetClass); } } else { // Non-empty set of methods annotatedMethods.forEach((method, scheduledMethods) - scheduledMethods.forEach(scheduled - processScheduled(scheduled, method, bean))); if (logger.isTraceEnabled()) { logger.trace(annotatedMethods.size() Scheduled methods processed on bean beanName : annotatedMethods); } } } return bean; }postProcessAfterInitialization()是在bean初始化之后执行的这里就是扫描bean是否有Scheduled 如果检测到某个方法上有Scheduled注解则将其封装为Runnable并注册到TaskScheduler来看看processScheduled(scheduled, method, bean)的执行逻辑protected void processScheduled(Scheduled scheduled, Method method, Object bean) { try { Runnable runnable createRunnable(bean, method); boolean processedSchedule false; String errorMessage Exactly one of the cron, fixedDelay(String), or fixedRate(String) attributes is required; SetScheduledTask tasks new LinkedHashSet(4); // Determine initial delay long initialDelay scheduled.initialDelay(); String initialDelayString scheduled.initialDelayString(); if (StringUtils.hasText(initialDelayString)) { Assert.isTrue(initialDelay 0, Specify initialDelay or initialDelayString, not both); if (this.embeddedValueResolver ! null) { initialDelayString this.embeddedValueResolver.resolveStringValue(initialDelayString); } if (StringUtils.hasLength(initialDelayString)) { try { initialDelay parseDelayAsLong(initialDelayString); } catch (RuntimeException ex) { throw new IllegalArgumentException( Invalid initialDelayString value \ initialDelayString \ - cannot parse into long); } } } // Check cron expression String cron scheduled.cron(); if (StringUtils.hasText(cron)) { String zone scheduled.zone(); if (this.embeddedValueResolver ! null) { cron this.embeddedValueResolver.resolveStringValue(cron); zone this.embeddedValueResolver.resolveStringValue(zone); } if (StringUtils.hasLength(cron)) { Assert.isTrue(initialDelay -1, initialDelay not supported for cron triggers); processedSchedule true; if (!Scheduled.CRON_DISABLED.equals(cron)) { TimeZone timeZone; if (StringUtils.hasText(zone)) { timeZone StringUtils.parseTimeZoneString(zone); } else { timeZone TimeZone.getDefault(); } tasks.add(this.registrar.scheduleCronTask(new CronTask(runnable, new CronTrigger(cron, timeZone)))); } } } // At this point we dont need to differentiate between initial delay set or not anymore if (initialDelay 0) { initialDelay 0; } // Check fixed delay long fixedDelay scheduled.fixedDelay(); if (fixedDelay 0) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule true; tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay))); } String fixedDelayString scheduled.fixedDelayString(); if (StringUtils.hasText(fixedDelayString)) { if (this.embeddedValueResolver ! null) { fixedDelayString this.embeddedValueResolver.resolveStringValue(fixedDelayString); } if (StringUtils.hasLength(fixedDelayString)) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule true; try { fixedDelay parseDelayAsLong(fixedDelayString); } catch (RuntimeException ex) { throw new IllegalArgumentException( Invalid fixedDelayString value \ fixedDelayString \ - cannot parse into long); } tasks.add(this.registrar.scheduleFixedDelayTask(new FixedDelayTask(runnable, fixedDelay, initialDelay))); } } // Check fixed rate long fixedRate scheduled.fixedRate(); if (fixedRate 0) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule true; tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay))); } String fixedRateString scheduled.fixedRateString(); if (StringUtils.hasText(fixedRateString)) { if (this.embeddedValueResolver ! null) { fixedRateString this.embeddedValueResolver.resolveStringValue(fixedRateString); } if (StringUtils.hasLength(fixedRateString)) { Assert.isTrue(!processedSchedule, errorMessage); processedSchedule true; try { fixedRate parseDelayAsLong(fixedRateString); } catch (RuntimeException ex) { throw new IllegalArgumentException( Invalid fixedRateString value \ fixedRateString \ - cannot parse into long); } tasks.add(this.registrar.scheduleFixedRateTask(new FixedRateTask(runnable, fixedRate, initialDelay))); } } // Check whether we had any attribute set Assert.isTrue(processedSchedule, errorMessage); // Finally register the scheduled tasks synchronized (this.scheduledTasks) { SetScheduledTask regTasks this.scheduledTasks.computeIfAbsent(bean, key - new LinkedHashSet(4)); regTasks.addAll(tasks); } } catch (IllegalArgumentException ex) { throw new IllegalStateException( Encountered invalid Scheduled method method.getName() : ex.getMessage()); } }入参是Scheduled很明显就是根据注解参数如fixedRate、fixedDelay或cron构造具体的调度规则将这些任务注册到TaskScheduler中我们来看看其中的cron表达式任务this.registrar.scheduleCronTask(new CronTask(runnable, new CronTrigger(cron, timeZone)))来到ScheduledTaskRegistrar的方法#scheduleCronTask():public ScheduledTask scheduleCronTask(CronTask task) { ScheduledTask scheduledTask this.unresolvedTasks.remove(task); boolean newTask false; if (scheduledTask null) { scheduledTask new ScheduledTask(task); newTask true; } if (this.taskScheduler ! null) { scheduledTask.future this.taskScheduler.schedule(task.getRunnable(), task.getTrigger()); } else { addCronTask(task); this.unresolvedTasks.put(task, scheduledTask); } return (newTask ? scheduledTask : null); }碍于篇幅问题就不再深入下去了最终调度器根据注解参数计算任务的触发时间基于TaskScheduler和ScheduledExecutorService执行任务感兴趣的可自行debug调试研究一下。5.cron表达式cron一共有7位但是最后一位是年可以留空所以我们可以写6位* 第一位表示秒取值0-59 * 第二位表示分取值0-59 * 第三位表示小时取值0-23 * 第四位日期天/日取值1-31 * 第五位日期月份取值1-12 * 第六位星期取值1-7星期一星期二...注不是第1周第二周的意思 另外1表示星期天2表示星期一。 * 第7为年份可以留空取值1970-2099cron中还有一些特殊的符号含义如下(*)星号可以理解为每的意思每秒每分每天每月每年... (?)问号问号只能出现在日期和星期这两个位置表示这个位置的值不确定每天3点执行所以第六位星期的位置我们是不需要关注的就是不确定的值。同时日期和星期是两个相互排斥的元素通过问号来表明不指定值。比如1月10日比如是星期1如果在星期的位置是另指定星期二就前后冲突矛盾了。 (-)减号表达一个范围如在小时字段中使用“10-12”则表示从10到12点即10,11,12 (,)逗号表达一个列表值如在星期字段中使用“1,2,4”则表示星期一星期二星期四 (/)斜杠如x/yx是开始值y是步长比如在第一位秒 0/15就是从0秒开始每15秒最后就是015304560 另*/y等同于0/y下面列举几个例子供大家来验证0 0 3 * * ? 每天3点执行 0 5 3 * * ? 每天3点5分执行 0 5 3 ? * * 每天3点5分执行与上面作用相同 0 5/10 3 * * ? 每天3点的 5分15分25分35分45分55分这几个时间点执行 0 10 3 ? * 1 每周星期天3点10分 执行注1表示星期天 0 10 3 ? * 1#3 每个月的第三个星期星期天 执行#号只能出现在星期的位置在线cron表达式生成http://qqe2.com/cron/index