VANILKA VANILKA -4 years ago 122
Java Question

Spring Cron verification

Could you verify that all my jobs are correctly configured ? I'm not sure for year task

//Every Hour
@Scheduled(cron = "0 0 * * * * ")
public void taskHour() {}

//Every Day
@Scheduled(cron = "0 0 0 * * *")
public void taskDay() {}

//EveryMonth
@Scheduled(cron = "0 0 0 1 * *")
public void taskMonth() {}

//Every Year
@Scheduled(cron = "0 0 0 1 1 *")
public void taskYear() {}

Answer Source

The pattern is a list of six single space-separated fields: representing second, minute, hour, day, month, weekday. Month and weekday names can be given as the first three letters of the English names.

Example patterns:

"0 0 * * * *" = the top of every hour of every day.
"*/10 * * * * *" = every ten seconds.
"0 0 8-10 * * *" = 8, 9 and 10 o'clock of every day.
"0 0 6,19 * * *" = 6:00 AM and 7:00 PM every day.
"0 0/30 8-10 * * *" = 8:00, 8:30, 9:00, 9:30, 10:00 and 10:30 every day.
"0 0 9-17 * * MON-FRI" = on the hour nine-to-five weekdays
"0 0 0 25 12 ?" = every Christmas Day at midnight

Quote from the spring documentation

So analyzing your code

//Every Hour
@Scheduled(cron = "0 0 * * * * ") 
public void taskHour() {}  

the top of every hour of every day. - OK

//Every Day
@Scheduled(cron = "0 0 0 * * *") 
public void taskDay() {}

the top of every day. - OK

//EveryMonth
@Scheduled(cron = "0 0 0 1 * *")
public void taskMonth() {}

It is scheduled for the first day of the month. - OK

//Every Year
@Scheduled(cron = "0 0 0 1 1 *")
public void taskYear() {}

It is scheduled for the first day of the january so it will be done once a year. - OK

So your code satisfies your requirements.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download