When adding timers in Spring, trigger time may be dynamically processed according to business needs;
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.scheduling.Trigger; import org.springframework.scheduling.TriggerContext; import org.springframework.scheduling.annotation.EnableScheduling; import org.springframework.scheduling.annotation.SchedulingConfigurer; import org.springframework.scheduling.config.ScheduledTaskRegistrar; import org.springframework.scheduling.support.CronTrigger; import org.springframework.stereotype.Component; import java.util.Date; @Component @EnableScheduling public class SchedulerTest implements SchedulingConfigurer { private final static Logger logger = LoggerFactory.getLogger(SchedulerTest.class); public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) { // Register the timing task to the Scheduling interface scheduledTaskRegistrar.addTriggerTask(new Runnable() { public void run() { logger.info("task job is running..."); } }, new Trigger() { public Date nextExecutionTime(TriggerContext triggerContext) { CronTrigger cronTrigger = new CronTrigger("Time Expression"); Date nextExec = cronTrigger.nextExecutionTime(triggerContext); return nextExec; } }); } }PS: When adding multiple timing tasks, just register them in Spring.
Summarize
The above is the example code for Spring dynamic configuration timer trigger time introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!