3

I have a function annotated with @Scheduled(cron = "0 0 22 ? * *"), means it will be executed every day at 10:00 p.m. I want to test that function manually and so I am changing system time manually just before 10:00 p.m hoping function will be executed when clock reaches 10:00 p.m. But function didn't get executed. Why? Doesn't @Scheduled depend on system time?

Pavel Smirnov
  • 4,611
  • 3
  • 18
  • 28
OwlR
  • 409
  • 1
  • 5
  • 19
  • 1
    I assume this is Spring? Have you put @EnableScheduling in your spring configuration class? – Laksitha Ranasingha Mar 18 '19 at 13:23
  • Yes, @Scheduled is spring annotation. – OwlR Mar 18 '19 at 13:25
  • 1
    If the process is already running when you change the time, I *believe* this will not cause a re-evaluation of the cron specification. So you'll need to change the time before running the process. Or, see: https://stackoverflow.com/questions/17327956/testing-scheduled-in-spring. – Not a JD Mar 18 '19 at 13:25
  • 1
    ok, Have you put @EnableScheduling in your spring configuration class? – Laksitha Ranasingha Mar 18 '19 at 13:25
  • Is it enterprise environment with multiple users logged in at the same time ? –  Mar 18 '19 at 13:34
  • Your SpringApplication class needs @EnableScheduling annotation. – Anil Mar 18 '19 at 13:40
  • 1
    @Schedule doesn't depent on system time. By default the time for cron job is taken from jvm i.e. java.util.date. So instead of changing system time try changing the cron job time to test the system. – Jinesh Shah Mar 18 '19 at 13:46
  • Because it isn't scheduled at that exact time. Each scheduling recalculates the delay needed to launch the next one. It isn't really a time based triggered. – M. Deinum Mar 18 '19 at 13:46
  • @Scheduled(cron = "0 0 22 ? * *") allow your system to trigger the method when ever the system clock reach 10:00 pm , no matter if you change the time at middle. Only thing you might missing is @EnableScheduling. – Sudip Bolakhe Mar 18 '19 at 14:34

0 Answers0