0

I am trying to read cron expression from the property file for @Scheduled annotation.

However, when I start the server, I get error like below

Encountered invalid @Scheduled method 'scheduleServicePrincipalOrchestorJob': For input string: "'0

Not Sure what is happening here.

My annotation looks like -

@Scheduled(cron= "${abc.cronExpression}", zone="America/Los_Angeles")

My application.properties looks like

abc.cronExpression="0 8 0 0 0 0"

Any leads on what is going wrong?

Gowri G
  • 420
  • 4
  • 18
xyz
  • 1

3 Answers3

0

Remove the quotes from the cron expression. Also some of the fields cannot be 0, so start with a valid expression like this and adjust to your needs:

abc.cronExpression=0 0/5 * * * ?

That is, at second 0, every 5 minutes, every day.

Santiago Medina
  • 529
  • 2
  • 12
0

A cron expression in Spring using @Scheduled is consists of the following parts:

<second> <minute> <hour> <day-of-month> <month> <day-of-week> <year>

Where the last part <year> is optional.

In your example, you use both 0 for <day-of-month> and <month> which is not possible as they are not starting from 0 but 1.

Your cron works with the following two adjustments:

bc.cronExpression=0 8 0 * * 0

Where * means any day in the month for any month.

Or you can use the following to execute your code only at the first day of the first month:

bc.cronExpression=0 8 0 1 1 0

For more information, Bealdung put together a great guide to cron expressions that may help you understand it further.

rieckpil
  • 10,470
  • 3
  • 32
  • 56
0

Maybe you can refer to this.

The value for annotation attribute Scheduled.cron must be a constant expression

https://stackoverflow.com/a/16509088/4258006

Shiva kumar
  • 673
  • 8
  • 23