package com.zskj.dry.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.util.concurrent.ThreadPoolExecutor;
/**
* 线程池
*
* @author 花开
* @date 2017/11/29
*/
@Configuration
@EnableConfigurationProperties(TaskExecutorProperties.class)
public class TaskExecutorConfiguration {
@Autowired
private TaskExecutorProperties taskExecutorProperties;
@Bean
public ThreadPoolTaskExecutor taskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(taskExecutorProperties.getCorePoolSize());
executor.setMaxPoolSize(taskExecutorProperties.getMaxPoolSize());
executor.setQueueCapacity(taskExecutorProperties.getQueueCapacity());
executor.setThreadNamePrefix("taskExecutor-");
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.setKeepAliveSeconds(taskExecutorProperties.getKeepAliveSeconds());
executor.initialize();
return executor;
}
}
package com.zskj.dry.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
* 线程池配置
*
* @author 花开
* @date 2017/11/29
*/
@ConfigurationProperties(prefix = "spring.task.executor")
public class TaskExecutorProperties {
/** 线程池维护线程的最少数量 */
private int corePoolSize;
/** 线程池维护线程的最大数量 */
private int maxPoolSize;
/** 缓存队列 */
private int queueCapacity;
/** 允许的空闲时间 */
private int keepAliveSeconds;
public int getCorePoolSize() {
return corePoolSize;
}
public void setCorePoolSize(int corePoolSize) {
this.corePoolSize = corePoolSize;
}
public int getMaxPoolSize() {
return maxPoolSize;
}
public void setMaxPoolSize(int maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
public int getQueueCapacity() {
return queueCapacity;
}
public void setQueueCapacity(int queueCapacity) {
this.queueCapacity = queueCapacity;
}
public int getKeepAliveSeconds() {
return keepAliveSeconds;
}
public void setKeepAliveSeconds(int keepAliveSeconds) {
this.keepAliveSeconds = keepAliveSeconds;
}
}