Different taskScheduler for different tasks

I am using Spring, and I have @Scheduled server 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 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.

+4
source share
1 answer

The first task does not require a pool of two threads.

, . fixedDelay :

@Scheduled(fixedDelay=5000)
public void doSomething() {
// something that should execute periodically
}

5 , , .

, , , .

0

Source: https://habr.com/ru/post/1681854/


All Articles