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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
| public class CronUtils {
private static final List<Integer> WEEKS = Arrays.stream(WeekEnum.values()).map(WeekEnum::getCode).collect(Collectors.toList());
public static String createCron(CronDto cronDto) { Integer cycleType = cronDto.getCycleType(); LocalDateTime executionTime = cronDto.getExecutionTime(); CronBuilder cronBuilder = CronBuilder.cron(CronDefinitionBuilder.instanceDefinitionFor(CronType.QUARTZ)); if (Objects.equals(CycleTypeEnum.MINUTE.getCode(), cycleType)) { return getSecondCron(cronBuilder, executionTime); } if (Objects.equals(CycleTypeEnum.HOUR.getCode(), cycleType)) { return getMinuteCron(cronBuilder, executionTime); } if (Objects.equals(CycleTypeEnum.DAY.getCode(), cycleType)) { return getDayCron(cronBuilder, executionTime); } if (Objects.equals(CycleTypeEnum.WEEK.getCode(), cycleType)) { return getWeekCron(cronDto, cronBuilder, executionTime); } if (Objects.equals(CycleTypeEnum.MONTH.getCode(), cycleType)) { return getMonthCron(cronDto, cronBuilder, executionTime); } if (Objects.equals(CycleTypeEnum.QUARTER.getCode(), cycleType)) { return getQuarterCron(cronDto, cronBuilder, executionTime); } if (Objects.equals(CycleTypeEnum.YEAR.getCode(), cycleType)) { return getYearCron(cronBuilder, executionTime); } return ""; }
public static String getYearCron(CronBuilder cronBuilder, LocalDateTime executionTime) { return cronBuilder.withSecond(on(executionTime.getSecond())) .withMinute(on(executionTime.getMinute())) .withHour(on(executionTime.getHour())) .withDoM(on(executionTime.getDayOfMonth())) .withMonth(on(executionTime.getMonthValue())) .withDoW(questionMark()) .instance() .asString(); }
public static String getQuarterCron(CronDto cronDto, CronBuilder cronBuilder, LocalDateTime executionTime) { List<FieldExpression> flist = new ArrayList<>(); cronDto.getQuartzMonths().forEach(e -> flist.add(FieldExpressionFactory.on(e))); return cronBuilder.withSecond(on(executionTime.getSecond())) .withMinute(on(executionTime.getMinute())) .withHour(on(executionTime.getHour())) .withDoM(questionMark()) .withMonth(and(flist)) .withDoW(on(WEEKS.get(cronDto.getDayOfWeek()), SpecialChar.HASH, cronDto.getWeek())) .instance() .asString(); }
public static String getMonthCron(CronDto cronDto, CronBuilder cronBuilder, LocalDateTime executionTime) { Integer repeatRule = cronDto.getRepeatRule(); if (Objects.equals(RepeatRuleEnum.WEEK.getCode(), repeatRule)) { List<FieldExpression> weekDays = new ArrayList<>(); if (!CollectionUtils.isEmpty(cronDto.getWeekDays())) { cronDto.getWeekDays().forEach(e -> weekDays.add(FieldExpressionFactory.on(WEEKS.get(cronDto.getDayOfWeek()), SpecialChar.HASH, e))); } return cronBuilder.withSecond(on(executionTime.getSecond())) .withMinute(on(executionTime.getMinute())) .withHour(on(executionTime.getHour())) .withDoM(questionMark()) .withMonth(always()) .withDoW(CollectionUtils.isEmpty(weekDays) ? on(WEEKS.get(cronDto.getDayOfWeek()), SpecialChar.HASH, cronDto.getWeek()) : and(weekDays)) .instance() .asString();
} if (Objects.equals(RepeatRuleEnum.DATE.getCode(), repeatRule)) { List<FieldExpression> monthDays = new ArrayList<>(); cronDto.getMonthDays().forEach(e -> monthDays.add(FieldExpressionFactory.on(e))); return cronBuilder.withSecond(on(executionTime.getSecond())) .withMinute(on(executionTime.getMinute())) .withHour(on(executionTime.getHour())) .withDoM(and(monthDays)) .withMonth(always()) .withDoW(questionMark()) .instance() .asString(); } return ""; }
public static String getWeekCron(CronDto cronDto, CronBuilder cronBuilder, LocalDateTime executionTime) { List<FieldExpression> weekDays = new ArrayList<>(); cronDto.getWeekDays().forEach(e -> weekDays.add(FieldExpressionFactory.on(e))); return cronBuilder.withSecond(on(executionTime.getSecond())) .withMinute(on(executionTime.getMinute())) .withHour(on(executionTime.getHour())) .withDoM(questionMark()) .withMonth(always()) .withDoW(and(weekDays)) .instance() .asString(); }
public static String getDayCron(CronBuilder cronBuilder, LocalDateTime executionTime) { return cronBuilder.withSecond(on(executionTime.getSecond())) .withMinute(on(executionTime.getMinute())) .withHour(on(executionTime.getHour())) .withDoM(always()) .withMonth(always()) .withDoW(questionMark()) .instance() .asString(); }
public static String getMinuteCron(CronBuilder cronBuilder, LocalDateTime executionTime) { return cronBuilder.withSecond(on(executionTime.getSecond())) .withMinute(on(executionTime.getMinute())) .withHour(always()) .withDoM(always()) .withMonth(always()) .withDoW(questionMark()) .instance() .asString(); }
public static String getSecondCron(CronBuilder cronBuilder, LocalDateTime executionTime) { return cronBuilder.withSecond(on(executionTime.getSecond())) .withMinute(always()) .withHour(always()) .withDoM(always()) .withMonth(always()) .withDoW(questionMark()) .instance() .asString(); } }
|