我已经使用Spring Framework的@Scheduled注释创建了一个简单的计划任务。
@Scheduled
@Scheduled(fixedRate = 2000) public void doSomething() {}
现在,当不再需要时,我想停止此任务。
我知道在此方法开始时可能有一种方法可以检查一个条件标志,但这不会停止执行此方法。
Spring提供了什么来停止@Scheduled任务?
Option 1: Using a post processor
为应停止调度的bean 提供ScheduledAnnotationBeanPostProcessor并显式调用postProcessBeforeDestruction(Object bean, String beanName)。
ScheduledAnnotationBeanPostProcessor
postProcessBeforeDestruction(Object bean, String beanName)
Option 2: Maintaining a map of target beans to its Future
private final Map<Object, ScheduledFuture<?>> scheduledTasks = new IdentityHashMap<>(); @Scheduled(fixedRate = 2000) public void fixedRateJob() { System.out.println("Something to be done every 2 secs"); } @Bean public TaskScheduler poolScheduler() { return new CustomTaskScheduler(); } class CustomTaskScheduler extends ThreadPoolTaskScheduler { @Override public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, long period) { ScheduledFuture<?> future = super.scheduleAtFixedRate(task, period); ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task; scheduledTasks.put(runnable.getTarget(), future); return future; } @Override public ScheduledFuture<?> scheduleAtFixedRate(Runnable task, Date startTime, long period) { ScheduledFuture<?> future = super.scheduleAtFixedRate(task, startTime, period); ScheduledMethodRunnable runnable = (ScheduledMethodRunnable) task; scheduledTasks.put(runnable.getTarget(), future); return future; } }
当必须停止Bean的调度时,可以查找该映射以获取Future与之对应的映射并显式取消它。