orberkov
orberkov

Reputation: 1155

Spring TaskScheduler not starting all @Scheduled Jobs

Our Spring project doesn't start all the @Scheduled job functions, please help us figure our why.

Here is our configuration:

@EnableScheduling
@EnableAsync
@Configuration
public class AppConfig {
    private static final Logger LOGGER = LoggerFactory.getLogger(AppConfig.class);

    @Bean
    public TaskScheduler taskScheduler() {
        LOGGER.info("Configuring TaskScheduler");
        ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler();
        threadPoolTaskScheduler.setPoolSize(500);
        threadPoolTaskScheduler.setThreadNamePrefix("ThreadPoolTaskScheduler");
        return threadPoolTaskScheduler;
    }

    @Bean
    public TaskExecutor threadPoolTaskExecutor() {
        LOGGER.info("Configuring TaskExecutor. Available Processors=" + Runtime.getRuntime().availableProcessors());
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
        executor.setMaxPoolSize(500);
        executor.initialize();
        return executor;
    }
}

As you can see, we are configuring both the TaskScheduler and the TaskExecutor.

Of course, our XMLs are configured too

<task:annotation-driven executor="executor" scheduler="scheduler" proxy-target-class="true" />
<task:executor id="executor" pool-size="200" />
<task:scheduler id="scheduler" pool-size="200" />

It seems that for some reason, only SOME of our @Scheduled Tasks are starting as required - but the rest of them do not.

We don't see any errors or warnings in the logs:

The @Configuration is running as planned, and prints the messages to the logs.

Configuring TaskScheduler
Configuring TaskExecutor. Available Processors=16

All of our @Scheduled tasks reside in @Component(s);

Upvotes: 1

Views: 699

Answers (1)

orberkov
orberkov

Reputation: 1155

The issue was eventually solved by reference of this SO question

The missing part was the NAMESPACE

the AppConfig resided in package com.companyname.service

while some other @Scheduled task resided in other packages.

We moved the AppConfig to com.companyname

All the @Scheduled tasks started to work.

Quoting Swapnil Pasarkar:

"The package of the scheduler jobs should be under the main Application class's package. e.g com.company is your main application class package then scheduler class package should be com.company.scheduler"

Upvotes: 1

Related Questions