原因:
Spring @Scheduled 使用非常方便,你只需要在指定执行定时任务的方法上添加 @Scheduled 注释即可,但是在最近用 @Scheduled 使用 Cron 表达式时,出现了大问题,问题如下:
首先,时钟往过去拨,@Scheduled 的 Cron 表达式不会按预期执行了。(原因见后续源码分析)
其次,Cron 不按预期执行了,Spring 也没有提供接口进行重启 Scheduled Job。
最后,当系统庞大后,你无法追踪每一个 @Scheduled,当出现 Scheduled Job 失效后,你只能重启 JVM。
造成 Scheduled Job 失效的原因,贴源码 org.springframework.scheduling.concurrent.ReschedulingRunnable:
@Nullable
public ScheduledFuture<?> schedule() {
synchronized (this.triggerContextMonitor) {
this.scheduledExecutionTime = this.trigger.nextExecutionTime(this.triggerContext);
if (this.scheduledExecutionTime == null) {
return null;
}
long initialDelay = this.scheduledExecutionTime.getTime() - System.currentTimeMillis();
this.currentFuture = this.executor.schedule(this,