【问题标题】:How to stop Spring Scheduled task when certain condition is met [duplicate]满足某些条件时如何停止Spring计划任务[重复]
【发布时间】:2018-04-05 16:18:06
【问题描述】:

我有一些计划任务:

@Component
class Task {
  @Scheduled(...)
  void exec() {
    doSmth();
    if (allDone) {
      // cancel task, it wont ever run again
    }
  } 
}

这就是我想要的样子。网上有一些解决方案,但它们似乎都相当复杂,对我来说不太适用,而这似乎是相当普遍的问题。

【问题讨论】:

    标签: java spring scheduled-tasks


    【解决方案1】:
         @Component
         class Task {
            private Logger logger = Logger.getLogger(this.getClass());
    
          @Value("${task.enable}")
          private boolean enableTask;
    
          @Override
          @Transactional(readOnly=true)
          @Scheduled(cron = "${task.schedule}")
          public void execute() {
    
               //Do something
              //can use DAO or other autowired beans here
              if(enableTask){
                  Do your conditional job here...
              }
         }
         ...
         }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多