I have a @Scheduled
task in my application which is setup using CRON
and run every 4 hours. The problem I face is that the CRON
job does not start immediately after application startup but it starts only 4 hours after the application startup.
I tried to use a @PostConstruct
method inside the task to invoke it, but that results in an error due to an uninitialized Spring context.
Please tell me how I can make the Scheduled task run immediately on application deployment and then on every 4 hours after the deployment.
EDIT:
I would not use a @PostConstruct
since my scheduled method depends on other Beans , which are not initialized when this PostConstruct method runs for some reason.
By 0 */4 * * * you specify "At minute 0 past every 4th hour (0:00, 4:00, 8:00 etc.)", which is not at startup time and then every 4 hours as I think you want. You can specify initial delay and rate by:
@Scheduled(initialDelay=0, fixedRate=4*60*60*1000)
If you are worried about hard-coded values, you can still provide config value:
@Scheduled(initialDelay=0, fixedRateString = "${some.config.string}")