How to stop a scheduled task that was started using @Scheduled annotation?

后端 未结 8 1591
挽巷
挽巷 2020-11-27 03:52

I have created a simple scheduled task using Spring Framework\'s @Scheduled annotation.

 @Scheduled(fixedRate = 2000)
 public void doSomething() {}
         


        
8条回答
  •  天涯浪人
    2020-11-27 04:35

    Option 1: Using a post processor

    Supply ScheduledAnnotationBeanPostProcessor and explicitly invoke postProcessBeforeDestruction(Object bean, String beanName), for the bean whose scheduling should be stopped.


    Option 2: Maintaining a map of target beans to its Future

    private final Map> 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;
        }
    }
    

    When the scheduling for a bean has to be stopped, you can lookup the map to get the corresponding Future to it and explicitly cancel it.

提交回复
热议问题