From cfecdfcb9257abcb4e0d417f11215d543737e0ca Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Mon, 7 Apr 2025 11:36:21 +0200 Subject: [PATCH] Polish "Update documentation for Task Execution" See gh-44926 --- .../task-execution-and-scheduling.adoc | 35 ++++++------------- 1 file changed, 11 insertions(+), 24 deletions(-) diff --git a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/task-execution-and-scheduling.adoc b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/task-execution-and-scheduling.adoc index 3dd15f9d846..3dd08aa71d8 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/task-execution-and-scheduling.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/task-execution-and-scheduling.adoc @@ -14,67 +14,54 @@ The auto-configured javadoc:org.springframework.core.task.AsyncTaskExecutor[] is - Utilized for inbound and outbound message channels in Spring WebSocket. - Acts as a bootstrap executor for JPA, based on the bootstrap mode of JPA repositories. -While this approach works in most scenarios, Spring Boot allows you to override the auto-configured -javadoc:org.springframework.core.task.AsyncTaskExecutor[]. -By default, when a custom javadoc:java.util.concurrent.Executor[] bean is registered, the auto-configured -javadoc:org.springframework.core.task.AsyncTaskExecutor[] steps aside, and the custom javadoc:java.util.concurrent.Executor[] is used for regular task execution (via javadoc:org.springframework.scheduling.annotation.EnableAsync[format=annotation]). +While this approach works in most scenarios, Spring Boot allows you to override the auto-configured javadoc:org.springframework.core.task.AsyncTaskExecutor[]. +By default, when a custom javadoc:java.util.concurrent.Executor[] bean is registered, the auto-configured javadoc:org.springframework.core.task.AsyncTaskExecutor[] backs off, and the custom javadoc:java.util.concurrent.Executor[] is used for regular task execution (via javadoc:org.springframework.scheduling.annotation.EnableAsync[format=annotation]). However, Spring MVC, Spring WebFlux, and Spring GraphQL all require a bean named `applicationTaskExecutor`. For Spring MVC and Spring WebFlux, this bean must be of type javadoc:org.springframework.core.task.AsyncTaskExecutor[], whereas Spring GraphQL does not enforce this type requirement. Spring WebSocket and JPA will use javadoc:org.springframework.core.task.AsyncTaskExecutor[] if either a single bean of this type is available or a bean named `applicationTaskExecutor` is defined. -The following code snippet demonstrates how to register a custom javadoc:org.springframework.core.task.AsyncTaskExecutor[] -to be used with Spring MVC, Spring WebFlux, Spring GraphQL, Spring WebSocket and JPA. +The following code snippet demonstrates how to register a custom javadoc:org.springframework.core.task.AsyncTaskExecutor[] to be used with Spring MVC, Spring WebFlux, Spring GraphQL, Spring WebSocket and JPA. include-code::TaskExecutionConfigurationExamples[tag=application-task-executor] [NOTE] ==== -The `applicationTaskExecutor` bean will also be used for regular task execution if there is no -javadoc:org.springframework.context.annotation.Primary[format=annotation] bean or a bean named `taskExecutor` of type javadoc:java.util.concurrent.Executor[] -or javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] present in the application context. +The `applicationTaskExecutor` bean will also be used for regular task execution if there is no javadoc:org.springframework.context.annotation.Primary[format=annotation] bean or a bean named `taskExecutor` of type javadoc:java.util.concurrent.Executor[] or javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] present in the application context. ==== [WARNING] ==== If neither the auto-configured `AsyncTaskExecutor` nor the `applicationTaskExecutor` bean is defined, the application defaults to a bean named `taskExecutor` for regular task execution (javadoc:org.springframework.scheduling.annotation.EnableAsync[format=annotation]), following Spring Framework's behavior. However, this bean will not be used for Spring MVC, Spring WebFlux, Spring GraphQL. -It could, however, be used for Spring WebSocket or JPA if bean's type is javadoc:org.springframework.core.task.AsyncTaskExecutor[]. +It could, however, be used for Spring WebSocket or JPA if the bean's type is javadoc:org.springframework.core.task.AsyncTaskExecutor[]. ==== -If your application needs multiple `Executor` beans for different integrations, such as one for regular task execution with javadoc:org.springframework.scheduling.annotation.EnableAsync[format=annotation] -and other for Spring MVC, Spring WebFlux, Spring WebSocket and JPA you can configure them as follows. +If your application needs multiple `Executor` beans for different integrations, such as one for regular task execution with javadoc:org.springframework.scheduling.annotation.EnableAsync[format=annotation] and other for Spring MVC, Spring WebFlux, Spring WebSocket and JPA, you can configure them as follows. include-code::TaskExecutionConfigurationExamples[tag=multiple-task-executor] [TIP] ==== -The auto-configured javadoc:org.springframework.boot.task.ThreadPoolTaskExecutorBuilder[] or -javadoc:org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder[] allow you to easily create instances of type javadoc:org.springframework.core.task.AsyncTaskExecutor[] that replicate the default behavior of auto-configuration. +The auto-configured javadoc:org.springframework.boot.task.ThreadPoolTaskExecutorBuilder[] or javadoc:org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder[] allow you to easily create instances of type javadoc:org.springframework.core.task.AsyncTaskExecutor[] that replicate the default behavior of auto-configuration. include-code::TaskExecutionConfigurationExamples[tag=executor-builder] ==== -If a `taskExecutor` named bean is not an option, you can mark your bean as javadoc:org.springframework.context.annotation.Primary[format=annotation] or define an -javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] bean to specify the -`Executor` responsible for handling regular task execution with javadoc:org.springframework.scheduling.annotation.EnableAsync[format=annotation]. +If a `taskExecutor` named bean is not an option, you can mark your bean as javadoc:org.springframework.context.annotation.Primary[format=annotation] or define an javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] bean to specify the `Executor` responsible for handling regular task execution with javadoc:org.springframework.scheduling.annotation.EnableAsync[format=annotation]. The following example demonstrates how to achieve this. include-code::TaskExecutionConfigurationExamples[tag=async-configurer] -To register a custom javadoc:java.util.concurrent.Executor[] while keeping the auto-configured -javadoc:org.springframework.core.task.AsyncTaskExecutor[], you can create a custom -javadoc:java.util.concurrent.Executor[] bean and set the `defaultCandidate=false` attribute in its -javadoc:org.springframework.context.annotation.Bean[format=annotation] annotation, as demonstrated in the following example: +To register a custom javadoc:java.util.concurrent.Executor[] while keeping the auto-configured javadoc:org.springframework.core.task.AsyncTaskExecutor[], you can create a custom javadoc:java.util.concurrent.Executor[] bean and set the `defaultCandidate=false` attribute in its javadoc:org.springframework.context.annotation.Bean[format=annotation] annotation, as demonstrated in the following example: include-code::TaskExecutionConfigurationExamples[tag=default-candidate-task-executor] -In that case, you will be able to javadoc:org.springframework.beans.factory.annotation.Autowired[format=annotation] -your custom javadoc:java.util.concurrent.Executor[] into other components while retaining the auto-configured `AsyncTaskExecutor`. +In that case, you will be able to autowire your custom javadoc:java.util.concurrent.Executor[] into other components while retaining the auto-configured javadoc:org.springframework.core.task.AsyncTaskExecutor[]. However, remember to use the javadoc:org.springframework.beans.factory.annotation.Qualifier[format=annotation] annotation alongside javadoc:org.springframework.beans.factory.annotation.Autowired[format=annotation]. -If for some reason, it is not possible, you can request Spring Boot to auto-configure an `AsyncTaskExecutor` anyway, as follows: +If this is not possible for you, you can request Spring Boot to auto-configure an javadoc:org.springframework.core.task.AsyncTaskExecutor[] anyway, as follows: [configprops,yaml] ----