Skip to content

Commit fec8b2c

Browse files
committed
Update documentation for Task Execution
Signed-off-by: Dmytro Nosan <dimanosan@gmail.com>
1 parent faef7d5 commit fec8b2c

File tree

2 files changed

+172
-12
lines changed

2 files changed

+172
-12
lines changed

spring-boot-project/spring-boot-docs/src/docs/antora/modules/reference/pages/features/task-execution-and-scheduling.adoc

Lines changed: 76 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,74 @@ In the absence of an javadoc:java.util.concurrent.Executor[] bean in the context
55
When virtual threads are enabled (using Java 21+ and configprop:spring.threads.virtual.enabled[] set to `true`) this will be a javadoc:org.springframework.core.task.SimpleAsyncTaskExecutor[] that uses virtual threads.
66
Otherwise, it will be a javadoc:org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor[] with sensible defaults.
77

8-
If a custom `Executor` bean is present, you can request Spring Boot to auto-configure an `AsyncTaskExecutor` anyway, as follows:
8+
The auto-configured javadoc:org.springframework.core.task.AsyncTaskExecutor[] is used for
9+
the following integrations unless a custom javadoc:java.util.concurrent.Executor[] bean is defined:
10+
11+
- Execution of asynchronous tasks using `@EnableAsync`, unless a bean of type javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] is defined.
12+
- Asynchronous handling of javadoc:java.util.concurrent.Callable[] return values from controller methods in Spring for GraphQL.
13+
- Asynchronous request handling in Spring MVC.
14+
- Support for blocking execution in Spring WebFlux.
15+
16+
While this approach works in most scenarios, Spring Boot allows you to override the auto-configured
17+
javadoc:org.springframework.core.task.AsyncTaskExecutor[]. By default, when a custom
18+
javadoc:java.util.concurrent.Executor[] bean is registered, the auto-configured
19+
javadoc:org.springframework.core.task.AsyncTaskExecutor[] steps aside, and the custom
20+
javadoc:java.util.concurrent.Executor[] is used for regular task execution (via `@EnableAsync`).
21+
However, Spring MVC, Spring WebFlux, and Spring GraphQL all require a bean named `applicationTaskExecutor`.
22+
For Spring MVC and Spring WebFlux, this bean must be of type javadoc:org.springframework.core.task.AsyncTaskExecutor[],
23+
whereas Spring GraphQL does not enforce this type requirement.
24+
25+
The following code snippet demonstrates how to register a custom javadoc:org.springframework.core.task.AsyncTaskExecutor[]
26+
to be used with Spring MVC, Spring WebFlux, Spring GraphQL:
27+
28+
include-code::TaskExecutionConfigurationExamples[tag=application-task-executor]
29+
30+
[NOTE]
31+
====
32+
The `applicationTaskExecutor` bean will also be used for regular task execution if there is no
33+
`@Primary` bean or a bean named `taskExecutor` of type javadoc:java.util.concurrent.Executor[]
34+
or javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] present in the application context.
35+
====
36+
37+
If your application needs multiple `Executor` beans for different integrations,
38+
such as one for regular task execution with `@EnableAsync` and other for Spring MVC, Spring WebFlux, and Spring GraphQL,
39+
you can configure them as follows:
40+
41+
include-code::TaskExecutionConfigurationExamples[tag=multiple-task-executor]
42+
43+
[TIP]
44+
====
45+
The auto-configured javadoc:org.springframework.boot.task.ThreadPoolTaskExecutorBuilder[] or
46+
javadoc:org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder[] allow you to easily create instances
47+
of type javadoc:org.springframework.core.task.AsyncTaskExecutor[] that replicate the default behavior of auto-configuration.
48+
49+
include-code::TaskExecutionConfigurationExamples[tag=executor-builder]
50+
51+
====
52+
53+
[WARNING]
54+
====
55+
If neither the auto-configured `AsyncTaskExecutor` nor the `applicationTaskExecutor` bean is defined,
56+
the application defaults to a bean named `taskExecutor` for regular task execution (`@EnableAsync`),
57+
following Spring Framework's behavior. However, this bean will not be used for Spring MVC, Spring WebFlux, or Spring GraphQL.
58+
====
59+
60+
If a `taskExecutor` named bean is not an option, you can define an
61+
javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] bean to specify the
62+
`Executor` responsible for handling regular task execution with `@EnableAsync`.
63+
The following example demonstrates how to achieve this.
64+
65+
include-code::TaskExecutionConfigurationExamples[tag=async-configurer]
66+
67+
To register a custom javadoc:java.util.concurrent.Executor[] while keeping the auto-configured
68+
javadoc:org.springframework.core.task.AsyncTaskExecutor[], you can create a custom
69+
javadoc:java.util.concurrent.Executor[] bean and set the `defaultCandidate=false` attribute in its
70+
javadoc:org.springframework.context.annotation.Bean[format=annotation] annotation,
71+
as demonstrated in the following example.
72+
73+
include-code::TaskExecutionConfigurationExamples[tag=default-candidate-task-executor]
74+
75+
If for some reason, it is not possible, you can request Spring Boot to auto-configure an `AsyncTaskExecutor` anyway, as follows:
976

1077
[configprops,yaml]
1178
----
@@ -15,7 +82,9 @@ spring:
1582
mode: force
1683
----
1784

18-
The auto-configured executor will be automatically used for:
85+
The auto-configured javadoc:org.springframework.core.task.AsyncTaskExecutor[] will be used automatically
86+
for all integrations, even if a custom javadoc:java.util.concurrent.Executor[] bean is registered,
87+
including those marked as `@Primary`. These integrations include:
1988

2089
- Asynchronous task execution (`@EnableAsync`), unless an javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] bean is present.
2190
- Spring for GraphQL's asynchronous handling of javadoc:java.util.concurrent.Callable[] return values from controller methods.
@@ -24,22 +93,17 @@ The auto-configured executor will be automatically used for:
2493

2594
[TIP]
2695
====
27-
If you have defined a custom javadoc:java.util.concurrent.Executor[] in the context, both regular task execution (that is javadoc:org.springframework.scheduling.annotation.EnableAsync[format=annotation]) and Spring for GraphQL will use it.
28-
However, the Spring MVC and Spring WebFlux support will only use it if it is an javadoc:org.springframework.core.task.AsyncTaskExecutor[] implementation named `applicationTaskExecutor`.
29-
3096
Depending on your target arrangement, you could set configprop:spring.task.execution.mode[] to `force` to auto-configure an `applicationTaskExecutor`, change your javadoc:java.util.concurrent.Executor[] into an javadoc:org.springframework.core.task.AsyncTaskExecutor[] or define both an javadoc:org.springframework.core.task.AsyncTaskExecutor[] and an javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] wrapping your custom javadoc:java.util.concurrent.Executor[].
31-
32-
Another option is to define those beans explicitly.
33-
The auto-configured javadoc:org.springframework.boot.task.ThreadPoolTaskExecutorBuilder[] or javadoc:org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder[] allow you to easily create instances that reproduce what the auto-configuration does by default.
3497
====
3598

36-
[NOTE]
99+
[WARNING]
37100
====
38-
If multiple javadoc:java.util.concurrent.Executor[] beans are defined with configprop:spring.task.execution.mode[] to `force`, all the supported integrations look for a bean named `applicationTaskExecutor`.
39-
If the auto-configured `AsyncTaskExecutor` is not defined, only regular task execution fallbacks to a bean named `taskExecutor` to match Spring Framework's behavior.
101+
When `force` mode is enabled, `applicationTaskExecutor` will also be configured for regular
102+
task execution with `@EnableAsync`, even if a `@Primary` bean or a bean named `taskExecutor`
103+
of type javadoc:java.util.concurrent.Executor[] is present. The only way to override the `Executor`
104+
for regular tasks is by registering an javadoc:org.springframework.scheduling.annotation.AsyncConfigurer[] bean.
40105
====
41106

42-
43107
When a javadoc:org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor[] is auto-configured, the thread pool uses 8 core threads that can grow and shrink according to the load.
44108
Those default settings can be fine-tuned using the `spring.task.execution` namespace, as shown in the following example:
45109

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* Copyright 2012-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.features.taskexecutionandscheduling;
18+
19+
import java.util.concurrent.Executor;
20+
import java.util.concurrent.ExecutorService;
21+
import java.util.concurrent.Executors;
22+
import java.util.concurrent.ScheduledExecutorService;
23+
24+
import org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder;
25+
import org.springframework.context.annotation.Bean;
26+
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
28+
import org.springframework.scheduling.annotation.AsyncConfigurer;
29+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
30+
31+
public class TaskExecutionConfigurationExamples {
32+
33+
// tag::default-candidate-task-executor[]
34+
@Bean(defaultCandidate = false)
35+
ScheduledExecutorService scheduledExecutorService() {
36+
return Executors.newSingleThreadScheduledExecutor();
37+
}
38+
// end::default-candidate-task-executor[]
39+
40+
// tag::application-task-executor[]
41+
@Bean("applicationTaskExecutor")
42+
SimpleAsyncTaskExecutor applicationTaskExecutor() {
43+
return new SimpleAsyncTaskExecutor("app-");
44+
}
45+
// end::application-task-executor[]
46+
47+
// tag::executor-builder[]
48+
@Bean
49+
SimpleAsyncTaskExecutor taskExecutor(SimpleAsyncTaskExecutorBuilder builder) {
50+
return builder.build();
51+
}
52+
// end::executor-builder[]
53+
54+
static class MultipleTaskExecutor {
55+
56+
// tag::multiple-task-executor[]
57+
@Bean("applicationTaskExecutor")
58+
SimpleAsyncTaskExecutor applicationTaskExecutor() {
59+
return new SimpleAsyncTaskExecutor("app-");
60+
}
61+
62+
@Bean("taskExecutor")
63+
ThreadPoolTaskExecutor taskExecutor() {
64+
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
65+
threadPoolTaskExecutor.setThreadNamePrefix("async-");
66+
return threadPoolTaskExecutor;
67+
}
68+
// end::multiple-task-executor[]
69+
70+
}
71+
72+
// tag::async-configurer[]
73+
@Configuration(proxyBeanMethods = false)
74+
public class TaskExecutionConfiguration {
75+
76+
@Bean
77+
AsyncConfigurer asyncConfigurer(ExecutorService executorService) {
78+
return new AsyncConfigurer() {
79+
80+
@Override
81+
public Executor getAsyncExecutor() {
82+
return executorService;
83+
}
84+
85+
};
86+
}
87+
88+
@Bean
89+
ExecutorService executorService() {
90+
return Executors.newCachedThreadPool();
91+
}
92+
93+
}
94+
// end::async-configurer[]
95+
96+
}

0 commit comments

Comments
 (0)