I would like to suggest that you write a document on TaskExecutionAutoConfiguration about
the default TaskExecutor settings in spring boot.
In spring's @EnableAsync documentation, it says that the default setting for TaskExecutor is SimpleAsyncTaskExecutor.
By default, Spring will be searching for an associated thread pool definition: either a unique org.springframework.core.task.TaskExecutor bean in the context, or an java.util.concurrent.Executor bean named "taskExecutor" otherwise. If neither of the two is resolvable, a org.springframework.core.task.SimpleAsyncTaskExecutor will be used to process async method invocations.
However, in Spring Boot, because of AutoConfiguration, the default value is set to ThreadPoolTaskExecutor. I had confusion in my work because of this.
If you look at the TaskExecutorConfigurations of the TaskExecutionAutoConfiguration class, which supports the automatic configuration of TaskExecutors, the default TaskExecutor is determined by whether it is a virtual thread or a platform thread.
TaskExecutorConfigurations.java
class TaskExecutorConfigurations {
@Configuration(proxyBeanMethods = false)
@ConditionalOnMissingBean(Executor.class)
@SuppressWarnings("removal")
static class TaskExecutorConfiguration {
@Bean(name = { TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME,
AsyncAnnotationBeanPostProcessor.DEFAULT_TASK_EXECUTOR_BEAN_NAME })
@ConditionalOnThreading(Threading.VIRTUAL)
SimpleAsyncTaskExecutor applicationTaskExecutorVirtualThreads(SimpleAsyncTaskExecutorBuilder builder) {
return builder.build();
}
@Lazy
@Bean(name = { TaskExecutionAutoConfiguration.APPLICATION_TASK_EXECUTOR_BEAN_NAME,
AsyncAnnotationBeanPostProcessor.DEFAULT_TASK_EXECUTOR_BEAN_NAME })
@ConditionalOnThreading(Threading.PLATFORM)
ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder taskExecutorBuilder,
ObjectProvider<ThreadPoolTaskExecutorBuilder> threadPoolTaskExecutorBuilderProvider) {
ThreadPoolTaskExecutorBuilder threadPoolTaskExecutorBuilder = threadPoolTaskExecutorBuilderProvider
.getIfUnique();
if (threadPoolTaskExecutorBuilder != null) {
return threadPoolTaskExecutorBuilder.build();
}
return taskExecutorBuilder.build();
}
}
...
The @EnableAsync is fine in the code documentation of the Spring Project,
but what do you think about having a guide for it in SpringBoot?
There is a suggestion that if TaskExecutionAutoConfiguration guides this,
there will be no confusion with the documentation in Spring Project
Comment From: bclozel
We do have a section for this here: https://docs.spring.io/spring-boot/reference/features/task-execution-and-scheduling.html
Can you explain what is missing?
Comment From: limwoobin
We're sorry. We are not aware of the content of this article. Please ignore this suggestion The documentation is sufficiently descriptive