当前位置:首页 > 通信资讯 > 正文

spring schedule 动态配置(spring @scheduled动态配置)

目录
  • @schedule注解动态配置时间间隔
  • spring 注解式Schedule配置定时任务

@schedule注解动态配置时间间隔

动态配置时间间隔是通过自己实现的任务注册到任务调度实现的,并在每次调度的时候更改下次调度时间间隔,如果任务阻塞或者挂掉了就不会再被调度了,如果设置时间过长,到下次调度就需要等待很长时间。

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 import org.springframework.beans.factory.annotation.Autowired; 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.PeriodicTrigger; import org.springframework.stereotype.Component; import java.util.Date; @Component @EnableScheduling public class DynamicScheduleTaskSecond implements SchedulingConfigurer { private static final long WEEK_MILLIS = 604800000; private static final long MIN_MILLIS = 1000; private static long period = 1000; static long l = System.currentTimeMillis(); @Autowired SetPeriod setPeriod; public static long getPeriod() { return period; } public static void setPeriod(long period) { DynamicScheduleTaskSecond.period = period; } @Override public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { taskRegistrar.addTriggerTask(new Runnable() { @Override public void run() { try { setPeriod.update(period); System.out.println("abc"); Long last = System.currentTimeMillis() - l; l = System.currentTimeMillis(); System.out.println(last); } catch (Exception e) { e.printStackTrace(); } } }, new Trigger() { @Override public Date nextExecutionTime(TriggerContext triggerContext) { if (period < MIN_MILLIS || period > WEEK_MILLIS) period = MIN_MILLIS; PeriodicTrigger periodicTrigger = new PeriodicTrigger(period); Date nextExecDate = periodicTrigger.nextExecutionTime(triggerContext); return nextExecDate; } }); } }
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 import org.springframework.stereotype.Component; @Component public class SetPeriod { private static Long maxPeriod = 1000l; public void update(Long period) { maxPeriod += 1000; setScheduleConfig(maxPeriod); } public boolean setScheduleConfig(Long period) { DynamicScheduleTaskSecond.setPeriod(period); return true; } }

上面是实现动态调度的一个简单实例,下面说一下基本原理。

动态调度功能主要是实现SchedulingConfigurer函数式接口,接口中的方法configureTasks的参数是重点。

?
1 2 3 4 5 6 7 8 9 10 11 @FunctionalInterface public interface SchedulingConfigurer { /** * Callback allowing a {@link org.springframework.scheduling.TaskScheduler * TaskScheduler} and specific {@link org.springframework.scheduling.config.Task Task} * instances to be registered against the given the {@link ScheduledTaskRegistrar}. * @param taskRegistrar the registrar to be configured. */ void configureTasks(ScheduledTaskRegistrar taskRegistrar); }

看名字 ScheduledTaskRegistrar就知道是一个调度任务注册类,调用这个类的addTriggerTask方法需要两个参数

?
1 2 3 public void addTriggerTask(Runnable task, Trigger trigger) { this.addTriggerTask(new TriggerTask(task, trigger)); }

一个是任务线程这个最后说,先说一下第二个Trigger,这是一个设置任务触发时间的接口,具体的实现有两个类,一个是CronTrigger对应的就是cron类型的时间设置,一个是PeriodicTrigger对应的就是FixDelay和FixRate两种方式的时间设置,实例中使用的是后者。

?
1 2 3 4 public interface Trigger { @Nullable Date nextExecutionTime(TriggerContext var1); }

接口方法参数是一个TriggerContext,这个参数就是任务触发的上下文,里面保存着上一次任务开始时间和结束时间和实际执行用时,自己需要实现这个nextExecutionTime方法根据上一次任务执行时间来返回一个新的Date时间,new一个新的periodicTrigger对象初始化period时间间隔为新的时间间隔用nextExecutionTime方法就可以了根据上下文时间返回一个新的任务调度时间了,但是period的时间不能太长也不能太短最好设置一个区间,这样可以避免很多粗心的错误导致的麻烦,到此完美解决动态设置任务调度时间间隔功能。

再说一下第一个线程任务中的需要做的事,执行的任务需要在其他的具体类中实现,然后在这个线程中调用,然后每次在调度任务的时候就要根据时间业务重新设置时间间隔,比如读配置后改变时间间隔,也就是调度和具体的任务形成一个环,调度执行具体的任务后,具体的任务在设置调度的时间间隔。

spring 注解式Schedule配置定时任务

?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 @Component public class ScheduledTasks { @Autowired private ActivityService activityService; // 1000即1s @Scheduled(fixedRate = 1000) public void reportCurrentTime() { System.out.println("当前时间: " + DateUtil.getNow()); log.info("打印当前时间: {}.", DateUtil.getNow()); } /*每天四点更新一次*/ @Scheduled(cron = "0 0 4 * * ? ") public void updateRankLIstToRedis() { /* redis超时设置 expire多次设置会覆盖 */ List<RankInfoDTO> list = activityService.countRankList(ACTIVITY_WEB); } }

很简单,要注意的是 设置时间间隔有两种 corn和fixedRate,一种适用于较长的时间而且能设置特定时间,一种则较短。

cron的话,百度第一个就是傻瓜式工具网页,不用怕不会写。

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/zpzkitt/article/details/99588579

如果您对该产品感兴趣,请填写办理(客服微信:xiaoxiongyidong)

为您推荐:

发表评论

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。