Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update Cron expression in SpringBoot @Scheduled

I have around 10 jobs scheduled with @Scheduled and a hardcoded cron expression like this:

@Scheduled(cron = "* * 1 * * *")
public void testMethod(){
    doSomething();
}

Now i want to be able to through the database update this cron expression and reschedule the specific job in runtime.

Does anyone knows how to do this?

Thanks

like image 296
tiagocarvalho92 Avatar asked Nov 10 '17 10:11

tiagocarvalho92


1 Answers

If you want to configure the scheduling of job at runtime, I don't think you can use the annotation @Scheduled.

You can use your own scheduler instead from Spring documentation :

scheduler.schedule(task, new CronTrigger("0 15 9-17 * * MON-FRI"));

Then, if you want to change the configuration, you can cancel the scheduling and create a new one.

TaskScheduler return a ScheduledFuture that you should save somewhere and it can be cancelled with cancel(...) method.

like image 127
Mickael Avatar answered Sep 19 '22 23:09

Mickael