在springboot中可以通过@scheduled 注解来定义一个定时任务, 但是有时候你可能会发现有的定时任务到时间了却没有执行,但是又不是每次都不执行,这是怎么回事?
下面这段代码定义了一个每隔十秒钟执行一次的定时任务:
@componentpublic class scheduledtaskdemo { private static final logger logger = loggerfactory.getlogger(scheduledtaskdemo.class); @scheduled(cron = "0/10 * * * * *") public void execute() { logger.info("scheduled task is running... ..."); }}
此时启动springboot应用, 可以在控制台看到这个定时任务每隔10秒钟打印一条log
2017-12-21 22:20:20.832 info 7424 — [ main] s.b.c.e.t.tomcatembeddedrvletcontainer : tomcat started on port(s): 8000 (http) 2017-12-21 22:20:20.859 info 7424 — [ main] com.example.demo.demoapplication : started demoapplication in 12.134 conds (jvm running for 14.156) 2017-12-21 2协议存款2:20:30.002 info 7424 — [pool-1-thread-1] c.e.demo.scheduled.scheduledtaskdemo : scheduled task is running… … 2017-12-21 22:20:40.001 info 7424 — [pool-1-thread-1] c.e.demo.scheduled.scheduledtaskdemo : scheduled task is running… … 2017-12-21 22:20:50.002 info 7424 — [pool-1-thread-1] c.e.demo.scheduled.scheduledtaskdemo : scheduled task is running… …
但是, 一切还没结束.
如果没有相关log显示, 检查是否在入口类或者configuration类上添加了@enablescheduling注解
在上面的相关代码中, 我们使用cron表达式来指定定时任务的执行时间点, 即从0秒开始, 每隔10秒钟执行一次, 现在我们再加一个定时任务:
@componentpublic class condscheduledtaskdemo { private static final logger logger = loggerfactory.getlogger(scheduledtaskdemo.class); @scheduled(cron = "0/10 * * * * *") public void cond() { logger.info("cond scheduled task is starting... ..."); logger.info("cond scheduled task is ending... ..."); }}
现在再启动springboot应用, 再看log:
2017-12-21 23:12:20.007 info 13208 — [pool-1-thread-1] c.e.demo.scheduled.scheduledtaskdemo : first scheduled task is starting… … 2017-12-21 23:12:35.013 info 13208 — [pool-1-thread-1] c.e.demo.scheduled.scheduledtaskdemo 童装店面设计: first scheduled task is ending… … 2017-12-21 23:12:35.016 info 13208 — [pool-1-thread-1] c.e.demo.scheduled.condscheduledtaskdemo : cond scheduled task is starting… … 2017-12-21 23:12:35.016 info 13208 — [pool-1-thread-1] 最美的诗句c.e.demo.scheduled.condscheduledtaskdemo : cond scheduled task is ending… … 2017-12-21 23:12:40.000 info 13208 — [pool-1-thread-1] c.e.demo.scheduled.scheduledtaskdemo : first scheduled task is starting… … 2017-12-21 23:12:55.001 info 13208 — [pool-1-thread-1] c.e.demo.scheduled.scheduledtaskdemo : first scheduled task is ending… … 2017-12-21 23:12:55.002 info 13208 — [pool-1-thread-1] c.e.demo.scheduled.condscheduledtaskdemo : cond scheduled task is starting… … 2017-12-21 23:12:55.002 info 13208 — [pool-1-thread-1] c.e.demo.scheduled.condscheduledtaskdemo : cond scheduled task is ending… …
注意log中定时任务执行的时间点, 第二个定时任务原本应该每隔10秒钟执行一次, 但是从23:12:20到23:13:55, 本该执行4次, 确只执行了对领导干部的评价2次.
难道是cron表达式不对?
no.
为了找到原因, 我们从@scheduled注解的源码开始找:
* * <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. *
划重点, 每一个有@scheduled注解的方法都会被注册为一个
scheduledannotationbeanpostprocessor, 再接着往下看scheduledannotationbeanpostprocessor:
/** * t the {@link org.springframework.scheduling.taskscheduler} that will invoke * the scheduled methods, or a {@link java.util.concurrent.scheduledexecutorrvice} * to be wrapped as a taskscheduler. * <p>if not specified, default scheduler resolution will apply: arching for a * unique {@link taskscheduler} bean in the context, or for a {@link taskscheduler} * bean named "taskscheduler" otherwi; the same lookup will also be performed for * a {@link scheduledexecutorrvice} bean. if neither of the two is resolvable, * a local single-threaded default scheduler will be created within the registrar. * @e #default_task_scheduler_bean_name */public void tscheduler(object scheduler) {this.scheduler = scheduler;}
重点来了, 注意这句话:
if neither of the two is resolvable, a local single-threaded default scheduler will be created within the registrar.
这句话意味着, 如果我们不主动配置我们需要的taskscheduler, springboot会默认使用一个单线程的scheduler来处理我们用@scheduled注解实现的定时任务, 到此我们刚才的问题就可以理解了:
23:12:20, 第一个定时任务在线程pool-1-thread-1开始执行, 由于我们没有配置scheduler, 目前这个线程池pool-1里只有一个线程, 在打印了starting日志之后, 这个线程开始sleep;第二个定时任务也准备执行, 但是线程池已经没有多余线程了, 只能等待.
23:12:30, 第一个定时任务还在sleep, 第二个定时任务还在等待.
23:12:35, 第一个定时任务slee有个地方只有我们知道p结束, 打印ending日志并结束, 此时线程池空闲, 第二个定时任务从等待状态直接开始执行, 执行结束之后, 线程池空闲.
23:12:40, 线程池空闲, 第一个定时任务执行, 打印starting日志, 开始sleep.
注意, 这里也有可能执行第二个定时任务, 看cpu了.
… …
搞清楚这个流程之后, 解决这个问题就很简单了.
根据刚才注释的描述, 我们只需要提供一个满足我们需要的taskscheduler并注册到context中就可以了.
@configurationpublic class scheduledtaskconfiguration implements schedulingconfigurer { /** * callback allowing a {@link taskscheduler * taskscheduler} and specific {@link task task} * instances to be registered against the given the {@link scheduledtaskregistrar} * * @param taskregistrar the registrar to be configured. */ @override public void configuretasks(scheduledtaskregistrar taskregistrar) { final threadpooltaskscheduler taskscheduler = new threadpooltaskscheduler(); taskscheduler.tpoolsize(2); taskscheduler.initialize(); taskregistrar.ttaskscheduler(taskscheduler); }}
上面的代码提供了一个线程池大小为2的taskscheduler, 现在再启动下springboot看看效果.
2017-12-21 23:28:30.001 info 7972 — [taskscheduler-1] c.e.demo.scheduled.scheduledtaskdemo : cond scheduled task is starting… … 2017-12-21 23:28:30.001 info 7972 — [taskscheduler-2] c.e.demo.scheduled.scheduledtaskdemo : first scheduled task is starting… … 2017-12-21 23:28:30.002 info 7972 — [taskscheduler-1] c.e.demo.scheduled.scheduledtaskdemo : cond scheduled task is ending… … 2017-12-21 23:28:40.001 info 7972 — [taskscheduler-1] c.e.demo.scheduled.scheduledtaskdemo : cond scheduled task is starting… … 2017-12-21 23:28:40.001 info 7972 — [taskscheduler-1] c.e.demo.scheduled.scheduledtaskdemo : cond scheduled task is ending… … 2017-12-21 23:28:45.002 info 7972 — [taskscheduler-2] c.e.demo.scheduled.scheduledtaskdemo : first scheduled task is ending… …
可以看到, 当线程池里有两个线程的时候, 这两个定时任务各自按照预定的时间进行触发, 互不影响了.
本文发布于:2023-04-05 09:32:55,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/85d67625cdab9a29c6fb757b9d38018f.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:服务器定时任务不执行(服务器设置定时重启).doc
本文 PDF 下载地址:服务器定时任务不执行(服务器设置定时重启).pdf
留言与评论(共有 0 条评论) |