添加线程池设置

This commit is contained in:
wangkang
2017-07-29 10:18:34 +08:00
parent c4f45557d0
commit 91d0a39968
2 changed files with 233 additions and 0 deletions
@@ -47,6 +47,11 @@ public class ApplicationConfig {
@NotNull
private String dateFormat = "yyyy-MM-dd'T'HH:mm:ss:SSSZ";
/**
* 描述 : 线程池属性
*/
private TaskExecutePoolProperties taskExecutePoolProperties;
/**
* 描述 : 获取timeZone
*
@@ -119,4 +124,175 @@ public class ApplicationConfig {
this.outputExceptionStackTrace = outputExceptionStackTrace;
}
/**
* 描述 : 获取taskExecutePoolProperties
*
* @return the taskExecutePoolProperties
*/
public TaskExecutePoolProperties getTaskExecutePoolProperties() {
return taskExecutePoolProperties;
}
/**
* 描述 : 设置taskExecutePoolProperties
*
* @param taskExecutePoolProperties the taskExecutePoolProperties to set
*/
public void setTaskExecutePoolProperties(TaskExecutePoolProperties taskExecutePoolProperties) {
this.taskExecutePoolProperties = taskExecutePoolProperties;
}
/**
* 描述 : 线程池配置
*
* @author Administrator
*
*/
public static class TaskExecutePoolProperties {
/**
* 描述 : 常量
*/
private final Integer keepAliveSecondsValue = 60;
/**
* 描述 : 默认线程池大小(默认:1)
*/
private Integer corePoolSize = 1;
/**
* 描述 : 最大线程池大小(默认:Integer.MAX_VALUE)
*/
private Integer maxPoolSize = Integer.MAX_VALUE;
/**
* 描述 : 队列容量(默认:Integer.MAX_VALUE)
*/
private Integer queueCapacity = Integer.MAX_VALUE;
/**
* 描述 : 线程池存活时间
*/
private Integer keepAliveSeconds = keepAliveSecondsValue;
/**
* 描述 : 线程优先级
*/
private Integer threadPriority = Thread.NORM_PRIORITY;
/**
* 描述 : 线程名称
*/
private String threadNamePrefix = "defaultTaskAsyncPool";
/**
* 描述 : 获取corePoolSize
*
* @return the corePoolSize
*/
public Integer getCorePoolSize() {
return corePoolSize;
}
/**
* 描述 : 设置corePoolSize
*
* @param corePoolSize the corePoolSize to set
*/
public void setCorePoolSize(Integer corePoolSize) {
this.corePoolSize = corePoolSize;
}
/**
* 描述 : 获取maxPoolSize
*
* @return the maxPoolSize
*/
public Integer getMaxPoolSize() {
return maxPoolSize;
}
/**
* 描述 : 设置maxPoolSize
*
* @param maxPoolSize the maxPoolSize to set
*/
public void setMaxPoolSize(Integer maxPoolSize) {
this.maxPoolSize = maxPoolSize;
}
/**
* 描述 : 获取queueCapacity
*
* @return the queueCapacity
*/
public Integer getQueueCapacity() {
return queueCapacity;
}
/**
* 描述 : 设置queueCapacity
*
* @param queueCapacity the queueCapacity to set
*/
public void setQueueCapacity(Integer queueCapacity) {
this.queueCapacity = queueCapacity;
}
/**
* 描述 : 获取keepAliveSeconds
*
* @return the keepAliveSeconds
*/
public Integer getKeepAliveSeconds() {
return keepAliveSeconds;
}
/**
* 描述 : 设置keepAliveSeconds
*
* @param keepAliveSeconds the keepAliveSeconds to set
*/
public void setKeepAliveSeconds(Integer keepAliveSeconds) {
this.keepAliveSeconds = keepAliveSeconds;
}
/**
* 描述 : 获取threadPriority
*
* @return the threadPriority
*/
public Integer getThreadPriority() {
return threadPriority;
}
/**
* 描述 : 设置threadPriority
*
* @param threadPriority the threadPriority to set
*/
public void setThreadPriority(Integer threadPriority) {
this.threadPriority = threadPriority;
}
/**
* 描述 : 获取threadNamePrefix
*
* @return the threadNamePrefix
*/
public String getThreadNamePrefix() {
return threadNamePrefix;
}
/**
* 描述 : 设置threadNamePrefix
*
* @param threadNamePrefix the threadNamePrefix to set
*/
public void setThreadNamePrefix(String threadNamePrefix) {
this.threadNamePrefix = threadNamePrefix;
}
}
}
@@ -0,0 +1,57 @@
/**
* TaskExecutePool.java
* Created at 2017-05-03
* Created by Administrator
* Copyright (C) 2016 egridcloud.com, All rights reserved.
*/
package com.egridcloud.udf.core;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
/**
* 描述 : TaskExecutePool
*
* @author Administrator
*
*/
@Configuration
public class TaskExecutePoolConfig {
/**
* 描述 : applicationConfig
*/
@Autowired
private ApplicationConfig applicationConfig;
/**
* 描述 : 默认线程池
*
* @return Executor
*/
@Bean
public Executor defaultTaskAsyncPool() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//设置线程池
executor.setCorePoolSize(applicationConfig.getTaskExecutePoolProperties().getCorePoolSize());
executor.setMaxPoolSize(applicationConfig.getTaskExecutePoolProperties().getMaxPoolSize());
executor.setQueueCapacity(applicationConfig.getTaskExecutePoolProperties().getQueueCapacity());
executor.setKeepAliveSeconds(
applicationConfig.getTaskExecutePoolProperties().getKeepAliveSeconds());
executor
.setThreadPriority(applicationConfig.getTaskExecutePoolProperties().getThreadPriority());
executor.setThreadNamePrefix(
applicationConfig.getTaskExecutePoolProperties().getThreadNamePrefix());
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
}