0
votes

I need to run a job in spring boot for every 9 hours. I have used @Scheduled(cron = "0 */9 * * *") for running the job .But when the run the application I am getting error as "Encountered invalid @Scheduled method 'data': Cron expression must consist of 6 fields"

Please anyone help me out on this

3

3 Answers

1
votes

You can use this site to find the correct cron expression.

According this site try this cron expression:

@Scheduled(cron = "0 0 * ? * *")

Also try this as alternative:

@Scheduled(cron = "0 0 */1 * * *")
1
votes

Spring uses an extended cron syntax:

From the @Scheduled docs

A cron-like expression, extending the usual UN*X definition to include triggers on the second, minute, hour, day of month, month, and day of week. For example, "0 * * * * MON-FRI" means once per minute on weekdays (at the top of the minute - the 0th second).

The fields read from left to right are interpreted as follows.

  • second
  • minute
  • hour
  • day of month
  • month
  • day of week

The special value "-" indicates a disabled cron trigger, primarily meant for >externally specified values resolved by a ${...} placeholder.

1
votes

If you have to rely on the application start time then you shouldn' use cron but use fixedDelay instead

@Scheduled(fixedDelay = 9 * 60 * 60 * 1000) // 9 hours in milliseconds