Nó sẽ không chạy. Nếu bạn muốn nó chạy trong 28 trong trường hợp của tháng hai, bạn phải tạo nhiều CronExpressions cho mỗi trường hợp của ngày trong tháng, và một kích hoạt cho mỗi một, và sau đó thêm tất cả các kích hoạt cho công việc yêu cầu của bạn.
Đây là những gì tôi đã làm:
CronExpressions tạo:
public static List<CronExpression> getCronExpressionList(int seconds, int minutes,
int hours, int dayInMonth, Month month,
DayOfWeek dayOfWeek) {
final String monthsWith30Days = Month.APR + "," + Month.JUN + ","
+ Month.SEP + "," + Month.NOV;
List<CronExpression> crons = new LinkedList<CronExpression>();
String timeString = String.format(("%s %s %s "), seconds, minutes,
hours, 0, 0, 0);
String dateString = "%s %s %s";
String cron = null;
cron = timeString + String.format(dateString, dayInMonth, "*", "?");
crons.add(new CronExpression(cron));
if (dayInMonth > 28) {
String febCron = timeString + getFebruarLastDayDateString(dateString);
crons.add(new CronExpression(febCron));
if (dayInMonth == 31) {
String monthsWithThirtyDaysCron = timeString + String.format(dateString,
"L", monthsWith30Days, "?");
crons.add(new CronExpression(monthsWithThirtyDaysCron));
}
}
return crons;
}
private static String getFebruarLastDayDateString(String initialCron)
throws ParseException {
return String.format(initialCron, "L", Month.FEB, "?");
}
tạo Trigger:
Set<CronTrigger> triggers = new HashSet<>();
int i = 1;
for (CronExpression cronEx : cronsList) {
CronTrigger trigger = newTrigger()
.withIdentity("trigger" + i, groupName)
.withSchedule(cronSchedule(cronEx))
.build();
triggers.add(trigger);
i++;
}
Vâng, tôi không quan tâm để chạy các công việc trên trước, hoặc thứ hai ngày cuối cùng của tháng. Tôi chỉ cần nó trên 30 (nếu ngày đó tồn tại). – dcernahoschi