@Scheduled annotation's attributes fixedDelayString, fixedRateString, initialDelayString and cron allow to use external properties via property placeholder such as ${my.app.myProp}
Example
Example Property file
src/main/resources/scheduling.propertiesfixed-rate=2000
fixed-delay=3000
initial-delay=1000
cron-expression=*/4 * * * * *
The bean methods using @Scheduled with external properties
package com.logicbig.example;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalTime;
@Component
public class MyBean {
@Scheduled(initialDelayString = "${initial-delay}",
fixedDelayString = "${fixed-delay}")
public void runTask() {
System.out.printf("Running scheduled task1 " +
" thread: %s, time: %s%n",
Thread.currentThread().getName(),
LocalTime.now());
}
@Scheduled(initialDelayString = "${initial-delay}",
fixedRateString = "${fixed-rate}")
public void runTask2() {
System.out.printf("Running scheduled task2 " +
" thread: %s, time: %s%n",
Thread.currentThread().getName(),
LocalTime.now());
}
@Scheduled(cron = "${cron-expression}")
public void runTask3() {
System.out.printf("Running scheduled task3 " +
" thread: %s, time: %s%n",
Thread.currentThread().getName(),
LocalTime.now());
}
}
The main class
The configuration class should specify what property file we want to load via @PropertySource
@ComponentScan
@EnableScheduling
@Configuration
@PropertySource("classpath:scheduling.properties")
public class ScheduledExample {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
ScheduledExample.class);
}
}
OutputRunning scheduled task2 thread: pool-1-thread-1, time: 16:09:01.292489300 Running scheduled task1 thread: pool-1-thread-1, time: 16:09:01.292489300 Running scheduled task2 thread: pool-1-thread-1, time: 16:09:03.286634600 Running scheduled task3 thread: pool-1-thread-1, time: 16:09:04.002911300 Running scheduled task1 thread: pool-1-thread-1, time: 16:09:04.301844700 Running scheduled task2 thread: pool-1-thread-1, time: 16:09:05.297963200 Running scheduled task2 thread: pool-1-thread-1, time: 16:09:07.301290700 Running scheduled task1 thread: pool-1-thread-1, time: 16:09:07.317009300 Running scheduled task3 thread: pool-1-thread-1, time: 16:09:08.016077700 Running scheduled task2 thread: pool-1-thread-1, time: 16:09:09.293244200 Running scheduled task1 thread: pool-1-thread-1, time: 16:09:10.333258500 Running scheduled task2 thread: pool-1-thread-1, time: 16:09:11.297501200 Running scheduled task3 thread: pool-1-thread-1, time: 16:09:12.001694800 Running scheduled task2 thread: pool-1-thread-1, time: 16:09:13.295883800 Running scheduled task1 thread: pool-1-thread-1, time: 16:09:13.342918600 Running scheduled task2 thread: pool-1-thread-1, time: 16:09:15.290727300 Running scheduled task3 thread: pool-1-thread-1, time: 16:09:16.009899100 Running scheduled task1 thread: pool-1-thread-1, time: 16:09:16.355963 Running scheduled task2 thread: pool-1-thread-1, time: 16:09:17.286473100 Running scheduled task2 thread: pool-1-thread-1, time: 16:09:19.296747700 Running scheduled task1 thread: pool-1-thread-1, time: 16:09:19.359342200 Running scheduled task3 thread: pool-1-thread-1, time: 16:09:20.013199900 ......................
Example ProjectDependencies and Technologies Used: - spring-context 6.2.12 (Spring Context)
Version Compatibility: 4.3.0.RELEASE - 6.2.12 Version compatibilities of spring-context with this example: Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|
|