问题
I am just can figure out how to configure a Cron job in Quartz with initial delay. So i need something that runs every hour with an initial delay of 10 min.
"* * 0/1 * * ?"
回答1:
Here's a late answer, hopefully this helps others. I solved the issue by having 2 scheduled functions in my service class:
@EnableScheduling
public class DeviceService {
@Scheduled(initialDelayString = "${devices.update.initial}", fixedDelay = 2592000000L)
public void initialUpdateDevices() {
updateDevices();
}
@Scheduled(cron = "${devices.update.cron}")
public void cronUpdateDevices() {
updateDevices();
}
private void updateDevices() {
...
}
}
The initial delay and the cron expression are set in application.properties. The fixedDelay is there because Spring doesn't allow initialDelay alone. I set it to 2592000000ms, which is 30 days. In our application, the potential extra update doesn't do any harm.
In application.properties:
devices.update.initial = 600000
devices.update.cron = 0 30 1 * * *
Initially run after 10 minutes (60000ms) and then every night at 01:30.
In application-test.properties for unit testing:
devices.update.initial = 86400000
devices.update.cron = 0 30 1 24 12 *
None of our unit tests take 1 day to execute so 86400000 milliseconds is a safe bet. The cron "0 30 1 24 12 *" is set to Christmas Eve's night when people should be dreaming of nice things.
来源:https://stackoverflow.com/questions/14983486/cron-expression-with-initial-delay-quartz