Spring-boot: Provide properties to set the number of threads of spring TaskScheduler

Created on 19 Aug 2014  路  5Comments  路  Source: spring-projects/spring-boot

By default, it use Executors.newSingleThreadScheduledExecutor() to create executor, and it only have one thread.
But when I config the executor manually:

    @Bean
    public ConcurrentTaskScheduler taskScheduler() {
        ConcurrentTaskScheduler taskScheduler = new ConcurrentTaskScheduler();
        taskScheduler.setScheduledExecutor(executorService());
        return taskScheduler;
    }

    @Bean
    public ScheduledExecutorService executorService() {
        ScheduledExecutorFactoryBean bean = new ScheduledExecutorFactoryBean();
        bean.setThreadNamePrefix("schedule");
        bean.setPoolSize(10);
        return bean.getObject();
    }

Exception occurs:

Exception in thread "main" java.lang.IllegalStateException: More than one TaskScheduler and/or ScheduledExecutorService  exist within the context.
enhancement

Most helpful comment

There is no _explicit_ scheduling support in Spring boot if that's what your question is. Are you saying we should provide an easier way to configure the executor service used by @EnableScheduling?

All 5 comments

When I config as follow:

    @Bean
    public ScheduledExecutorFactoryBean executorService() {
        ScheduledExecutorFactoryBean bean = new ScheduledExecutorFactoryBean();
        bean.setThreadNamePrefix("schedule-");
        bean.setPoolSize(10);
        return bean;
    }

It, works.
However, is there any convenient way to config executor pool size. like:

spring.datasource.max-active: 20

to config:

spring.scheduling.poolSize: 20

There is no _explicit_ scheduling support in Spring boot if that's what your question is. Are you saying we should provide an easier way to configure the executor service used by @EnableScheduling?

@snicoll yes, my English is very poor.

Duplicates #1563.

That's not a duplicate of #1563 - #1563 is about TaskExecutor and this one is about TaskScheduler. Those are two completely separate contracts.

Was this page helpful?
0 / 5 - 0 ratings