shikeying
2024-01-19 54ea586ece304ef2569a345c9b26b2a9b9702c8a
定时任务,修改启动不执行(当过期)
1个文件已添加
2个文件已修改
105 ■■■■■ 已修改文件
walker-scheduler/src/main/java/com/walker/scheduler/Option.java 60 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
walker-scheduler/src/test/java/com/walker/scheduler/EveryDayScheduler.java 17 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
walker-scheduler/src/test/java/com/walker/scheduler/TestScheduler.java 28 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
walker-scheduler/src/main/java/com/walker/scheduler/Option.java
@@ -56,6 +56,18 @@
    private int currentTaskMonth = 0;            // 周期性,当前执行到的月,记录
    private int currentTaskYear = 0;                // 周期性,当前执行到的年,记录
    public int getCurrentTaskDay() {
        return currentTaskDay;
    }
    public int getCurrentTaskMonth() {
        return currentTaskMonth;
    }
    public int getCurrentTaskYear() {
        return currentTaskYear;
    }
    public String getTimeRangesValue() {
        StringBuilder s = new StringBuilder();
        for(Integer[] vals : timeRanges){
@@ -110,6 +122,43 @@
        this.currentTaskMonth = month;
        this.currentTaskDay = day;
//        this.currentTaskHour = hour;
        // 2024-01-18,修复:如果已经过了今天时间点的任务,如:凌晨2点,则需要自动切换到下一个时间。
        // 否则出现任务永远无法执行情况。
        if(this.isCycleTask){
            Option.TimeObject timeObj = this.isAvailableTime(System.currentTimeMillis());
            int currentHour = DateUtils.getCurrentHour();
            int currentDay = DateUtils.getCurrentYearMonthDay()[2];
            if(this.periodType == PeriodType.DAY){
                if(currentHour > hour){
                    // 今天已经过了时间点,自动切换到下一次
                    this.scheduleToNext(timeObj);
                    logger.info("今天已经过了时间点,自动切换到下一次,option={}", this);
                }
            } else if (this.periodType == PeriodType.MONTH) {
                if(currentDay > day){
                    this.scheduleToNext(timeObj);
                    logger.info("本月当前日期{}已超过设置日期{},自动切换到下一次,option={}", currentDay, day, this);
                } else if(currentDay == day && (currentHour > hour)){
                    this.scheduleToNext(timeObj);
                    logger.info("本月日期已到达,但由于时间已过期,因此任务会在下个月执行,option={}", this);
                }
            } else if (this.periodType == PeriodType.YEAR) {
                int currentMonth = DateUtils.getCurrentYearMonthDay()[1];
                if(currentMonth > month){
                    this.scheduleToNext(timeObj);
                    logger.info("本年当前月份{}已超过设置月份{},自动切换到下一次,option={}", currentMonth, month, this);
                } else if (currentMonth == month && (currentDay > day)) {
                    this.scheduleToNext(timeObj);
                    logger.info("本年当前月份相同,但当前日期{}已超过设置日期{},自动切换到下一次,option={}", currentDay, day, this);
                } else if(currentMonth == month && (currentDay == day) && currentHour > hour){
                    this.scheduleToNext(timeObj);
                    logger.info("本年当前月份日期相同,但当前时间{}已超过设置时间{},自动切换到下一次,option={}", currentHour, hour, this);
                }
            }
        }
    }
    /**
@@ -454,6 +503,17 @@
        }
    }
    @Override
    public String toString(){
        return new StringBuilder("[periodType=").append(this.periodType)
                .append(", timeType=").append(this.timeType)
                .append(", timeRanges=").append(this.timeRanges)
                .append(", isCycleTask=").append(this.isCycleTask)
                .append(", setYearMonthDayHour=").append(this.year).append("-").append(this.month).append("-").append(this.day).append("-").append(this.hour)
                .append(", nextTime=").append(this.currentTaskYear).append("-").append(this.currentTaskMonth).append("-").append(this.currentTaskDay)
                .append("]").toString();
    }
    public static void main(String[] args){
        /**
        long test = System.currentTimeMillis();
walker-scheduler/src/test/java/com/walker/scheduler/EveryDayScheduler.java
New file
@@ -0,0 +1,17 @@
package com.walker.scheduler;
import com.walker.scheduler.impl.ForeverScheduler;
public class EveryDayScheduler extends ForeverScheduler {
    public EveryDayScheduler(int id, String name) {
        super(id, name);
    }
    @Override
    protected Object onProcess(Object[] inputParams) throws Exception {
        logger.info("执行了一次,下次:" + this.getOption());
        return "ok";
    }
}
walker-scheduler/src/test/java/com/walker/scheduler/TestScheduler.java
@@ -1,11 +1,39 @@
package com.walker.scheduler;
import com.walker.infrastructure.utils.DateUtils;
import com.walker.infrastructure.utils.WaitConsoleInput;
import com.walker.scheduler.impl.ForeverScheduler;
import org.junit.Test;
public class TestScheduler {
    @Test
    public void testEveryDayOnce(){
        EveryDayScheduler scheduler = new EveryDayScheduler(1, "机构信息同步");
        Option option = new Option();
//        option.setPeriodType(Option.PeriodType.DAY);
//        option.setTimeType(Option.TimeType.EXACTLY);
//        int[] yearMonthDay = DateUtils.getCurrentYearMonthDay();
//        // 设置每天凌晨3点执行
//        int settingHour = 3;
//        option.setExactlyTime(yearMonthDay[0], yearMonthDay[1], yearMonthDay[2], settingHour);
//        scheduler.setOption(option);
//        scheduler.start();
//        System.out.println("初始启动,option=" + scheduler.getOption());
        option.setPeriodType(Option.PeriodType.MONTH);
        option.setTimeType(Option.TimeType.EXACTLY);
        int[] yearMonthDay = DateUtils.getCurrentYearMonthDay();
        // 设置每天凌晨3点执行
        int settingHour = 3;
        option.setExactlyTime(yearMonthDay[0], yearMonthDay[1], 19, settingHour);
        scheduler.setOption(option);
        scheduler.start();
        System.out.println("初始启动,option=" + scheduler.getOption());
        WaitConsoleInput.waitInput();
    }
//    @Test
    public void testRunIdSchedulers(){
        this.createIdScheduler(2, 5000).start();