By default spring uses a default instance of TaskScheduler. This example shows how to specify a custom TaskScheduler while using @Scheduled annotation.
Example
Bean with @Scheduled methods
package com.logicbig.example;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.LocalTime;
@Component
public class MyBean {
@Scheduled(fixedDelay = 1000)
public void runTask() {
System.out.printf("task thread: %s, time: %s%n",
Thread.currentThread().getName(),
LocalTime.now());
}
@Scheduled(fixedRate = 2000)
public void runTask2() {
System.out.printf("task 2 thread: %s, time: %s%n",
Thread.currentThread().getName(),
LocalTime.now());
}
}
Configuring a custom TaskScheduler
package com.logicbig.example;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.annotation.ScheduledAnnotationBeanPostProcessor;
import org.springframework.scheduling.concurrent.ConcurrentTaskExecutor;
import org.springframework.scheduling.concurrent.ConcurrentTaskScheduler;
import java.time.LocalTime;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@EnableScheduling
@ComponentScan
@Configuration
public class ScheduledOverrideDefaultExecutorExample {
@Bean
public TaskScheduler taskScheduler() {
return new ConcurrentTaskScheduler(
Executors.newScheduledThreadPool(3));
}
public static void main(String[] args) throws Exception {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(
ScheduledOverrideDefaultExecutorExample.class);
}
}
Outputtask 2 thread: pool-1-thread-1, time: 16:12:11.066611600 task thread: pool-1-thread-2, time: 16:12:11.066611600 task thread: pool-1-thread-2, time: 16:12:12.068969300 task 2 thread: pool-1-thread-3, time: 16:12:13.066237600 task thread: pool-1-thread-1, time: 16:12:13.082184100 task thread: pool-1-thread-1, time: 16:12:14.098223600 task 2 thread: pool-1-thread-2, time: 16:12:15.066513400 task thread: pool-1-thread-3, time: 16:12:15.113458100 task thread: pool-1-thread-3, time: 16:12:16.120636300 task 2 thread: pool-1-thread-1, time: 16:12:17.066409600 task thread: pool-1-thread-2, time: 16:12:17.128454600 task thread: pool-1-thread-2, time: 16:12:18.134474700 task 2 thread: pool-1-thread-3, time: 16:12:19.070154200 task thread: pool-1-thread-1, time: 16:12:19.148299900 task thread: pool-1-thread-1, time: 16:12:20.156524500 .....................
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
|
|