public class DynamicCronSchedule implements SchedulingConfigurer{
private static Logger logger = LoggerFactory.getLogger(DynamicCronSchedule.class);
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
Integer num = 3000;
Runnable task = new Runnable() {
@Override
public void run() {
System.out.println(format+" "+body);
}
};
Trigger trigger = new Trigger() {
@Override
public Date nextExecutionTime(TriggerContext triggerContext) {
//执行于每一次任务的触发
String cron = "0/"+ RandomUtils.nextInt(1,30) +" * * * * ?";
logger.info("cron expression is [{}]",cron);
logger.info("trigger list size is [{}]",taskRegistrar.getTriggerTaskList().size());
CronTrigger cronTrigger = new CronTrigger(cron);
Date nextExecTime = cronTrigger.nextExecutionTime(triggerContext);
return nextExecTime;
}
};
taskRegistrar.addTriggerTask(task, trigger);
}
}