Search code examples
springspring-bootspring-integration

Spring Boot and Spring Integration does not fit together


I have a short question. We are using spring integration in our project and faced a problem after Upgrading the spring boot version. The threadpool size is 1 instead of 10 per default.

I analysed it and found out the following:

The problem is that with the newer version with autoconfiguration (https://github.com/spring-projects/spring-boot/blob/cd018aff9c65009d5e0ba47f48fa748eac6bff12/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/integration/IntegrationAutoConfiguration.java) a ThreadPoolTaskScheduler is initialized Line 174 if none exists, but with 0 configuration. Then when we use spring-boot-starter-integration normally with the following class (https://github.com/spring-projects/spring-integration/blob/c4ee5512f6f7170ada43af4f1167d74adb2e0dd2/spring-integration-core/src/main/java/org/springframework/integration/config/DefaultConfiguringBeanFactoryPostProcessor.java) also a TaskScheduler is configured Line 286, but this time with PoolSize 10. But because there already exists a TaskScheduler in ApplicationContext not a second one is created and the threadPoolSize stays on 1.

My question is now: Why does spring-boot has to create a TaskScheduler on its own when it will be also created by spring-integration itself?Is it the responsibility of Spring Boot to create it?

I made a example project: https://drive.google.com/file/d/1xmDpZ5SmQlNN948uoyQun91JJ0AkTq-F/view?usp=sharing

These are my Breakpoints in IntegrationAutoConfiguration.java and DefaultConfiguringBeanFactoryPostProcessor.java.

I expected that the Breakpoint in DefaultConfiguringBeanFactoryPostProcessor.java get reached but it does not. DefaultConfiguringBeanFactoryPostProcessor.java IntegrationAutoConfiguration.java


Solution

  • The TaskScheduler is needed not only for Spring Integration. For example see @EnableScheduling in Spring Framework: https://docs.spring.io/spring-framework/reference/integration/scheduling.html. There are also might be many other places where we would need to schedule some tasks without Spring Integration involved.

    Since TaskScheduler is crucial for Spring Integration polling endpoints, it creates one for us automatically without Spring Boot - for convenience.

    Since Spring Boot is a framework for microservices it was a sane choice to have a pool for that TaskScheduler as 1 - one task per service, but properly. When Spring Integration is involved in Spring Boot environment, it just honors whatever is there auto-configured. For consistency with Spring Boot best practice.

    If one thread is not enough for your in that pool, you can provide a respective configuration property - spring.task.scheduling.pool.size=10: https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#features.task-execution-and-scheduling.