Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the default scheduler pool size in spring-boot?

I'm using spring-boot and @Scheduled annotation to execute some tasks.

How can I find out what the default pool size of scheduled tasks is by default in spring-boot?

Reason: the following class does not execute the jobs in parallel, but one after the other. Maybe only a single thread executor is configured by default?

@Service
public class ZipFileTesterAsync {

    @Scheduled(fixedDelay = 60000, initialDelay = 500)
    public void run() throws Exception {
        System.out.println("import 1");
        TimeUnit.MINUTES.sleep(1);
        System.out.println("import 1 finished");
    }

    @Scheduled(fixedDelay = 60000, initialDelay = 1000)
    public void run2() throws Exception {
        System.out.println("import 2");
        TimeUnit.MINUTES.sleep(1);
    }
}

Result: the 2nd job is executed after the first finished.

like image 860
membersound Avatar asked Apr 22 '15 11:04

membersound


People also ask

What is scheduler pool size?

The default scheduler pool size in spring-boot is only one.

What is pool size in ThreadPoolTaskScheduler?

The configured bean threadPoolTaskScheduler can execute tasks asynchronously based on the configured pool size of 5. Note that all ThreadPoolTaskScheduler related thread names will be prefixed with ThreadPoolTaskScheduler.

How many threads would a Spring scheduler come with default Springboot app?

If you do not provide a pool-size attribute, the default thread pool will only have a single thread. So if you have many scheduled tasks, you should configure the scheduler, as explained in the documentation, to have a pool with more threads, to make sure one long task doesn't delay all the other ones.

Can we have multiple scheduler in Spring boot?

You can have multiple schedules as many as you need, and every one with its own schedule.


3 Answers

Yes, all @Scheduled methods share a single thread by default. It is possible to override this behavior by defining a @Configuration such as this:

@Configuration
public class SchedulingConfigurerConfiguration implements SchedulingConfigurer {

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
        taskScheduler.setPoolSize(100);
        taskScheduler.initialize();
        taskRegistrar.setTaskScheduler(taskScheduler);
    }
}

This example ensures that all @Scheduled methods share a thread pool of size 100.

like image 76
Arie Z. Avatar answered Oct 14 '22 00:10

Arie Z.


The default pool size is 1, and you can set the pool size in application.properties science springboot2.1.0 via changing the value of spring.task.scheduling.pool.size.

spring.task.scheduling.pool.size=20

The same task will be executed in serialized when the trigger period is shorter than the execution duration. And Spring Boot will execute different tasks in parallel with a maximum of 20 threads.

like image 35
robothy Avatar answered Oct 14 '22 00:10

robothy


The default scheduler pool size in spring-boot is only one.

In org.springframework.scheduling.config.ScheduledTaskRegistrar:

    /**
     * Schedule all registered tasks against the underlying
     * {@linkplain #setTaskScheduler(TaskScheduler) task scheduler}.
     */
    @SuppressWarnings("deprecation")
    protected void scheduleTasks() {
        if (this.taskScheduler == null) {
            this.localExecutor = Executors.newSingleThreadScheduledExecutor();
            this.taskScheduler = new ConcurrentTaskScheduler(this.localExecutor);
        }
        ...
    }

like image 3
Lebecca Avatar answered Oct 13 '22 22:10

Lebecca