将自定义系统日期设置为Scheduler

问题描述 投票:1回答:1

我需要使用可自定义的系统日期执行计划任务,例如,我应该能够将计划程序日期设置为2019-01-15,在这种情况下,计划程序必须执行在该日期安排的所有任务,了解当前日期是2019-02-12。目前,我已经看过使用Spring或Quartz的时区解决方案。谢谢!。

java spring scheduled-tasks quartz
1个回答
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

© www.soinside.com 2019 - 2024. All rights reserved.