@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