springboot自定义执行时间定时器

yuanxl 1年前 ⋅ 1421 阅读
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);
    }
}

全部评论: 0

    我有话说: