Update Cron expression in SpringBoot @Scheduled

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.


I think that @Scheduled no support this feature (must be interesting implement that). For advance scheduling feature you need to use quartz or other scheduler solution. My answer is based on Quartz Solution:

@Component
class ReschedulerComponent{

    @Autowired
    private SchedulerFactoryBean schedulerFactoryBean;

    public void reSchedule(){

    Trigger oldTriger = schedulerFactoryBean.getScheduler().getTrigger("my_custom_trigger");

        Trigger myNewTrigger = TriggerBuilder
                .newTrigger()
                .forJob(jobDetail) // Name of your job
                .withIdentity("my_custom_trigger")
                .startAt(myNewDATE)
                .withSchedule(SimpleScheduleBuilder.simpleSchedule().withMisfireHandlingInstructionFireNow())
                .build();

    schedulerFactoryBean.getScheduler().rescheduleJob(oldTriger.getKey(), myNewTrigger);
    }

}

Quick introduction: https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/scheduling.html