'I have two different classes which uses @EnableScheduling but they are using same executor service

I want both classes to use different executor services. but both are picking the same executor.

class implements SchedulingConfigurer

    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        taskRegistrar.setScheduler(taskExecutor());
    }

    @Bean()
    public ThreadPoolTaskScheduler taskScheduler() {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(2);
        taskScheduler.setThreadNamePrefix("taskScheduler1 - ");
        return taskScheduler;
    }

Similarly in another class, I'm using this setup with setThreadNamePrefix as taskScheduler2.

But both the classes are using taskScheduler1


Solution 1:[1]

I did not find a way to use different ThreadPoolTaskScheduler for two scheduled tasks. I think using @Async can do something similar. Put the business code in an asynchronous task and specify a different thread pool to run.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 HUTUTU