-1

It's my first time using the annotation @Scheduled and i need it to repeat the code evry hour from 8to20 from monday to friday, so i used one of the multiple cron generator online and this is what came out :

@Scheduled(cron = "0 0 8-20 ? * MON-FRI *")
public void deleteExpiredSlots() {
    //my code here
}

i get this error Cron expression must consist of 6 fields (found 7 in "0 0 8-20 ? * MON-FRI *")

I removed the ? and the error changed to Encountered invalid @Scheduled method 'deleteExpiredSlots': For input string: "MON" 'MON-FRI' in cron expression "0 0 8-20 * MON-FRI *"

Someone knows what's the problem and possibly how to solve it?

asdrubalo
  • 49
  • 1
  • 6

1 Answers1

0

The seven parameter CRON expression is only supported as of Spring 5.3. Prior to this, you should use this 6 parameter version:

@Scheduled(cron = "0 0 8-20 * * MON-FRI")
public void deleteExpiredSlots() {
    // your code here
}

The above will fire at the top of the hour from 8am to 8pm inclusive, on weekdays.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks but what' s the meaning of those ` * ` , between hours and days of the week ... I found online that the cron structure is something like this : ` ( seconds minutes hours days months years )` .... Is this wrong ? – asdrubalo Sep 16 '22 at 15:29
  • The two `*` in my CRON expression mean, respectively, day of month, and month. See [this SO question](https://stackoverflow.com/questions/26147044/spring-cron-expression-for-every-day-101am) for more information. – Tim Biegeleisen Sep 16 '22 at 15:33