如果我在我的控制器中运行多次'/ schedule'与我的服务器中的不同cronExpression运行一些任务计数 . 如何取消已运行的特定任务?例如,如果我使用不同的cron表达式任务安排3次,如何取消第一个或第二个ScheduledTask?我是否必须取消这个ScheduledTask?我是否需要将它存储在一些带有字典ID ScheduledTask的静态列表中?怎么可能提供?谢谢 .

我的代码片段就在这里 .

MyController.groovy

class MyController{

  private final NotificationScheduler notificationScheduler

  @Autowired
  MyController(NotificationScheduler notificationScheduler) {
    this.notificationScheduler = notificationScheduler
  }

  @RequestMapping(method = GET, value = '/schedule')
  public void Schedule(String cronExpression){

    notificationScheduler.schedule(cronExpression);
  }
  @RequestMapping(method = GET, value = '/cancel')
  public void Cancel(){
    notificationScheduler.cancel()
  }
}

NotificationScheduler.groovy

@Service
public class NotificationScheduler {

  private final TaskScheduler scheduler;

  @Autowired
  NotificationScheduler(TaskScheduler scheduler)
  {
    this.scheduler = scheduler
  }

  public void schedule(String cronExpression) throws ParseException {
    utilityNotifierScheduledFuture = scheduler.schedule(new Notifier(), new CronTrigger(cronExpression));
  }
  public void cancel(){
    utilityNotifierScheduledFuture.cancel(false)
  }
}

Notifier.groovy

class Notifier implements Runnable{

  @Override
  void run() {
    System.out.println("UtilityNotifier is being run by " + thread.getName() + " (" + thread.getId() + ")");
    System.out.println("Send notification");
  }
}