好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

spring schedule实现动态配置执行时间

spring schedule 动态配置执行时间

之前saas平台实现动态修改定时任务的时间,都是通过xx-job这样的框架来实现,这样我们可以单独一个服务来管理我们整个saas平台的定时任务,但是最近给银行做的一个小项目,需要本地化部署,所以我不想弄很多的服务,并且他们并没有要求修改以后即时生效,所以我直接采用了 spring schedule结合mysql动态配置执行时间。

之前我们用的schedule通过注解的方式,只能用静态的corn表达式,如果想实现动态的需要实现SchedulingConfigurer,并且通过注解@EnableScheduling。如下:

?

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

56

57

58

59

package com.zqf.marketing.task; 

import com.zqf.db.marketingrobot.sys.model.RobotSysSwitch;

import com.zqf.marketing.sys.service.SwitchService;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Lazy;

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.Service;

import java.util.Date;

 

/**

  * @author zhenghao

  * @description

  * @date 2019/1/22 21:50

  */

 

@Lazy ( false )

@Service

@EnableScheduling

public class TestTaskService implements SchedulingConfigurer {

     private static Logger log = LoggerFactory.getLogger(TestTaskService. class );

     @Autowired

     private SwitchService switchService;

 

     private String SpringDynamicCronTask() {

         String cron = "0/5 * * * * ?" ;

         //从数据库获得配置的corn表达式

         RobotSysSwitch switchById = switchService.getSwitchById(5L);

         cron = switchById.getSwitchFlag();

         log.info(cron);

         return cron;

     }

 

     @Override

     public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {

         scheduledTaskRegistrar.addTriggerTask( new Runnable() {

             @Override

             public void run() {

                 // 任务逻辑

                 log.info( "task_task_tak" );

             }

         }, new Trigger() {

             @Override

             public Date nextExecutionTime(TriggerContext triggerContext) {

                 String s = SpringDynamicCronTask();

                 // 任务触发,可修改任务的执行周期

                 CronTrigger trigger = new CronTrigger(s);

                 Date nextExec = trigger.nextExecutionTime(triggerContext);

                 return nextExec;

             }

         }); 

     }

}

这样我们就可以动态的修改task的执行时间,生效时间为,上一个任务的执行周期,也可以满足我们现在需求,这样就可以实习项目更加的灵活!

@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

@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的时间不能太长也不能太短最好设置一个区间,这样可以避免很多粗心的错误导致的麻烦,到此完美解决动态设置任务调度时间间隔功能。

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

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

原文链接:https://blog.csdn.net/hao134838/article/details/86604722

查看更多关于spring schedule实现动态配置执行时间的详细内容...

  阅读:17次