Search code examples
springcronscheduled-tasksspring-scheduled

Can we make a @Scheduled execution on Spring, mixed with cron parameter, forcing a first execution at boot time?


I made many searches over internet about an option mentioned by Baeldung here, but I can't find any example. I would like to use something like this:

@Scheduled(cron="@reboot")
@Scheduled(cron="0 0 5 * * *")
public void somethingToDoOnRebootTime() {
  // code here, to run every day at 5a.m., AND at boot first time... 
}

But it didn't work, 'cause "@reboot" is not a valid cron expression... I tried to use this "@reboot" as a normal annotation to the method, but it didn't exists too...

Someone can help me? Is the article on Baeldung wrong?


Solution

  • Based on @M.Deinum comment... I used ApplicationListener but with ApplicationReadyEvent! So, my example becames:

    @EventListener(ApplicationReadyEvent.class)
    @Scheduled(cron="0 0 5 * * *")
    public void somethingToDoOnRebootTime() {
      // code here, to run every day at 5a.m., AND at boot first time... 
    }