springboot using SchedulingConfigurer can set the timer next execution time.
As for the dynamic modification of runtime, then there are the most commonly used in three ways: 1, Interface 2, 3 database, configuration center hot update
We are here to get hold of the interface examples.
package com.example.demo.javaConfig; import org.springframework.context.annotation.Configuration; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import java.text.SimpleDateFormat; import java.util.Date; @RestController @Configuration public class ScheduleDynamicTest implements SchedulingConfigurer { private String cron = "0 0/1 * * * ?"; @RequestMapping("changeCron") public String changeCron(){ cron = "0 0/3 * * * ?"; return "ok" ; } @Override public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { // 构建一个线程来执行job Runnable job = new Runnable() { @Override public void RUN () { the SimpleDateFormat SDF = new new the SimpleDateFormat ( "the MM-dd-YYYY HH: mm: SS" ); String nowStr = sdf.format ( new new a Date ()); System.out.println (nowStr); } }; // build a trigger, and set the next execution time rewriting trigger the Trigger trigger = new new the Trigger () { @Override public a Date nextExecutionTime (triggerContext triggerContext) { a CronTrigger CT = new new a CronTrigger (the cron); return ct.nextExecutionTime(triggerContext); } }; scheduledTaskRegistrar.addTriggerTask(job,trigger); } }