To schedule a task at a fixed rate, use the fixedRate
member. For example:
Fixed Rate Example
Fixed Rate Example
@Scheduled(fixedRate = "5m")
void everyFiveMinutes() {
System.out.println("Executing everyFiveMinutes()")
}
Fixed Rate Example
@Scheduled(fixedRate = "5m")
internal fun everyFiveMinutes() {
println("Executing everyFiveMinutes()")
}
The task above will execute every 5 minutes.
Fixed Delay Example
void fiveMinutesAfterLastExecution() {
System.out.println("Executing fiveMinutesAfterLastExecution()");
}
Fixed Delay Example
Fixed Delay Example
@Scheduled(fixedDelay = "5m")
internal fun fiveMinutesAfterLastExecution() {
println("Executing fiveMinutesAfterLastExecution()")
}
To schedule a task use the cron
member:
Cron Example
@Scheduled(cron = "0 15 10 ? * MON" )
void everyMondayAtTenFifteenAm() {
System.out.println("Executing everyMondayAtTenFifteenAm()");
}
@Scheduled(cron = "0 15 10 ? * MON" )
}
Cron Example
The above example will run the task every Monday morning at 10:15AM for the time zone of the server.
To schedule a task so that it runs a single time after the server starts, use the initialDelay
member:
Initial Delay Example
@Scheduled(initialDelay = "1m" )
void onceOneMinuteAfterStartup() {
System.out.println("Executing onceOneMinuteAfterStartup()");
}
Initial Delay Example
@Scheduled(initialDelay = "1m" )
void onceOneMinuteAfterStartup() {
System.out.println("Executing onceOneMinuteAfterStartup()")
}
@Scheduled(initialDelay = "1m")
internal fun onceOneMinuteAfterStartup() {
}
The above example will only run a single time 1 minute after the server starts.