Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Quartz cron - what if the day of month does not exist?

I'm trying to write a simple cron expression for quartz scheduler. I want the job to run every month on day 30 at 3am.

0 0 3 30 JAN-DEC ? *

I wonder what happens for the month of February? Will the job run or not run?

I'm not looking for a last day of month solution, I need the user to select the day of month when the job will run (ideally once for all months).

like image 496
dcernahoschi Avatar asked Dec 19 '11 11:12

dcernahoschi


2 Answers

L ("last") - has different meaning in each of the two fields in which it is allowed. For example, the value "L" in the day-of-month field means "the last day of the month" - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means "7" or "SAT". But if used in the day-of-week field after another value, it means "the last xxx day of the month" - for example "6L" means "the last friday of the month". When using the 'L' option, it is important not to specify lists, or ranges of values, as you'll get confusing results.

You can use this to specify instead of specifying 30 in your corn job directly.

http://www.quartz-scheduler.org/documentation/quartz-1.x/tutorials/crontrigger

Check for Special characters.

Thanks.

like image 147
Ramakanth77 Avatar answered Sep 20 '22 06:09

Ramakanth77


It won't run. If you want it to run in the 28th in case of February, you have to create multiple CronExpressions for each case of days in month, and a trigger for each one, and then add all the triggers to your required Job.

This is what I have done:

CronExpressions creation:

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, "?");
}

Trigger creation:

        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++;
        }
like image 44
yishaiz Avatar answered Sep 18 '22 06:09

yishaiz