Skip to content

Commit 3c806d2

Browse files
committed
Polish "Update documentation for Task Execution"
See gh-44926
1 parent cc1e232 commit 3c806d2

File tree

12 files changed

+369
-103
lines changed

12 files changed

+369
-103
lines changed

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

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Spring WebSocket and JPA will use javadoc:org.springframework.core.task.AsyncTas
2424

2525
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.
2626

27-
include-code::TaskExecutionConfigurationExamples[tag=application-task-executor]
27+
include-code::application/MyTaskExecutorConfiguration[]
2828

2929
[NOTE]
3030
====
@@ -40,23 +40,23 @@ It could, however, be used for Spring WebSocket or JPA if the bean's type is jav
4040

4141
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.
4242

43-
include-code::TaskExecutionConfigurationExamples[tag=multiple-task-executor]
43+
include-code::multiple/MyTaskExecutorConfiguration[]
4444

4545
[TIP]
4646
====
4747
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.
4848
49-
include-code::TaskExecutionConfigurationExamples[tag=executor-builder]
49+
include-code::builder/MyTaskExecutorConfiguration[]
5050
====
5151

5252
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].
5353
The following example demonstrates how to achieve this.
5454

55-
include-code::TaskExecutionConfigurationExamples[tag=async-configurer]
55+
include-code::async/MyTaskExecutorConfiguration[]
5656

5757
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:
5858

59-
include-code::TaskExecutionConfigurationExamples[tag=default-candidate-task-executor]
59+
include-code::defaultcandidate/MyTaskExecutorConfiguration[]
6060

6161
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[].
6262
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].

spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/TaskExecutionConfigurationExamples.java

-98
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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.application;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
22+
23+
@Configuration(proxyBeanMethods = false)
24+
public class MyTaskExecutorConfiguration {
25+
26+
@Bean("applicationTaskExecutor")
27+
SimpleAsyncTaskExecutor applicationTaskExecutor() {
28+
return new SimpleAsyncTaskExecutor("app-");
29+
}
30+
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.async;
18+
19+
import java.util.concurrent.Executor;
20+
import java.util.concurrent.ExecutorService;
21+
import java.util.concurrent.Executors;
22+
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.scheduling.annotation.AsyncConfigurer;
26+
27+
@Configuration(proxyBeanMethods = false)
28+
public class MyTaskExecutorConfiguration {
29+
30+
@Bean
31+
AsyncConfigurer asyncConfigurer(ExecutorService executorService) {
32+
return new AsyncConfigurer() {
33+
34+
@Override
35+
public Executor getAsyncExecutor() {
36+
return executorService;
37+
}
38+
39+
};
40+
}
41+
42+
@Bean
43+
ExecutorService executorService() {
44+
return Executors.newCachedThreadPool();
45+
}
46+
47+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
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.builder;
18+
19+
import org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder;
20+
import org.springframework.context.annotation.Bean;
21+
import org.springframework.context.annotation.Configuration;
22+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
23+
24+
@Configuration(proxyBeanMethods = false)
25+
public class MyTaskExecutorConfiguration {
26+
27+
@Bean
28+
SimpleAsyncTaskExecutor taskExecutor(SimpleAsyncTaskExecutorBuilder builder) {
29+
return builder.build();
30+
}
31+
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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.defaultcandidate;
18+
19+
import java.util.concurrent.Executors;
20+
import java.util.concurrent.ScheduledExecutorService;
21+
22+
import org.springframework.beans.factory.annotation.Qualifier;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
26+
@Configuration(proxyBeanMethods = false)
27+
public class MyTaskExecutorConfiguration {
28+
29+
@Bean(defaultCandidate = false)
30+
@Qualifier("scheduledExecutorService")
31+
ScheduledExecutorService scheduledExecutorService() {
32+
return Executors.newSingleThreadScheduledExecutor();
33+
}
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.multiple;
18+
19+
import org.springframework.context.annotation.Bean;
20+
import org.springframework.context.annotation.Configuration;
21+
import org.springframework.core.task.SimpleAsyncTaskExecutor;
22+
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
23+
24+
@Configuration(proxyBeanMethods = false)
25+
public class MyTaskExecutorConfiguration {
26+
27+
@Bean("applicationTaskExecutor")
28+
SimpleAsyncTaskExecutor applicationTaskExecutor() {
29+
return new SimpleAsyncTaskExecutor("app-");
30+
}
31+
32+
@Bean("taskExecutor")
33+
ThreadPoolTaskExecutor taskExecutor() {
34+
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
35+
threadPoolTaskExecutor.setThreadNamePrefix("async-");
36+
return threadPoolTaskExecutor;
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2012-2023 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.application
18+
19+
import org.springframework.context.annotation.Bean
20+
import org.springframework.context.annotation.Configuration
21+
import org.springframework.core.task.SimpleAsyncTaskExecutor
22+
23+
@Configuration(proxyBeanMethods = false)
24+
class MyTaskExecutorConfiguration {
25+
26+
@Bean("applicationTaskExecutor")
27+
fun applicationTaskExecutor(): SimpleAsyncTaskExecutor {
28+
return SimpleAsyncTaskExecutor("app-")
29+
}
30+
31+
}

0 commit comments

Comments
 (0)