首頁 > 軟體

關於@Scheduled註解的任務為什麼不執行的問題

2022-10-02 14:01:29

概述

在SpringBoot中可以通過@Scheduled來註解定義一個定時任務,但是有時候你可能發現有的定時任務道理時間卻沒有執行,但是又不是每次都不執行,為什麼呢???

舉例說明

下面這段diam定義了一個沒隔10s執行一次的定時任務:

package com.study.practice.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @Description : schedule測試
 * @Version : V1.0.0
 * @Date : 2021/12/1 11:22
 */
@Component
@Slf4j
public class ScheduleTest {
    @Scheduled(cron = "0/10 * * * * ?")
    public void execute() {
        log.info("Scheduled task is running ... ...");
    }
}

記得在啟動類或者Configuration類上新增了@EnableScheduling註解。

啟動應用,控制檯每隔10秒列印一條紀錄檔:

2021-12-01 11:27:00.002  INFO 97876 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:27:10.001  INFO 97876 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:27:20.002  INFO 97876 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:27:30.001  INFO 97876 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:27:40.002  INFO 97876 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:27:50.001  INFO 97876 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:28:00.002  INFO 97876 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:28:10.002  INFO 97876 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...

但是,問題才剛剛開始。

在上面的相關程式碼中,我們使用cron表示式指定的定時任務執行時間點從0秒開始,每隔10s執行一次,現在我們再加一個定時任務:

package com.study.practice.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Description : schedule測試
 * @Version : V1.0.0
 * @Date : 2021/12/1 11:22
 */
@Component
@Slf4j
public class ScheduleTestTwo {
    @Scheduled(cron = "0/10 * * * * *")
    public void second() throws InterruptedException {
        log.info("Second scheduled task is running... ...");
        TimeUnit.SECONDS.sleep(5);
    }
}

啟動專案,執行結果如下:

2021-12-01 11:36:30.001  INFO 134576 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:36:30.002  INFO 134576 --- [   scheduling-1] c.s.practice.schedule.ScheduleTestTwo    : Second scheduled task is running... ...
2021-12-01 11:36:40.002  INFO 134576 --- [   scheduling-1] c.s.practice.schedule.ScheduleTestTwo    : Second scheduled task is running... ...
2021-12-01 11:36:45.002  INFO 134576 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:36:50.001  INFO 134576 --- [   scheduling-1] c.s.practice.schedule.ScheduleTestTwo    : Second scheduled task is running... ...
2021-12-01 11:36:55.002  INFO 134576 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 11:37:00.001  INFO 134576 --- [   scheduling-1] c.s.practice.schedule.ScheduleTestTwo    : Second scheduled task is running... ...
2021-12-01 11:37:05.001  INFO 134576 --- [   scheduling-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...

可以看到任務1和任務2本該都是每個10s執行,但是卻發現只有任務2執行了,任務1卻等待了5s之後才執行。

原因分析

為了找到原因,我們從@Scheduled註解的原始碼開始分析:

下面是@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:

    /**
     * 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.
     * <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
     */
    public void setScheduler(Object scheduler) {
        this.scheduler = scheduler;
    }

重點來了,注意這句話:

if neither of two is resolvable, a local single-thread default scheduler will be created within in the registrar.

這句話意味著,如果我們不主動設定我們需要的TaskScheduler,SpringBoot會預設使用一個單執行緒的scheduler來處理我們用@Scheduled註解實現的定時任務,到此我們剛才的問題就可以理解了。

因為是單個執行緒執行所有的定時任務,所有task2如果先執行,因為執行中等待了5s,所以task2執行完後,task1接著繼續執行。

解決方案

搞清楚這個流程後,解決這個問題就很簡單了。

根據剛才註釋的描述,我們只需要提供一個滿足自己需要的TaskScheduler並註冊到context容器中就可以了。

package com.study.practice.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;

/**
 * @Description : scheduler設定類
 * @Version : V1.0.0
 * @Date : 2021/12/1 14:00
 */
@Configuration
public class ScheduledTaskConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(2);
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);
    }
}

上面的程式碼提供了一個執行緒池大小為2的taskScheduler,再次啟動SpringBoot檢視效果。

2021-12-01 14:05:20.001  INFO 39768 --- [TaskScheduler-2] c.s.practice.schedule.ScheduleTestTwo    : Second scheduled task is running... ...
2021-12-01 14:05:20.001  INFO 39768 --- [TaskScheduler-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 14:05:30.002  INFO 39768 --- [TaskScheduler-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 14:05:30.002  INFO 39768 --- [TaskScheduler-2] c.s.practice.schedule.ScheduleTestTwo    : Second scheduled task is running... ...
2021-12-01 14:05:40.001  INFO 39768 --- [TaskScheduler-1] c.study.practice.schedule.ScheduleTest   : Scheduled task is running ... ...
2021-12-01 14:05:40.001  INFO 39768 --- [TaskScheduler-2] c.s.practice.schedule.ScheduleTestTwo    : Second scheduled task is running... ...

可以看到,當執行緒池裡有兩個執行緒時,這兩個任務各種按照預定的時間進行觸發,互不影響了。

以上為個人經驗,希望能給大家一個參考,也希望大家多多支援it145.com。


IT145.com E-mail:sddin#qq.com