我需要执行具有可自定义系统日期的计划任务,例如,我应该能够将计划程序日期设置为2019-01-15,在这种情况下,计划程序必须执行在该日期安排的所有任务,了解当前日期是2019-02-12目前,我只看到了使用Spring或Quartz的时区解决方案。 谢谢!。
答案 0 :(得分:0)
使用TaskScheduler
模块:
private TaskScheduler scheduler;
Runnable exampleRunnable = new Runnable(){
@Override
public void run() {
System.out.println("Executing task");
}
};
@Async
public void executeTaskT() {
ScheduledExecutorService localExecutor = Executors.newSingleThreadScheduledExecutor();
scheduler = new ConcurrentTaskScheduler(localExecutor);
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
Date scheduledDate = dateFormat.parse("12-02-2019", dateFormat);
scheduler.schedule(exampleRunnable, scheduledDate);
}
executeTaskT(); //call it somewhere after the spring application has been configured
有关更多信息,请参见: Spring scheduling task - run only once