11

I am very new to Java. As my first project, I am going to work with cron job scheduler. I want some clarification on scheduling. I have a code which will run every hour.

  CronTrigger ct = new CronTrigger("cronTrigger", "group2", "0 1/0 * * * ?"); 

I have read the documents about scheduling, but I got confused

In one document i have read like in the below

 ("0 0 * * * ?") 
  • 1st 0 indicates seconds
  • 2nd indicates mins
  • 3rd hour
  • 4th which day of the month
  • 5th which month.

In some document I read that 1st indicates mins 2nd - hour etc.

Can anyone please explain me this(0 1/0 * * * ?) and also what it means (1/0)?

And I want to run a job in every six hours.

If i give like this (0 */6 * * * ?) whether it will run in every six hours?

fedorqui
  • 275,237
  • 103
  • 548
  • 598
Sunny
  • 1,167
  • 5
  • 15
  • 27

3 Answers3

14

If you check in crontab.guru, both of these are almost equivalent:

*   * * * *
* 1/0 * * *

This is because X/Y means: starting from X, every Y. That is, all X + Yn. So if you say */2 it will do every 2 hours.

In this case: 1/0 means "starting from 1, every hour", so it matches from 1 to 23, whereas * matches from 0 to 23.

Following your question, */6 matches every 6 hour, so it will precisely run at hour 0, 6, 12 and 18.

Regarding your question on what is the 6th parameter ? doing, I read that:

I believe that's processed by the CronExpression class which has six constants: minute, hour, day, month, weekday, year. Cron uses minute, hour, day, month, weekday. The addition of the year for the yearly() method seems to be the reason for the extra *.

So instead of having the common syntax

 +---------------- minute (0 - 59)
 |  +------------- hour (0 - 23)
 |  |  +---------- day of month (1 - 31)
 |  |  |  +------- month (1 - 12)
 |  |  |  |  +---- day of week (0 - 6) (Sunday=0 or 7)
 |  |  |  |  |
 *  *  *  *  *  command to be executed 

With Java you have

   +----------------- minute (0 - 59)
   |  +-------------- hour (0 - 23)
   |  |  +----------- day of month (1 - 31)
   |  |  |  +-------- month (1 - 12)
   |  |  |  |  +----- day of week (0 - 6) (Sunday=0 or 7)
 # |  |  |  |  |  +-- year                       <-- this is extra !!
   |  |  |  |  |  |
   *  *  *  *  *  * command to be executed 

This last parameter can have a value as well, but in your case it specifies ?. As for what I read in crontab.guru, it means:

? blank (non-standard)

So I would schedule it normally with the 5 usual parameters and then add ? at the end so that it runs in all years.

fedorqui
  • 275,237
  • 103
  • 548
  • 598
3

A small mistake slipped into the accepted answer: the extra field in Spring six fields cron expressions is the first field for "second".

+-------------------- second (0 - 59) <- extra field in Spring cron expression
|  +----------------- minute (0 - 59)
|  |  +-------------- hour (0 - 23)
|  |  |  +----------- day of month (1 - 31)
|  |  |  |  +-------- month (1 - 12)
|  |  |  |  |  +----- day of week (0 - 6) (Sunday=0 or 7)
|  |  |  |  |  |
*  *  *  *  *  *   <command to be executed>

Beware that every 5 fields cron expression examples or calculated by cron calculators on the internet (like crontab.guru) are wrong with Spring.

The expression: 0 * * * * (5 fields) will have Spring throws an IllegalArgumentException("Cron expression must consist of 6 fields"). 5 fields cron expressions must be converted to Spring's 6 fields format by adding a leading '*' or '0'.

see also Spring cron vs normal cron?

Zartc
  • 193
  • 7
0

Just adding the doc of spring in the exact point of this doubt to make it clear. scheduling-cron-expression

Blockquote All Spring cron expressions have to conform to the same format, whether you are using them in @Scheduled annotations, task:scheduled-tasks elements, or someplace else. A well-formed cron expression, such as * * * * * *, consists of six space-separated time and date fields, each with its own range of valid values:

<pre> ┌───────────── second (0-59)
 │ ┌───────────── minute (0 - 59)
 │ │ ┌───────────── hour (0 - 23)
 │ │ │ ┌───────────── day of the month (1 - 31)
 │ │ │ │ ┌───────────── month (1 - 12) (or JAN-DEC)
 │ │ │ │ │ ┌───────────── day of the week (0 - 7)
 │ │ │ │ │ │          (0 or 7 is Sunday, or MON-SUN)
 │ │ │ │ │ │
 * * * * * *</pre>