Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cron expression for Spring scheduler - run only once a year

My Spring service loooks like

@Scheduled( cron="0 0  7 * * SUN")
public void doSomething() {
    // do something
}

I understand you can't have the 7th value which is reserved for specifying a year. using a expression can I tell spring to just run once a year at a particular time say 25th Dec 6 am in 2020 ?

Thanks

like image 385
java1977 Avatar asked Dec 08 '22 23:12

java1977


2 Answers

Yes, you can. Just have a look at this answer. In short, you can use the format:

0 0 6 6 9 ? 
| | | | | | 
| | | | | |
| | | | | +----- any day of the week.
| | | | +------- 9th month (September).
| | | +--------- 6th day of the month.
| | +----------- 6th hour of the day.
| +------------- Top of the hour (minutes = 0).
+--------------- Top of the minute (seconds = 0).
like image 127
Erik Pragt Avatar answered Jan 13 '23 12:01

Erik Pragt


you can pass month basis it will run only once in year

@Schedule(cron="0 0 0 25 12 ?") --- it will run 25th December every year 

public void CronExpression(){

//your logic

}
like image 23
Anant T Avatar answered Jan 13 '23 12:01

Anant T