Add ThreadPoolTaskSchedulerBuilder and deprecate TaskSchedulerBuilder
Closes gh-36651
This commit is contained in:
@@ -24,6 +24,7 @@ import javax.sql.DataSource;
|
||||
import io.rsocket.transport.netty.server.TcpServerTransport;
|
||||
|
||||
import org.springframework.beans.factory.BeanFactory;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
|
||||
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
|
||||
@@ -43,6 +44,7 @@ import org.springframework.boot.context.properties.EnableConfigurationProperties
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
|
||||
import org.springframework.boot.task.TaskSchedulerBuilder;
|
||||
import org.springframework.boot.task.ThreadPoolTaskSchedulerBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -168,11 +170,18 @@ public class IntegrationAutoConfiguration {
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnBean(TaskSchedulerBuilder.class)
|
||||
@ConditionalOnMissingBean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
|
||||
@SuppressWarnings("removal")
|
||||
protected static class IntegrationTaskSchedulerConfiguration {
|
||||
|
||||
@Bean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
|
||||
public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
|
||||
return builder.build();
|
||||
public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder taskSchedulerBuilder,
|
||||
ObjectProvider<ThreadPoolTaskSchedulerBuilder> threadPoolTaskSchedulerBuilderProvider) {
|
||||
ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder = threadPoolTaskSchedulerBuilderProvider
|
||||
.getIfUnique();
|
||||
if (threadPoolTaskSchedulerBuilder != null) {
|
||||
return threadPoolTaskSchedulerBuilder.build();
|
||||
}
|
||||
return taskSchedulerBuilder.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -16,20 +16,14 @@
|
||||
|
||||
package org.springframework.boot.autoconfigure.task;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.LazyInitializationExcludeFilter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.task.TaskSchedulingProperties.Shutdown;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
import org.springframework.boot.task.TaskSchedulerBuilder;
|
||||
import org.springframework.boot.task.TaskSchedulerCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.config.TaskManagementConfigUtils;
|
||||
@@ -38,38 +32,21 @@ import org.springframework.scheduling.config.TaskManagementConfigUtils;
|
||||
* {@link EnableAutoConfiguration Auto-configuration} for {@link TaskScheduler}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Moritz Halbritter
|
||||
* @since 2.1.0
|
||||
*/
|
||||
@ConditionalOnClass(ThreadPoolTaskScheduler.class)
|
||||
@AutoConfiguration(after = TaskExecutionAutoConfiguration.class)
|
||||
@EnableConfigurationProperties(TaskSchedulingProperties.class)
|
||||
@Import({ TaskSchedulingConfigurations.ThreadPoolTaskSchedulerBuilderConfiguration.class,
|
||||
TaskSchedulingConfigurations.TaskSchedulerBuilderConfiguration.class,
|
||||
TaskSchedulingConfigurations.ThreadPoolTaskSchedulerConfiguration.class })
|
||||
public class TaskSchedulingAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
|
||||
@ConditionalOnMissingBean({ TaskScheduler.class, ScheduledExecutorService.class })
|
||||
public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
|
||||
public static LazyInitializationExcludeFilter scheduledBeanLazyInitializationExcludeFilter() {
|
||||
return new ScheduledBeanLazyInitializationExcludeFilter();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties,
|
||||
ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
|
||||
TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
|
||||
builder = builder.poolSize(properties.getPool().getSize());
|
||||
Shutdown shutdown = properties.getShutdown();
|
||||
builder = builder.awaitTermination(shutdown.isAwaitTermination());
|
||||
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
|
||||
builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
|
||||
builder = builder.customizers(taskSchedulerCustomizers);
|
||||
return builder;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
/*
|
||||
* 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.autoconfigure.task;
|
||||
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.task.TaskSchedulerBuilder;
|
||||
import org.springframework.boot.task.TaskSchedulerCustomizer;
|
||||
import org.springframework.boot.task.ThreadPoolTaskSchedulerBuilder;
|
||||
import org.springframework.boot.task.ThreadPoolTaskSchedulerCustomizer;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.config.TaskManagementConfigUtils;
|
||||
|
||||
/**
|
||||
* {@link TaskScheduler} configurations to be imported by
|
||||
* {@link TaskSchedulingAutoConfiguration} in a specific order.
|
||||
*
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
class TaskSchedulingConfigurations {
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
|
||||
@ConditionalOnMissingBean({ TaskScheduler.class, ScheduledExecutorService.class })
|
||||
@SuppressWarnings("removal")
|
||||
static class ThreadPoolTaskSchedulerConfiguration {
|
||||
|
||||
@Bean
|
||||
ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder taskSchedulerBuilder,
|
||||
ObjectProvider<ThreadPoolTaskSchedulerBuilder> threadPoolTaskSchedulerBuilderProvider) {
|
||||
ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder = threadPoolTaskSchedulerBuilderProvider
|
||||
.getIfUnique();
|
||||
if (threadPoolTaskSchedulerBuilder != null) {
|
||||
return threadPoolTaskSchedulerBuilder.build();
|
||||
}
|
||||
return taskSchedulerBuilder.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@SuppressWarnings("removal")
|
||||
static class TaskSchedulerBuilderConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean
|
||||
TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties,
|
||||
ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
|
||||
TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
|
||||
builder = builder.poolSize(properties.getPool().getSize());
|
||||
TaskSchedulingProperties.Shutdown shutdown = properties.getShutdown();
|
||||
builder = builder.awaitTermination(shutdown.isAwaitTermination());
|
||||
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
|
||||
builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
|
||||
builder = builder.customizers(taskSchedulerCustomizers);
|
||||
return builder;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@SuppressWarnings("removal")
|
||||
static class ThreadPoolTaskSchedulerBuilderConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnMissingBean({ TaskSchedulerBuilder.class, ThreadPoolTaskSchedulerBuilder.class })
|
||||
ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder(TaskSchedulingProperties properties,
|
||||
ObjectProvider<ThreadPoolTaskSchedulerCustomizer> threadPoolTaskSchedulerCustomizers,
|
||||
ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
|
||||
TaskSchedulingProperties.Shutdown shutdown = properties.getShutdown();
|
||||
ThreadPoolTaskSchedulerBuilder builder = new ThreadPoolTaskSchedulerBuilder();
|
||||
builder = builder.poolSize(properties.getPool().getSize());
|
||||
builder = builder.awaitTermination(shutdown.isAwaitTermination());
|
||||
builder = builder.awaitTerminationPeriod(shutdown.getAwaitTerminationPeriod());
|
||||
builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
|
||||
builder = builder.customizers(threadPoolTaskSchedulerCustomizers);
|
||||
// Apply the deprecated TaskSchedulerCustomizers, too
|
||||
builder = builder.additionalCustomizers(taskSchedulerCustomizers.orderedStream().map(this::adapt).toList());
|
||||
return builder;
|
||||
}
|
||||
|
||||
private ThreadPoolTaskSchedulerCustomizer adapt(TaskSchedulerCustomizer customizer) {
|
||||
return customizer::customize;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -31,7 +31,10 @@ import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.boot.LazyInitializationBeanFactoryPostProcessor;
|
||||
import org.springframework.boot.autoconfigure.AutoConfigurations;
|
||||
import org.springframework.boot.task.TaskSchedulerBuilder;
|
||||
import org.springframework.boot.task.TaskSchedulerCustomizer;
|
||||
import org.springframework.boot.task.ThreadPoolTaskSchedulerBuilder;
|
||||
import org.springframework.boot.task.ThreadPoolTaskSchedulerCustomizer;
|
||||
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -49,7 +52,9 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* Tests for {@link TaskSchedulingAutoConfiguration}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
* @author Moritz Halbritter
|
||||
*/
|
||||
@SuppressWarnings("removal")
|
||||
class TaskSchedulingAutoConfigurationTests {
|
||||
|
||||
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
|
||||
@@ -67,6 +72,26 @@ class TaskSchedulingAutoConfigurationTests {
|
||||
.run((context) -> assertThat(context).doesNotHaveBean(ScheduledBeanLazyInitializationExcludeFilter.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldSupplyBeans() {
|
||||
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class).run((context) -> {
|
||||
assertThat(context).hasSingleBean(TaskSchedulerBuilder.class);
|
||||
assertThat(context).hasSingleBean(ThreadPoolTaskSchedulerBuilder.class);
|
||||
assertThat(context).hasSingleBean(ThreadPoolTaskScheduler.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldNotSupplyThreadPoolTaskSchedulerBuilderIfCustomTaskSchedulerBuilderIsPresent() {
|
||||
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class)
|
||||
.withBean(TaskSchedulerBuilder.class, TaskSchedulerBuilder::new)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(TaskSchedulerBuilder.class);
|
||||
assertThat(context).doesNotHaveBean(ThreadPoolTaskSchedulerBuilder.class);
|
||||
assertThat(context).hasSingleBean(ThreadPoolTaskScheduler.class);
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableSchedulingWithNoTaskExecutorAutoConfiguresOne() {
|
||||
this.contextRunner
|
||||
@@ -86,7 +111,7 @@ class TaskSchedulingAutoConfigurationTests {
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableSchedulingWithNoTaskExecutorAppliesCustomizers() {
|
||||
void enableSchedulingWithNoTaskExecutorAppliesTaskSchedulerCustomizers() {
|
||||
this.contextRunner.withPropertyValues("spring.task.scheduling.thread-name-prefix=scheduling-test-")
|
||||
.withUserConfiguration(SchedulingConfiguration.class, TaskSchedulerCustomizerConfiguration.class)
|
||||
.run((context) -> {
|
||||
@@ -97,6 +122,18 @@ class TaskSchedulingAutoConfigurationTests {
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableSchedulingWithNoTaskExecutorAppliesCustomizers() {
|
||||
this.contextRunner.withPropertyValues("spring.task.scheduling.thread-name-prefix=scheduling-test-")
|
||||
.withUserConfiguration(SchedulingConfiguration.class, ThreadPoolTaskSchedulerCustomizerConfiguration.class)
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(TaskExecutor.class);
|
||||
TestBean bean = context.getBean(TestBean.class);
|
||||
assertThat(bean.latch.await(30, TimeUnit.SECONDS)).isTrue();
|
||||
assertThat(bean.threadNames).allMatch((name) -> name.contains("customized-scheduler-"));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void enableSchedulingWithExistingTaskSchedulerBacksOff() {
|
||||
this.contextRunner.withUserConfiguration(SchedulingConfiguration.class, TaskSchedulerConfiguration.class)
|
||||
@@ -175,6 +212,16 @@ class TaskSchedulingAutoConfigurationTests {
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class ThreadPoolTaskSchedulerCustomizerConfiguration {
|
||||
|
||||
@Bean
|
||||
ThreadPoolTaskSchedulerCustomizer testTaskSchedulerCustomizer() {
|
||||
return ((taskScheduler) -> taskScheduler.setThreadNamePrefix("customized-scheduler-"));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class SchedulingConfigurerConfiguration implements SchedulingConfigurer {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user