001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.task;
018
019import java.util.concurrent.ScheduledExecutorService;
020
021import org.springframework.beans.factory.ObjectProvider;
022import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
023import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
024import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
025import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
026import org.springframework.boot.context.properties.EnableConfigurationProperties;
027import org.springframework.boot.task.TaskSchedulerBuilder;
028import org.springframework.boot.task.TaskSchedulerCustomizer;
029import org.springframework.context.annotation.Bean;
030import org.springframework.context.annotation.Configuration;
031import org.springframework.scheduling.TaskScheduler;
032import org.springframework.scheduling.annotation.SchedulingConfigurer;
033import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
034import org.springframework.scheduling.config.TaskManagementConfigUtils;
035
036/**
037 * {@link EnableAutoConfiguration Auto-configuration} for {@link TaskScheduler}.
038 *
039 * @author Stephane Nicoll
040 * @since 2.1.0
041 */
042@ConditionalOnClass(ThreadPoolTaskScheduler.class)
043@Configuration
044@EnableConfigurationProperties(TaskSchedulingProperties.class)
045public class TaskSchedulingAutoConfiguration {
046
047        @Bean
048        @ConditionalOnBean(name = TaskManagementConfigUtils.SCHEDULED_ANNOTATION_PROCESSOR_BEAN_NAME)
049        @ConditionalOnMissingBean({ SchedulingConfigurer.class, TaskScheduler.class,
050                        ScheduledExecutorService.class })
051        public ThreadPoolTaskScheduler taskScheduler(TaskSchedulerBuilder builder) {
052                return builder.build();
053        }
054
055        @Bean
056        @ConditionalOnMissingBean
057        public TaskSchedulerBuilder taskSchedulerBuilder(TaskSchedulingProperties properties,
058                        ObjectProvider<TaskSchedulerCustomizer> taskSchedulerCustomizers) {
059                TaskSchedulerBuilder builder = new TaskSchedulerBuilder();
060                builder = builder.poolSize(properties.getPool().getSize());
061                builder = builder.threadNamePrefix(properties.getThreadNamePrefix());
062                builder = builder.customizers(taskSchedulerCustomizers);
063                return builder;
064        }
065
066}