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!.
Asked
Active
Viewed 464 times
1
-
How do you expect to execute the tasks on 2019-01-15 if the date is in the past ? – Alan Kavanagh Feb 12 '19 at 14:52
-
@AK47 that's the problem, I need to set the scheduler system date to X, and at the same time I've could have scheduled tasks to be executed at `code`"0 0 17 ? * MON,WED,FRI" – Christian Carreño Morales Feb 12 '19 at 15:04
1 Answers
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