1

I need to execute scheduled tasks with customizable system dates, for example, I should be able to set the scheduler date to 2019-01-15, in this scenario, the scheduler must execute all tasks scheduled on that date, understanding that the current date is 2019-02-12. At the moment, I've seen solutions only for time zone, using Spring or Quartz. Thanks!.

1 Answers1

0

Use the TaskScheduler module:

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

For more information, see: Spring scheduling task - run only once

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65