2
votes

Im trying to manage scheduled tasks using spring boot. I want to execute my job only one time at a particular date ( specified by the user ). Here is my Job :

@Component
public class JobScheduler{

    @Autowired
    JobController controller;
    // Retrieving the Date entered by the user
    controller.getDateForExecution();  // 2016/05/24 10:00 for example

    @Scheduled(???)
    public void performJob() throws Exception {
        controller.doSomething();
    }

There are multiple options for Scheduled annotation such as fixedDelay, fixedRate, initialDelay, cron ... but none of these can accept a Date. So, how can i execute my method at the specified Date dynamically ( ie depending on the Date insered ) ?

Ps : The method can be executed more than once if the user enter two or more Dates ..

1

1 Answers

1
votes

Spring has the TaskScheduler abstraction that you can use: http://docs.spring.io/spring/docs/current/spring-framework-reference/html/scheduling.html#scheduling-task-scheduler

It has a method to schedule execution of a Runnable at a certain Date:

ScheduledFuture schedule(Runnable task, Date startTime);

A little off-topic maybe: If JobController is a Spring Controller (or RestController), I would not autowire it into the JobScheduler. I would inverse it and inject the JobScheduler into the JobController.