In the actual development process, you may need to perform regular tasks of system maintenance at night, but if cro expression needs to change, we are usually written in the above methods, can not be changed on the fly if you need to modify the source code will have to modify the repackaged release.
1. Create a new blank spring boot project
2, add @EnableScheduling notes on startup class, enable regular tasks.
3, the timing of the new task class
1 @Component 2 public class TestTask implements SchedulingConfigurer { 3 4 private static final String DEFAULT_CRON = "* * * * * ?"; 5 private String cron = DEFAULT_CRON; 6 7 8 @Override 9 public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { 10 Runnable task = new Runnable() { 11 @Override 12 public void run() { 13 //Scheduled task execution content 14 System.out.println ( "Hi Dynamic Task the this!" ); 15 } 16 }; . 17 the Trigger Trigger = new new the Trigger () { 18 is @Override . 19 public a Date nextExecutionTime (TriggerContext triggerContext) { 20 is // Timing task trigger, modify the execution cycle of the timing tasks 21 // analog acquisition timing of expression configuration task 22 is String thecron = "" ; 23 is the try { 24 RestTemplate RestTemplate = new new RestTemplate (); 25 thecron=restTemplate.getForObject("http://localhost:8089/config/getconfig",String.class); 26 27 }catch (Exception ex) 28 { 29 30 } 31 if (StringUtils.isEmpty(thecron)) { 32 cron = DEFAULT_CRON; 33 }else 34 { 35 setCron(thecron); 36 } 37 CronTrigger trigger = new CronTrigger(cron); 38 Date nextExecDate = trigger.nextExecutionTime(triggerContext); 39 return nextExecDate; 40 } 41 }; 42 taskRegistrar.addTriggerTask(task, trigger); 43 } 44 45 public void setCron(String cron) { 46 this.cron = cron; 47 } 48 }