SpringBoot3優(yōu)雅停止/重啟定時任務(wù)
環(huán)境:SpringBoot3.2.5
1. 簡介
在Spring Boot中,使用@Scheduled注解可以方便地創(chuàng)建定時任務(wù)。然而,隨著應(yīng)用程序的復(fù)雜性和運維需求的增加,動態(tài)管理這些定時任務(wù)成為了一個重要的問題。針對這種動態(tài)管理定時任務(wù)Spring Boot中并沒有提供相應(yīng)的實現(xiàn),所以就需要我們自己動手來實現(xiàn)定時任務(wù)的管理。
2. 執(zhí)行原理
首先,我們要搞清楚Spring Boot定時任務(wù)的執(zhí)行原理,其核心先通過ScheduledAnnotationBeanPostProcessor處理器,找到所有的Bean中使用了@Scheduled注解的方法,然后將對應(yīng)的方法包裝到Runnable中。
public class ScheduledAnnotationBeanPostProcessor {
  public Object postProcessAfterInitialization(Object bean, String beanName) {
    // 找到符合條件的方法
    Map<Method, Set<Scheduled>> annotatedMethods = MethodIntrospector.selectMethods(targetClass,
      (MethodIntrospector.MetadataLookup<Set<Scheduled>>) method -> {
        Set<Scheduled> scheduledAnnotations = AnnotatedElementUtils.getMergedRepeatableAnnotations(
            method, Scheduled.class, Schedules.class);
        return (!scheduledAnnotations.isEmpty() ? scheduledAnnotations : null);
      });
    // 處理方法,在processScheduled方法中會將任務(wù)包裝成ScheduledMethodRunnable對象
    annotatedMethods.forEach((method, scheduledAnnotations) ->
      scheduledAnnotations.forEach(scheduled -> processScheduled(scheduled, method, bean))); 
  }
}接下來,就是通過TaskScheduler來執(zhí)行定時任務(wù),該接口提供了一些列的方法:
public interface TaskScheduler {
  // 這些調(diào)用任務(wù)都返回了Future
  ScheduledFuture<?> schedule(Runnable task, Trigger trigger) ;
  ScheduledFuture<?> schedule(Runnable task, Instant startTime);
  ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Instant startTime, Duration period);
  // 還有其它方法。
}在默認(rèn)情況下,Spring Boot定時任務(wù)的執(zhí)行線程池使用的是ThreadPoolTaskSchedulerBean。內(nèi)部真正任務(wù)調(diào)用是通過ScheduledExecutorService執(zhí)行定時任務(wù)。
所以,要實現(xiàn)動態(tài)管理任務(wù),就需要記錄下每個任務(wù)信息。記錄任務(wù)信息是為了停止任務(wù)及再次啟動任務(wù),在上面的調(diào)度方法都返回了Future對象,可以通過該Future對象來終止任務(wù),可以通過再次調(diào)用schedule方法來再次啟動任務(wù)。所以,我們需要自定義TaskScheduler,在自定義的實現(xiàn)中我們就能很方便的記錄管理每個定時任務(wù)。
3. 實戰(zhàn)案例
要管理任務(wù),我們就必須為每個任務(wù)提供一個有意義的名稱。@Scheduled注解并沒有提供此功能。所以這塊功能,需要自己實現(xiàn)。
3.1 自定義@Task注解
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Task {
  /**任務(wù)名稱*/
  String value() default "" ;
}該注解用來對任務(wù)的說明。
3.2 任務(wù)信息TaskInfo
public class TaskInfo {
  private Runnable task ;
  private Instant startTime ;
  private Trigger trigger ;
  private Duration period ;
  private Duration delay ;
  private ScheduledFuture<?> future ;
}該類用來在執(zhí)行任務(wù)前記錄當(dāng)前的信息,以便可以對任務(wù)進行停止和重啟。
3.3 自定義線程池
@Component
public class PackTaskScheduler extends ThreadPoolTaskScheduler {
  
  private static final Map<String, TaskInfo> TASK = new ConcurrentHashMap<>() ;
  @Override
  public ScheduledFuture<?> schedule(Runnable task, Trigger trigger) {
    ScheduledFuture<?> schedule = super.schedule(task, trigger) ;
    if (task instanceof ScheduledMethodRunnable smr) {
      String taskName = parseTask(smr);
      TASK.put(taskName, new TaskInfo(task, null, trigger, null, null, schedule)) ;
    }
    return schedule ;
  }
  // 還有其它重寫的方法,自行實現(xiàn)
  private String parseTask(ScheduledMethodRunnable smr) {
    Method method = smr.getMethod();
    Task t = method.getAnnotation(Task.class) ;
    String taskName = method.getName() ; 
    if (t != null) {
      String value = t.value() ;
      if (StringUtils.hasLength(value)) {
        taskName = value ;
      }
    }
    return taskName ;
  }
  public void stop(String taskName) {
    TaskInfo task = TASK.get(taskName) ;
    if (task != null) {
      task.getFuture().cancel(true) ;
    }
  }
  public void start(String taskName) {
    TaskInfo task = TASK.get(taskName) ;
    if (task != null) {
      if (task.trigger != null) {
        this.schedule(task.getTask(), task.getTrigger()) ;
      }
      if (task.period != null) {
        this.scheduleAtFixedRate(task.getTask(), task.getPeriod()) ;
      }
    }
  }
}該類的核心作用就2個:1. 重寫任務(wù)調(diào)度方法,記錄任務(wù)信息2. 添加停止/重啟任務(wù)調(diào)度也可以考慮在該類中實現(xiàn)任務(wù)的持久化。
以上就完成了所有的核心操作。接下來寫2個方法進行測試。
3.4 測試
定時任務(wù)
@Scheduled(cron = "*/3 * * * * *")
@Task("測試定時任務(wù)-01")
public void scheduler() throws Exception {
  System.err.printf("當(dāng)前時間: %s, 當(dāng)前線程: %s, 是否虛擬線程: %b%n", new SimpleDateFormat("HH:mm:ss").format(new Date()), Thread.currentThread().getName(), Thread.currentThread().isVirtual()) ;
}停止/重啟接口
private final PackTaskScheduler packTaskScheduler ;
public SchedulerController(PackTaskScheduler packTaskScheduler) {
  this.packTaskScheduler = packTaskScheduler ;
}
@GetMapping("stop")
public Object stop(String taskName) {
  this.packTaskScheduler.stop(taskName) ;
  return String.format("停止任務(wù)【%s】成功", taskName) ;
}
@GetMapping("/start") 
public Object start(String taskName) {
  this.packTaskScheduler.start(taskName) ;
  return String.format("啟動任務(wù)【%s】成功", taskName) ; 
}分別調(diào)用上面2個方法可以對具體的任務(wù)進行停止及重啟。















 
 
 














 
 
 
 