Different taskScheduler for different tasks

Viewed 561

I'm using Spring and I've serveral @Scheduled classes in my application:

@Component
public class CheckHealthTask {

    @Scheduled(fixedDelay = 10_000)
    public void checkHealth() {
        //stuff inside
    }
}


@Component
public class ReconnectTask {
    @Scheduled(fixedDelay = 1200_000)
     public void run() {
           //stuff here
      }
}

I want the first task use a pool of 2 threads, while the second use a single thread. I don't want the second task is stuck because the first one use all threads available and the computation is slower than fixedDelay time. Of course mine is just an example to get you the idea.

I could use a configuration class like this:

@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 don't understand how define a different configuration for each @Scheduled component though.

1 Answers
Related