I am using Spring, and I have @Scheduled server classes in my application:
@Component
public class CheckHealthTask {
@Scheduled(fixedDelay = 10_000)
public void checkHealth() {
}
}
@Component
public class ReconnectTask {
@Scheduled(fixedDelay = 1200_000)
public void run() {
}
}
I want the first task to use a pool of two threads, and the second to use one thread. I don’t want the second task stuck, because the first uses all available threads, and the calculation is slower than fixedDelay. Of course, my example is just an example that will help you with this.
I could use the configuration class as follows:
@Configuration
@EnableScheduling
public class TaskConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(taskScheduler());
}
@Bean
public Executor taskScheduler() {
ThreadPoolTaskScheduler t = new ThreadPoolTaskScheduler();
t.setPoolSize(2);
t.setThreadNamePrefix("taskScheduler - ");
t.initialize();
return t;
}
}
I do not understand how to define a different configuration for each @Scheduled component.
source
share