mirror of
https://github.com/spring-projects/spring-boot.git
synced 2026-10-01 14:59:50 +00:00
Create reusable default TaskScheduler configuration
Create a `DefaultTaskSchedulerConfiguration` for use when standard task scheduler beans are required. Closes gh-49280
This commit is contained in:
+68
@@ -0,0 +1,68 @@
|
||||
/*
|
||||
* Copyright 2012-present 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 org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnThreading;
|
||||
import org.springframework.boot.task.SimpleAsyncTaskSchedulerBuilder;
|
||||
import org.springframework.boot.task.ThreadPoolTaskSchedulerBuilder;
|
||||
import org.springframework.boot.thread.Threading;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.SimpleAsyncTaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.config.TaskSchedulerRouter;
|
||||
|
||||
/**
|
||||
* Configuration that can be imported to expose a standard {@link TaskScheduler} if the
|
||||
* user has not enabled task scheduling explicitly. A {@link SimpleAsyncTaskScheduler} is
|
||||
* exposed if the user enables virtual threads via
|
||||
* {@code spring.threads.virtual.enabled=true}, otherwise {@link ThreadPoolTaskScheduler}.
|
||||
* <p>
|
||||
* Configurations importing this one should be ordered after
|
||||
* {@link TaskSchedulingAutoConfiguration}.
|
||||
*
|
||||
* @author Phillip Webb
|
||||
* @since 4.1.0
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnMissingBean(name = DefaultTaskSchedulerConfiguration.DEFAULT_TASK_SCHEDULER_BEAN_NAME)
|
||||
public class DefaultTaskSchedulerConfiguration {
|
||||
|
||||
/**
|
||||
* The bean name of the default task scheduler.
|
||||
*/
|
||||
public static final String DEFAULT_TASK_SCHEDULER_BEAN_NAME = TaskSchedulerRouter.DEFAULT_TASK_SCHEDULER_BEAN_NAME;
|
||||
|
||||
@Bean(name = DEFAULT_TASK_SCHEDULER_BEAN_NAME)
|
||||
@ConditionalOnBean(ThreadPoolTaskSchedulerBuilder.class)
|
||||
@ConditionalOnThreading(Threading.PLATFORM)
|
||||
ThreadPoolTaskScheduler taskScheduler(ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder) {
|
||||
return threadPoolTaskSchedulerBuilder.build();
|
||||
}
|
||||
|
||||
@Bean(name = DEFAULT_TASK_SCHEDULER_BEAN_NAME)
|
||||
@ConditionalOnBean(SimpleAsyncTaskSchedulerBuilder.class)
|
||||
@ConditionalOnThreading(Threading.VIRTUAL)
|
||||
SimpleAsyncTaskScheduler taskSchedulerVirtualThreads(
|
||||
SimpleAsyncTaskSchedulerBuilder simpleAsyncTaskSchedulerBuilder) {
|
||||
return simpleAsyncTaskSchedulerBuilder.build();
|
||||
}
|
||||
|
||||
}
|
||||
+2
-14
@@ -32,11 +32,10 @@ import org.springframework.boot.task.ThreadPoolTaskSchedulerCustomizer;
|
||||
import org.springframework.boot.thread.Threading;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Import;
|
||||
import org.springframework.core.task.TaskDecorator;
|
||||
import org.springframework.core.task.support.CompositeTaskDecorator;
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.SimpleAsyncTaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.config.TaskManagementConfigUtils;
|
||||
|
||||
/**
|
||||
@@ -58,20 +57,9 @@ class TaskSchedulingConfigurations {
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
|
||||
@ConditionalOnMissingBean({ TaskScheduler.class, ScheduledExecutorService.class })
|
||||
@Import(DefaultTaskSchedulerConfiguration.class)
|
||||
static class TaskSchedulerConfiguration {
|
||||
|
||||
@Bean(name = "taskScheduler")
|
||||
@ConditionalOnThreading(Threading.VIRTUAL)
|
||||
SimpleAsyncTaskScheduler taskSchedulerVirtualThreads(SimpleAsyncTaskSchedulerBuilder builder) {
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
@ConditionalOnThreading(Threading.PLATFORM)
|
||||
ThreadPoolTaskScheduler taskScheduler(ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder) {
|
||||
return threadPoolTaskSchedulerBuilder.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
|
||||
+2
-34
@@ -33,19 +33,16 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnSingleCandidate;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnThreading;
|
||||
import org.springframework.boot.autoconfigure.condition.SearchStrategy;
|
||||
import org.springframework.boot.autoconfigure.jmx.JmxAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.jmx.JmxProperties;
|
||||
import org.springframework.boot.autoconfigure.task.DefaultTaskSchedulerConfiguration;
|
||||
import org.springframework.boot.autoconfigure.task.TaskSchedulingAutoConfiguration;
|
||||
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.jdbc.init.DataSourceScriptDatabaseInitializer;
|
||||
import org.springframework.boot.sql.autoconfigure.init.OnDatabaseInitializationCondition;
|
||||
import org.springframework.boot.task.SimpleAsyncTaskSchedulerBuilder;
|
||||
import org.springframework.boot.task.ThreadPoolTaskSchedulerBuilder;
|
||||
import org.springframework.boot.thread.Threading;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Conditional;
|
||||
@@ -69,8 +66,6 @@ import org.springframework.messaging.rsocket.RSocketRequester;
|
||||
import org.springframework.messaging.rsocket.RSocketStrategies;
|
||||
import org.springframework.messaging.rsocket.annotation.support.RSocketMessageHandler;
|
||||
import org.springframework.scheduling.Trigger;
|
||||
import org.springframework.scheduling.concurrent.SimpleAsyncTaskScheduler;
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
|
||||
import org.springframework.scheduling.support.CronTrigger;
|
||||
import org.springframework.scheduling.support.PeriodicTrigger;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -93,6 +88,7 @@ import org.springframework.util.StringUtils;
|
||||
afterName = "org.springframework.boot.jdbc.autoconfigure.DataSourceAutoConfiguration")
|
||||
@ConditionalOnClass(EnableIntegration.class)
|
||||
@EnableConfigurationProperties({ IntegrationProperties.class, JmxProperties.class })
|
||||
@Import(DefaultTaskSchedulerConfiguration.class)
|
||||
public final class IntegrationAutoConfiguration {
|
||||
|
||||
@Bean(name = IntegrationContextUtils.INTEGRATION_GLOBAL_PROPERTIES_BEAN_NAME)
|
||||
@@ -174,34 +170,6 @@ public final class IntegrationAutoConfiguration {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Expose a standard {@link org.springframework.scheduling.TaskScheduler
|
||||
* TaskScheduler} if the user has not enabled task scheduling explicitly. A
|
||||
* {@link SimpleAsyncTaskScheduler} is exposed if the user enables virtual threads via
|
||||
* {@code spring.threads.virtual.enabled=true}, otherwise
|
||||
* {@link ThreadPoolTaskScheduler}.
|
||||
*/
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
@ConditionalOnMissingBean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
|
||||
protected static class IntegrationTaskSchedulerConfiguration {
|
||||
|
||||
@Bean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
|
||||
@ConditionalOnBean(ThreadPoolTaskSchedulerBuilder.class)
|
||||
@ConditionalOnThreading(Threading.PLATFORM)
|
||||
ThreadPoolTaskScheduler taskScheduler(ThreadPoolTaskSchedulerBuilder threadPoolTaskSchedulerBuilder) {
|
||||
return threadPoolTaskSchedulerBuilder.build();
|
||||
}
|
||||
|
||||
@Bean(name = IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)
|
||||
@ConditionalOnBean(SimpleAsyncTaskSchedulerBuilder.class)
|
||||
@ConditionalOnThreading(Threading.VIRTUAL)
|
||||
SimpleAsyncTaskScheduler taskSchedulerVirtualThreads(
|
||||
SimpleAsyncTaskSchedulerBuilder simpleAsyncTaskSchedulerBuilder) {
|
||||
return simpleAsyncTaskSchedulerBuilder.build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Spring Integration JMX configuration.
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user