简体   繁体   中英

Quartz cron expression of 0/0

As a part of a input validation I was thinking whether this is a really valid cron expression and how it would executed:

0 0/0 * * * ?

Quartz validation returns true

org.quartz.CronExpression.isValidExpression("0 0/0 * * * ?")

So, does this run all the time, never, every hour or every minute...?

You can find the result by using TriggerUtils.computeFireTimesBetween() :

try {
        CronTriggerImpl cron = new CronTriggerImpl();

        cron.setStartTime(new Date());
        cron.setCronExpression("0 0/0 * * * ?");

        BaseCalendar calendar = new BaseCalendar();
        List<Date> result = TriggerUtils.computeFireTimesBetween(cron, calendar, new Date(),DateBuilder.futureDate(1, IntervalUnit.DAY));

        for (Date date : result) {
            System.out.println(date);
        }

    } catch (ParseException e) {
        e.printStackTrace();
    }

and the output is:

Thu Apr 05 18:00:00 CST 2012
Thu Apr 05 19:00:00 CST 2012
Thu Apr 05 20:00:00 CST 2012
Thu Apr 05 21:00:00 CST 2012
Thu Apr 05 22:00:00 CST 2012
Thu Apr 05 23:00:00 CST 2012
Fri Apr 06 00:00:00 CST 2012
Fri Apr 06 01:00:00 CST 2012
Fri Apr 06 02:00:00 CST 2012
Fri Apr 06 03:00:00 CST 2012
Fri Apr 06 04:00:00 CST 2012
.......................

So, 0 0/0 * * *? will run at every hour at 0 mins and 0 sec.

According to the CronTrigger Tutorial documentation:

  • / - used to specify increments. For example, "0/15" in the seconds field means " the seconds 0, 15, 30, and 45 ". And "5/15" in the seconds field means " the seconds 5, 20, 35, and 50 ". You can also specify '/' after the '' character - in this case '' is equivalent to having '0' before the '/'. '1/3' in the day-of-month field means "fire every 3 days starting on the first day of the month".

This means 0 doesn't really make sense in this context. However CronExpression seems to ignore it and simply discard that value, effectively treating this expressions as:

0 0 * * * ?

If you are curious, this is the code that discards invalid 0 value:

    if ((incr == 0 || incr == -1) && val != ALL_SPEC_INT) {
        if (val != -1) {
            set.add(Integer.valueOf(val));
        } else {
            set.add(NO_SPEC);
        }

        return;
    }

More information at CronTrigger documentation. My guess is that the expression 0 0/0 * * *? means once every hour (ie 0/0 does not denote every minute). However, if you need all the time (ie every second), you can use * * * *? * * * * *? * .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM