简体   繁体   中英

Re-scheduling a task that executes once using Spring Trigger

I have a requirement where I need to schedule a task (from UI) that will execute only once. After completion, I should be able to re-schedule (from UI) the same task again.

I know @Schedule won't work here as I need to execute only once. So after further searching I am able to schedule the task to execute only once at specific time using TaskScheduler with Runnable and Date and also along with @Async. However I am unable to make it reschedule.

Looks like using quartz might be possible, but I haven't gone through it yet.

Is it possible to implement my requirement with Spring Trigger. I can see only two implementation of trigger interface CronTrigger and PeriodicTrigger.

Please suggest any possible approaches. Including initial piece of code would be helpful.

The easiest way I see would be to create a regularly scheduled "trigger" method in a Spring bean that checks a certain condition and only executes the "real" action when the condition is met (eg the time you entered in the UI is in the past and the job has not started yet):

@Scheduled(fixedDelay = 5000)
public void trigger() {
  if(condition){
    //... do the action
  }
}

This requires some persistence to store the "job metadata" like the execution date and the current state of the job, but that seems "lighter" than working with threads or including quartz just for this one use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM