Spring's @Scheduled annotation's fixedRate property can be used to execute the annotated method with a fixed rate. The difference between fixedRate and fixedDelay is: fixedDelay waits for the previous execution to complete PLUS the delay period before starting the next execution. fixedRate executes at fixed intervals regardless of previous execution completion.
Example
Bean using @Scheduled annotation
Following example schedules two methods with fixed rate, one uses the property fixedRate and another one uses fixedRateString.
package com.logicbig.example;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalTime;
@Component
public class MyBean {
@Scheduled(initialDelay = 1000, fixedRate = 1000)
public void runTask() {
System.out.printf("Running scheduled task " +
" thread: %s, time: %s%n",
Thread.currentThread().getName(),
LocalTime.now());
}
@Scheduled(initialDelay = 1000,
fixedRateString = "2000")
public void runTask2() {
System.out.printf("Running scheduled task 2" +
" thread: %s, time: %s%n",
Thread.currentThread().getName(),
LocalTime.now());
}
}
Main class
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
@ComponentScan
@EnableScheduling
@Configuration
public class ScheduledExample {
public static void main(String[] args){
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
ScheduledExample.class);
}
}
OutputRunning scheduled task thread: pool-1-thread-1, time: 16:12:30.819769600 Running scheduled task 2 thread: pool-1-thread-1, time: 16:12:30.820770200 Running scheduled task thread: pool-1-thread-1, time: 16:12:31.812907700 Running scheduled task thread: pool-1-thread-1, time: 16:12:32.809861300 Running scheduled task 2 thread: pool-1-thread-1, time: 16:12:32.825956900 Running scheduled task thread: pool-1-thread-1, time: 16:12:33.822800300 Running scheduled task thread: pool-1-thread-1, time: 16:12:34.815637500 Running scheduled task 2 thread: pool-1-thread-1, time: 16:12:34.830898900 Running scheduled task thread: pool-1-thread-1, time: 16:12:35.817561600 Running scheduled task thread: pool-1-thread-1, time: 16:12:36.816961600 Running scheduled task 2 thread: pool-1-thread-1, time: 16:12:36.832602400 Running scheduled task thread: pool-1-thread-1, time: 16:12:37.823146200 Running scheduled task thread: pool-1-thread-1, time: 16:12:38.818677300 Running scheduled task 2 thread: pool-1-thread-1, time: 16:12:38.819673600 Running scheduled task thread: pool-1-thread-1, time: 16:12:39.833400500 .....................
Example ProjectDependencies and Technologies Used: - spring-context 6.2.12 (Spring Context)
Version Compatibility: 3.2.9.RELEASE - 6.2.12 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|
|