Spring @Scheduled cron job in combination with Instant.now()

266 Views Asked by At

In Spring I use standard CRON job scheduling to do some business level measurements every 10 minutes with this example

@Async
@Scheduled(cron = "0 0/10 * * * ?")
public void takeMeasurements() {
    Instant dateTimeNow = Instant.now();

    // Business impl of measurements taking & storing into DB
}

I've had no problem with this impl in under multiple customer environments (Win, MacOS, Linux) running as SpringBoot containers in docker but I observed that in one Win10 Enterprise environment the timestamp coming from Instant.now() called when CRON job fires has time "in past" e.g. 11:59:59:998 with job that was supposed to be fired at 12:00:00.

I can handle this scenario with some accepted range of deviation (for my purposes it's OK to do "rounding" of +-30s) but for some folks/scenarios it might not be.

Where is the underlying problem of this?

Doesn't CRON use same machine clock as java.time.Instant uses?

0

There are 0 best solutions below