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.Executor; 020 021import org.springframework.beans.factory.ObjectProvider; 022import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 023import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; 024import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; 025import org.springframework.boot.context.properties.EnableConfigurationProperties; 026import org.springframework.boot.task.TaskExecutorBuilder; 027import org.springframework.boot.task.TaskExecutorCustomizer; 028import org.springframework.context.annotation.Bean; 029import org.springframework.context.annotation.Configuration; 030import org.springframework.context.annotation.Lazy; 031import org.springframework.core.task.TaskDecorator; 032import org.springframework.core.task.TaskExecutor; 033import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; 034 035/** 036 * {@link EnableAutoConfiguration Auto-configuration} for {@link TaskExecutor}. 037 * 038 * @author Stephane Nicoll 039 * @since 2.1.0 040 */ 041@ConditionalOnClass(ThreadPoolTaskExecutor.class) 042@Configuration 043@EnableConfigurationProperties(TaskExecutionProperties.class) 044public class TaskExecutionAutoConfiguration { 045 046 /** 047 * Bean name of the application {@link TaskExecutor}. 048 */ 049 public static final String APPLICATION_TASK_EXECUTOR_BEAN_NAME = "applicationTaskExecutor"; 050 051 private final TaskExecutionProperties properties; 052 053 private final ObjectProvider<TaskExecutorCustomizer> taskExecutorCustomizers; 054 055 private final ObjectProvider<TaskDecorator> taskDecorator; 056 057 public TaskExecutionAutoConfiguration(TaskExecutionProperties properties, 058 ObjectProvider<TaskExecutorCustomizer> taskExecutorCustomizers, 059 ObjectProvider<TaskDecorator> taskDecorator) { 060 this.properties = properties; 061 this.taskExecutorCustomizers = taskExecutorCustomizers; 062 this.taskDecorator = taskDecorator; 063 } 064 065 @Bean 066 @ConditionalOnMissingBean 067 public TaskExecutorBuilder taskExecutorBuilder() { 068 TaskExecutionProperties.Pool pool = this.properties.getPool(); 069 TaskExecutorBuilder builder = new TaskExecutorBuilder(); 070 builder = builder.queueCapacity(pool.getQueueCapacity()); 071 builder = builder.corePoolSize(pool.getCoreSize()); 072 builder = builder.maxPoolSize(pool.getMaxSize()); 073 builder = builder.allowCoreThreadTimeOut(pool.isAllowCoreThreadTimeout()); 074 builder = builder.keepAlive(pool.getKeepAlive()); 075 builder = builder.threadNamePrefix(this.properties.getThreadNamePrefix()); 076 builder = builder.customizers(this.taskExecutorCustomizers); 077 builder = builder.taskDecorator(this.taskDecorator.getIfUnique()); 078 return builder; 079 } 080 081 @Lazy 082 @Bean(name = APPLICATION_TASK_EXECUTOR_BEAN_NAME) 083 @ConditionalOnMissingBean(Executor.class) 084 public ThreadPoolTaskExecutor applicationTaskExecutor(TaskExecutorBuilder builder) { 085 return builder.build(); 086 } 087 088}