标签:dynamic return map 并且 ext 示例 strong javac date
springboot中,使用SchedulingConfigurer能设置定时器下次的执行时间 。
至于运行时动态修改,那么目前最常用的有3种方式 : 1、 接口 2、数据库 3、配置中心热更新
这里给大家弄个接口示例 。
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() { SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); String nowStr = sdf.format(new Date()); System.out.println(nowStr); } }; // 构建一个trigger ,并且设置重写trigger的下一次执行时间 Trigger trigger = new Trigger(){ @Override public Date nextExecutionTime(TriggerContext triggerContext) { CronTrigger ct = new CronTrigger(cron); return ct.nextExecutionTime(triggerContext); } }; scheduledTaskRegistrar.addTriggerTask(job,trigger); } }
标签:dynamic return map 并且 ext 示例 strong javac date
原文地址:https://www.cnblogs.com/andsoso/p/11910668.html