Update Cron expression in SpringBoot @Scheduled

tiagocarvalho92 :

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

Mickael :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=459709&siteId=1