From 3c806d2b31cf1ccbc14ad1497e77060823e58a48 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 7 Apr 2025 14:18:37 -0700 Subject: [PATCH] Polish "Update documentation for Task Execution" See gh-44926 --- .../task-execution-and-scheduling.adoc | 10 +- .../TaskExecutionConfigurationExamples.java | 98 ------------------- .../MyTaskExecutorConfiguration.java | 31 ++++++ .../async/MyTaskExecutorConfiguration.java | 47 +++++++++ .../builder/MyTaskExecutorConfiguration.java | 32 ++++++ .../MyTaskExecutorConfiguration.java | 35 +++++++ .../multiple/MyTaskExecutorConfiguration.java | 39 ++++++++ .../MyTaskExecutorConfiguration.kt | 31 ++++++ .../async/MyTaskExecutorConfiguration.kt | 43 ++++++++ .../builder/MyTaskExecutorConfiguration.kt | 32 ++++++ .../MyTaskExecutorConfiguration.kt | 34 +++++++ .../multiple/MyTaskExecutorConfiguration.kt | 40 ++++++++ 12 files changed, 369 insertions(+), 103 deletions(-) delete mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/TaskExecutionConfigurationExamples.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/application/MyTaskExecutorConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/async/MyTaskExecutorConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/builder/MyTaskExecutorConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/defaultcandidate/MyTaskExecutorConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/multiple/MyTaskExecutorConfiguration.java create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/application/MyTaskExecutorConfiguration.kt create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/async/MyTaskExecutorConfiguration.kt create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/builder/MyTaskExecutorConfiguration.kt create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/defaultcandidate/MyTaskExecutorConfiguration.kt create mode 100644 spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/multiple/MyTaskExecutorConfiguration.kt 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 3dd08aa71d..00842a9cea 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 @@ -24,7 +24,7 @@ Spring WebSocket and JPA will use javadoc:org.springframework.core.task.AsyncTas 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] +include-code::application/MyTaskExecutorConfiguration[] [NOTE] ==== @@ -40,23 +40,23 @@ It could, however, be used for Spring WebSocket or JPA if the bean's type is jav 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] +include-code::multiple/MyTaskExecutorConfiguration[] [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. -include-code::TaskExecutionConfigurationExamples[tag=executor-builder] +include-code::builder/MyTaskExecutorConfiguration[] ==== 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] +include-code::async/MyTaskExecutorConfiguration[] 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] +include-code::defaultcandidate/MyTaskExecutorConfiguration[] 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]. diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/TaskExecutionConfigurationExamples.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/TaskExecutionConfigurationExamples.java deleted file mode 100644 index ca7321ca1e..0000000000 --- a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/TaskExecutionConfigurationExamples.java +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright 2012-2025 the original author or authors. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.springframework.boot.docs.features.taskexecutionandscheduling; - -import java.util.concurrent.Executor; -import java.util.concurrent.ExecutorService; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; - -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.core.task.SimpleAsyncTaskExecutor; -import org.springframework.scheduling.annotation.AsyncConfigurer; -import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; - -public class TaskExecutionConfigurationExamples { - - // tag::default-candidate-task-executor[] - @Bean(defaultCandidate = false) - @Qualifier("scheduledExecutorService") - ScheduledExecutorService scheduledExecutorService() { - return Executors.newSingleThreadScheduledExecutor(); - } - // end::default-candidate-task-executor[] - - // tag::application-task-executor[] - @Bean("applicationTaskExecutor") - SimpleAsyncTaskExecutor applicationTaskExecutor() { - return new SimpleAsyncTaskExecutor("app-"); - } - // end::application-task-executor[] - - // tag::executor-builder[] - @Bean - SimpleAsyncTaskExecutor taskExecutor(SimpleAsyncTaskExecutorBuilder builder) { - return builder.build(); - } - // end::executor-builder[] - - static class MultipleTaskExecutor { - - // tag::multiple-task-executor[] - @Bean("applicationTaskExecutor") - SimpleAsyncTaskExecutor applicationTaskExecutor() { - return new SimpleAsyncTaskExecutor("app-"); - } - - @Bean("taskExecutor") - ThreadPoolTaskExecutor taskExecutor() { - ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); - threadPoolTaskExecutor.setThreadNamePrefix("async-"); - return threadPoolTaskExecutor; - } - // end::multiple-task-executor[] - - } - - // tag::async-configurer[] - @Configuration(proxyBeanMethods = false) - public class TaskExecutionConfiguration { - - @Bean - AsyncConfigurer asyncConfigurer(ExecutorService executorService) { - return new AsyncConfigurer() { - - @Override - public Executor getAsyncExecutor() { - return executorService; - } - - }; - } - - @Bean - ExecutorService executorService() { - return Executors.newCachedThreadPool(); - } - - } - // end::async-configurer[] - -} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/application/MyTaskExecutorConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/application/MyTaskExecutorConfiguration.java new file mode 100644 index 0000000000..dd165c3c61 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/application/MyTaskExecutorConfiguration.java @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.application; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.SimpleAsyncTaskExecutor; + +@Configuration(proxyBeanMethods = false) +public class MyTaskExecutorConfiguration { + + @Bean("applicationTaskExecutor") + SimpleAsyncTaskExecutor applicationTaskExecutor() { + return new SimpleAsyncTaskExecutor("app-"); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/async/MyTaskExecutorConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/async/MyTaskExecutorConfiguration.java new file mode 100644 index 0000000000..2e2962e382 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/async/MyTaskExecutorConfiguration.java @@ -0,0 +1,47 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.async; + +import java.util.concurrent.Executor; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.scheduling.annotation.AsyncConfigurer; + +@Configuration(proxyBeanMethods = false) +public class MyTaskExecutorConfiguration { + + @Bean + AsyncConfigurer asyncConfigurer(ExecutorService executorService) { + return new AsyncConfigurer() { + + @Override + public Executor getAsyncExecutor() { + return executorService; + } + + }; + } + + @Bean + ExecutorService executorService() { + return Executors.newCachedThreadPool(); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/builder/MyTaskExecutorConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/builder/MyTaskExecutorConfiguration.java new file mode 100644 index 0000000000..5198019b67 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/builder/MyTaskExecutorConfiguration.java @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.builder; + +import org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.SimpleAsyncTaskExecutor; + +@Configuration(proxyBeanMethods = false) +public class MyTaskExecutorConfiguration { + + @Bean + SimpleAsyncTaskExecutor taskExecutor(SimpleAsyncTaskExecutorBuilder builder) { + return builder.build(); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/defaultcandidate/MyTaskExecutorConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/defaultcandidate/MyTaskExecutorConfiguration.java new file mode 100644 index 0000000000..2db4db5cdb --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/defaultcandidate/MyTaskExecutorConfiguration.java @@ -0,0 +1,35 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.defaultcandidate; + +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; + +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration(proxyBeanMethods = false) +public class MyTaskExecutorConfiguration { + + @Bean(defaultCandidate = false) + @Qualifier("scheduledExecutorService") + ScheduledExecutorService scheduledExecutorService() { + return Executors.newSingleThreadScheduledExecutor(); + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/multiple/MyTaskExecutorConfiguration.java b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/multiple/MyTaskExecutorConfiguration.java new file mode 100644 index 0000000000..396cefc9f6 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/java/org/springframework/boot/docs/features/taskexecutionandscheduling/multiple/MyTaskExecutorConfiguration.java @@ -0,0 +1,39 @@ +/* + * Copyright 2012-2025 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.multiple; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.SimpleAsyncTaskExecutor; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +@Configuration(proxyBeanMethods = false) +public class MyTaskExecutorConfiguration { + + @Bean("applicationTaskExecutor") + SimpleAsyncTaskExecutor applicationTaskExecutor() { + return new SimpleAsyncTaskExecutor("app-"); + } + + @Bean("taskExecutor") + ThreadPoolTaskExecutor taskExecutor() { + ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor(); + threadPoolTaskExecutor.setThreadNamePrefix("async-"); + return threadPoolTaskExecutor; + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/application/MyTaskExecutorConfiguration.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/application/MyTaskExecutorConfiguration.kt new file mode 100644 index 0000000000..93db03f9bf --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/application/MyTaskExecutorConfiguration.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.application + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.core.task.SimpleAsyncTaskExecutor + +@Configuration(proxyBeanMethods = false) +class MyTaskExecutorConfiguration { + + @Bean("applicationTaskExecutor") + fun applicationTaskExecutor(): SimpleAsyncTaskExecutor { + return SimpleAsyncTaskExecutor("app-") + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/async/MyTaskExecutorConfiguration.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/async/MyTaskExecutorConfiguration.kt new file mode 100644 index 0000000000..81661b2af4 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/async/MyTaskExecutorConfiguration.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.async + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.scheduling.annotation.AsyncConfigurer +import java.util.concurrent.Executor +import java.util.concurrent.ExecutorService +import java.util.concurrent.Executors + +@Configuration(proxyBeanMethods = false) +class MyTaskExecutorConfiguration { + + @Bean + fun asyncConfigurer(executorService: ExecutorService): AsyncConfigurer { + return object : AsyncConfigurer { + override fun getAsyncExecutor(): Executor { + return executorService + } + } + } + + @Bean + fun executorService(): ExecutorService { + return Executors.newCachedThreadPool() + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/builder/MyTaskExecutorConfiguration.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/builder/MyTaskExecutorConfiguration.kt new file mode 100644 index 0000000000..448c14fe7d --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/builder/MyTaskExecutorConfiguration.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.builder + +import org.springframework.boot.task.SimpleAsyncTaskExecutorBuilder +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.core.task.SimpleAsyncTaskExecutor + +@Configuration(proxyBeanMethods = false) +class MyTaskExecutorConfiguration { + + @Bean + fun taskExecutor(builder: SimpleAsyncTaskExecutorBuilder): SimpleAsyncTaskExecutor { + return builder.build() + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/defaultcandidate/MyTaskExecutorConfiguration.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/defaultcandidate/MyTaskExecutorConfiguration.kt new file mode 100644 index 0000000000..32581a1fe0 --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/defaultcandidate/MyTaskExecutorConfiguration.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.defaultcandidate + +import org.springframework.beans.factory.annotation.Qualifier +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import java.util.concurrent.Executors +import java.util.concurrent.ScheduledExecutorService + +@Configuration(proxyBeanMethods = false) +class MyTaskExecutorConfiguration { + + @Bean(defaultCandidate = false) + @Qualifier("scheduledExecutorService") + fun scheduledExecutorService(): ScheduledExecutorService { + return Executors.newSingleThreadScheduledExecutor() + } + +} diff --git a/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/multiple/MyTaskExecutorConfiguration.kt b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/multiple/MyTaskExecutorConfiguration.kt new file mode 100644 index 0000000000..39458319ad --- /dev/null +++ b/spring-boot-project/spring-boot-docs/src/main/kotlin/org/springframework/boot/docs/features/taskexecutionandscheduling/multiple/MyTaskExecutorConfiguration.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2012-2023 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.docs.features.taskexecutionandscheduling.multiple + +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.core.task.SimpleAsyncTaskExecutor +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor + + +@Configuration(proxyBeanMethods = false) +class MyTaskExecutorConfiguration { + + @Bean("applicationTaskExecutor") + fun applicationTaskExecutor(): SimpleAsyncTaskExecutor { + return SimpleAsyncTaskExecutor("app-") + } + + @Bean("taskExecutor") + fun taskExecutor(): ThreadPoolTaskExecutor { + val threadPoolTaskExecutor = ThreadPoolTaskExecutor() + threadPoolTaskExecutor.threadNamePrefix = "async-" + return threadPoolTaskExecutor + } + +}