Spring Boot 使用 JAVA 根據時間和定時器類型獲取自定義corn規則表達式

爲了方便書寫調度相關任務,寫了一個自定義根據前端上傳的任務類型及時間獲取自定義corn表達式的案例,僅供參考

1.定義model

public class TaskScheduleModel {

    /**
     * 所選作業類型:
     * 0  -> 每分鐘
     * 1  -> 每小時
     * 2  -> 每天
     * 3  -> 每週
     * 4  -> 每月
     * 5  -> 每年
     */
    private Integer jobType;
    //啓動時間
    private String startDate;

    public Integer getJobType() {
        return jobType;
    }

    public void setJobType(Integer jobType) {
        this.jobType = jobType;
    }

    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }
}

2.獲取表達式

/**
     * 方法摘要:構建Cron表達式
     *
     * @param taskScheduleModel
     * @return String
     */
    public static String createCronExpression(TaskScheduleModel taskScheduleModel) throws Exception{

        //拆分時間字符串 年,月,日,時,分,秒
        String[] split1 = taskScheduleModel.getStartDate().split(" |-|:");


        StringBuffer cronExp = new StringBuffer("");

        if (null == taskScheduleModel.getJobType()) {
            System.out.println("執行週期未配置");//執行週期未配置
        }

        if (null != split1[5] && null != split1[4] && null != split1[3]) {

            if(taskScheduleModel.getJobType().intValue() == 0){

                //秒
                cronExp.append(split1[5]).append(" ");

                //每分鐘
                cronExp.append("* ").append(" ");
                cronExp.append("* ");//小時
                cronExp.append("* ");//日
                cronExp.append("* ");//月
                cronExp.append("?");//周
            }else if(taskScheduleModel.getJobType().intValue() == 1){

                //秒
                cronExp.append(split1[5]).append(" ");
                //分
                cronExp.append(split1[4]).append(" ");

                //每小時
                cronExp.append("* ");//小時
                cronExp.append("* ");//日
                cronExp.append("* ");//月
                cronExp.append("?");//周
            }else if(taskScheduleModel.getJobType().intValue() == 2 || taskScheduleModel.getJobType().intValue() == 3 ||
                    taskScheduleModel.getJobType().intValue() == 4 || taskScheduleModel.getJobType().intValue() == 5){

                //秒
                cronExp.append(split1[5]).append(" ");
                //分
                cronExp.append(split1[4]).append(" ");
                //時
                cronExp.append(split1[3]).append(" ");
            }

            //按每日
            if (taskScheduleModel.getJobType().intValue() == 2) {

                cronExp.append("* ");//日
                cronExp.append("* ");//月
                cronExp.append("?");//周
            }
            //按每週
            else if (taskScheduleModel.getJobType().intValue() == 3) {

                String[] split2 = taskScheduleModel.getStartDate().split(" ");
                //獲取本週的周幾
                int dayForWeek = DataUtils.dayForWeek(split2[0]);
                //一個月中第幾天
                cronExp.append("? ");
                //月份
                cronExp.append("* ");
                //周
                cronExp.append(dayForWeek + 1);
            }
            //按每月
            else if (taskScheduleModel.getJobType().intValue() == 4) {
                //一個月中的哪幾天
                cronExp.append(split1[2]);
                //月份
                cronExp.append(" * ");
                //周
                cronExp.append("?");
            }
            //按每年
            else if (taskScheduleModel.getJobType().intValue() == 5) {

                //一個月中的哪幾天
                cronExp.append(split1[2]).append(" ");
                //月份
                cronExp.append(split1[1]).append(" ");
                //周
                cronExp.append("?");
            }

        } else {
            System.out.println("時或分或秒參數未配置");//時或分或秒參數未配置
        }
        return cronExp.toString();
    }

之前在網上一直沒有找到合適的,以上僅供參考。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章