From 91d0a399682a77a2905c1fa249ac3cec37e99078 Mon Sep 17 00:00:00 2001 From: wangkang Date: Wed, 3 May 2017 10:31:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E7=BA=BF=E7=A8=8B=E6=B1=A0?= =?UTF-8?q?=E8=AE=BE=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../udf/core/ApplicationConfig.java | 176 ++++++++++++++++++ .../udf/core/TaskExecutePoolConfig.java | 57 ++++++ 2 files changed, 233 insertions(+) create mode 100644 udf-starter-core/src/main/java/com/egridcloud/udf/core/TaskExecutePoolConfig.java diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/ApplicationConfig.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/ApplicationConfig.java index e05e522..f47d94e 100644 --- a/udf-starter-core/src/main/java/com/egridcloud/udf/core/ApplicationConfig.java +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/ApplicationConfig.java @@ -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; + } + + } + } diff --git a/udf-starter-core/src/main/java/com/egridcloud/udf/core/TaskExecutePoolConfig.java b/udf-starter-core/src/main/java/com/egridcloud/udf/core/TaskExecutePoolConfig.java new file mode 100644 index 0000000..9b1341f --- /dev/null +++ b/udf-starter-core/src/main/java/com/egridcloud/udf/core/TaskExecutePoolConfig.java @@ -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; + } + +}