i need to use @Schedule spring annotation to with cron parameter to run the job for every one hour. I have tried various option but it doesn't seems to work.
Could someone help me with the valid expression to run for every 1 hour?
ex: 1:00 2:00 3:00 4:00 5:00 6:00 7:00 etc.,
Referred: http://www.baeldung.com/spring-scheduled-tasks and http://www.baeldung.com/cron-expressions
Tried the following
0 0/60 * * * *
0 * 0/1 * * *
* * 0/1 * * *
* * 0/60 * * *
Thanks.
FixedRate annotation got worked to run for every hour
@Scheduled(fixedRate=60*60*1000)
public void scheduleFixedRateTask() {
System.out.println(
"Fixed rate task - " + System.currentTimeMillis() / 1000);
}
FixedRate annotation to run for every hour with 10 minutes delay. Incase if you want to make an initial delay to start the job, you can specify "initialDelay". If you specify this value, the the very first time job will be started after the given delay. In the below example, the method is scheduled to run at every hour with initial start delay as 10 minutes.
@Scheduled(fixedRate=60*60*1000, initialDelay=10*60*1000)
@Component
public class SomeScheduler {
@Scheduled(cron = "0 0 0/1 * * ?")
public void print() {
System.out.println("====>> print method()...");
}
}
@EnableScheduling
@Configuration
public class AppStarter {
}
Spring @Scheduled
annotation has a optional element called timeUnit
to setup the time unit use in the fixedDelay
, fixedDelayString
, fixedRate
, fixedRateString
, initialDelay
, and initialDelayString
So to run a job every one hour @Scheduled(fixedRate = 1, timeUnit = TimeUnit.HOURS)
For other time units (TimeUnit Enum constants) see the docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With