63 changed files with 1394 additions and 295 deletions
@ -0,0 +1,93 @@ |
|||||
|
package com.epmet.commons.tools.config; |
||||
|
|
||||
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
||||
|
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.Executor; |
||||
|
import java.util.concurrent.ExecutorService; |
||||
|
import java.util.concurrent.RejectedExecutionHandler; |
||||
|
import java.util.concurrent.ThreadPoolExecutor; |
||||
|
|
||||
|
/** |
||||
|
* 线程池配置类 |
||||
|
* thread: |
||||
|
* # 线程池配置 |
||||
|
* threadPool: |
||||
|
* enableCustomize: true [true会使用自定义线程池,false则使用springboot自动配置的线程池。推荐使用自定义线程池] |
||||
|
* 可以只配置此参数,其他参会会使用默认值,但还是建议把参数配置全。 |
||||
|
* corePoolSize: 2 |
||||
|
* maxPoolSize: 4 |
||||
|
* queueCapacity: 2 |
||||
|
* keepAliveSeconds: 60 |
||||
|
* threadNamePrefix: [线程池名字] |
||||
|
* rejectedExecutionHandler: [拒绝策略] |
||||
|
* |
||||
|
* 顺序:核心线程->放入队列->未达到maxPoolSize则继续增加线程直到达到maxPoolSize->拒绝策略 |
||||
|
* 开启自定义线程池:thread.threadPool.enableCustomize=true,不配置或者配置为false,自定义线程池都不会开启 |
||||
|
* rejectedExecutionHandler拒绝策略:abortPolicy/discardPolicy/discardOldestPolicy/callerRunsPolicy(默认) |
||||
|
*/ |
||||
|
@EnableConfigurationProperties(EpmetThreadPoolProperties.class) |
||||
|
@Configuration |
||||
|
@ConditionalOnProperty(prefix = "thread.threadPool", name = "enableCustomize", havingValue = "true", matchIfMissing = false) |
||||
|
public class AsyncConfig { |
||||
|
|
||||
|
/** |
||||
|
* 默认值 |
||||
|
*/ |
||||
|
private int corePoolSize = 5; |
||||
|
private int maxPoolSize = 8; |
||||
|
private int queueCapacity = 20; |
||||
|
private String threadNamePrefix = "epmet-default-"; |
||||
|
private int keepAliveSeconds = 60; |
||||
|
private String rejectedExecutionHandler = "callerRunsPolicy"; |
||||
|
|
||||
|
public AsyncConfig(EpmetThreadPoolProperties properties) { |
||||
|
if (properties.getCorePoolSize() != null) corePoolSize = properties.getCorePoolSize(); |
||||
|
if (properties.getMaxPoolSize() != null) maxPoolSize = properties.getMaxPoolSize(); |
||||
|
if (properties.getQueueCapacity() != null) queueCapacity = properties.getQueueCapacity(); |
||||
|
if (properties.getThreadNamePrefix() != null) threadNamePrefix = properties.getThreadNamePrefix(); |
||||
|
if (properties.getKeepAliveSeconds() != null) keepAliveSeconds = properties.getKeepAliveSeconds(); |
||||
|
if (properties.getRejectedExecutionHandler() != null) rejectedExecutionHandler = properties.getRejectedExecutionHandler(); |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public Executor executor() { |
||||
|
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
||||
|
executor.setCorePoolSize(corePoolSize); |
||||
|
executor.setMaxPoolSize(maxPoolSize); |
||||
|
executor.setQueueCapacity(queueCapacity); |
||||
|
executor.setThreadNamePrefix(threadNamePrefix); |
||||
|
executor.setRejectedExecutionHandler(getRejectedExecutionHandler(rejectedExecutionHandler)); //对拒绝task的处理策略
|
||||
|
executor.setKeepAliveSeconds(keepAliveSeconds); |
||||
|
executor.initialize(); |
||||
|
return executor; |
||||
|
} |
||||
|
|
||||
|
@Bean |
||||
|
public ExecutorService executorService() { |
||||
|
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) executor(); |
||||
|
return executor.getThreadPoolExecutor(); |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取拒绝策略handler |
||||
|
* @param policy |
||||
|
* @return |
||||
|
*/ |
||||
|
private RejectedExecutionHandler getRejectedExecutionHandler(String policy) { |
||||
|
switch (policy) { |
||||
|
case "abortPolicy": |
||||
|
return new ThreadPoolExecutor.AbortPolicy(); |
||||
|
case "discardPolicy": |
||||
|
return new ThreadPoolExecutor.DiscardPolicy(); |
||||
|
case "discardOldestPolicy": |
||||
|
return new ThreadPoolExecutor.DiscardOldestPolicy(); |
||||
|
default: |
||||
|
// 默认情况下,使用主线程执行
|
||||
|
return new ThreadPoolExecutor.CallerRunsPolicy(); |
||||
|
} |
||||
|
} |
||||
|
} |
@ -0,0 +1,47 @@ |
|||||
|
package com.epmet.commons.tools.config; |
||||
|
|
||||
|
import lombok.Data; |
||||
|
import org.springframework.boot.context.properties.ConfigurationProperties; |
||||
|
|
||||
|
/** |
||||
|
* 线程池配置参数 |
||||
|
* thread-pool会自动对应到threadPool |
||||
|
*/ |
||||
|
@ConfigurationProperties(prefix = "thread.thread-pool") |
||||
|
@Data |
||||
|
public class EpmetThreadPoolProperties { |
||||
|
|
||||
|
/** |
||||
|
* 是否允许自定义线程池 |
||||
|
*/ |
||||
|
private Boolean enableCustomize; |
||||
|
/** |
||||
|
* 核心线程数 |
||||
|
*/ |
||||
|
private Integer corePoolSize; |
||||
|
|
||||
|
/** |
||||
|
* 最大线程数 |
||||
|
*/ |
||||
|
private Integer maxPoolSize; |
||||
|
|
||||
|
/** |
||||
|
* 队列容量 |
||||
|
*/ |
||||
|
private Integer queueCapacity; |
||||
|
|
||||
|
/** |
||||
|
* 线程名前缀 |
||||
|
*/ |
||||
|
private String threadNamePrefix; |
||||
|
|
||||
|
/** |
||||
|
* 线程存活时长 |
||||
|
*/ |
||||
|
private Integer keepAliveSeconds; |
||||
|
|
||||
|
/** |
||||
|
* 拒绝策略 |
||||
|
*/ |
||||
|
private String rejectedExecutionHandler; |
||||
|
} |
@ -1,49 +0,0 @@ |
|||||
package com.epmet.config; |
|
||||
|
|
||||
import com.epmet.properties.ThreadProperties; |
|
||||
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.annotation.EnableAsync; |
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
|
||||
|
|
||||
import java.util.concurrent.Executor; |
|
||||
import java.util.concurrent.ExecutorService; |
|
||||
import java.util.concurrent.ThreadPoolExecutor; |
|
||||
|
|
||||
/** |
|
||||
* 线程池配置类 |
|
||||
*/ |
|
||||
@Configuration |
|
||||
@EnableConfigurationProperties(ThreadProperties.class) |
|
||||
@EnableAsync |
|
||||
public class AsyncConfig { |
|
||||
|
|
||||
@Autowired |
|
||||
private ThreadProperties threadProperties; |
|
||||
|
|
||||
@Bean |
|
||||
public Executor executor() { |
|
||||
ThreadProperties.ThreadPoolProperties threadPoolProps = threadProperties.getThreadPool(); |
|
||||
|
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
|
||||
executor.setCorePoolSize(threadPoolProps.getCorePoolSize()); |
|
||||
executor.setMaxPoolSize(threadPoolProps.getMaxPoolSize()); |
|
||||
executor.setQueueCapacity(threadPoolProps.getQueueCapacity()); |
|
||||
executor.setThreadNamePrefix("data-stats-"); |
|
||||
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
|
|
||||
// CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
|
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略
|
|
||||
executor.setKeepAliveSeconds(threadPoolProps.getKeepAlive()); |
|
||||
executor.initialize(); |
|
||||
return executor; |
|
||||
} |
|
||||
|
|
||||
@Bean |
|
||||
public ExecutorService executorService() { |
|
||||
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) executor(); |
|
||||
return executor.getThreadPoolExecutor(); |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -1,49 +0,0 @@ |
|||||
package com.epmet.config; |
|
||||
|
|
||||
import com.epmet.properties.ThreadProperties; |
|
||||
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.annotation.EnableAsync; |
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
|
||||
|
|
||||
import java.util.concurrent.Executor; |
|
||||
import java.util.concurrent.ExecutorService; |
|
||||
import java.util.concurrent.ThreadPoolExecutor; |
|
||||
|
|
||||
/** |
|
||||
* 线程池配置类 |
|
||||
*/ |
|
||||
@Configuration |
|
||||
@EnableConfigurationProperties(ThreadProperties.class) |
|
||||
@EnableAsync |
|
||||
public class AsyncConfig { |
|
||||
|
|
||||
@Autowired |
|
||||
private ThreadProperties threadProperties; |
|
||||
|
|
||||
@Bean |
|
||||
public Executor executor() { |
|
||||
ThreadProperties.ThreadPoolProperties threadPoolProps = threadProperties.getThreadPool(); |
|
||||
|
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
|
||||
executor.setCorePoolSize(threadPoolProps.getCorePoolSize()); |
|
||||
executor.setMaxPoolSize(threadPoolProps.getMaxPoolSize()); |
|
||||
executor.setQueueCapacity(threadPoolProps.getQueueCapacity()); |
|
||||
executor.setThreadNamePrefix("epmet-message-"); |
|
||||
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
|
|
||||
// CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
|
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略
|
|
||||
executor.setKeepAliveSeconds(threadPoolProps.getKeepAlive()); |
|
||||
executor.initialize(); |
|
||||
return executor; |
|
||||
} |
|
||||
|
|
||||
@Bean |
|
||||
public ExecutorService executorService() { |
|
||||
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) executor(); |
|
||||
return executor.getThreadPoolExecutor(); |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -1,49 +0,0 @@ |
|||||
package com.epmet.config; |
|
||||
|
|
||||
import com.epmet.properties.ThreadProperties; |
|
||||
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.annotation.EnableAsync; |
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
|
||||
|
|
||||
import java.util.concurrent.Executor; |
|
||||
import java.util.concurrent.ExecutorService; |
|
||||
import java.util.concurrent.ThreadPoolExecutor; |
|
||||
|
|
||||
/** |
|
||||
* 线程池配置类 |
|
||||
*/ |
|
||||
@Configuration |
|
||||
@EnableConfigurationProperties(ThreadProperties.class) |
|
||||
@EnableAsync |
|
||||
public class AsyncConfig { |
|
||||
|
|
||||
@Autowired |
|
||||
private ThreadProperties threadProperties; |
|
||||
|
|
||||
@Bean |
|
||||
public Executor executor() { |
|
||||
ThreadProperties.ThreadPoolProperties threadPoolProps = threadProperties.getThreadPool(); |
|
||||
|
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
|
||||
executor.setCorePoolSize(threadPoolProps.getCorePoolSize()); |
|
||||
executor.setMaxPoolSize(threadPoolProps.getMaxPoolSize()); |
|
||||
executor.setQueueCapacity(threadPoolProps.getQueueCapacity()); |
|
||||
executor.setThreadNamePrefix("epmet-point-"); |
|
||||
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
|
|
||||
// CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
|
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略
|
|
||||
executor.setKeepAliveSeconds(threadPoolProps.getKeepAlive()); |
|
||||
executor.initialize(); |
|
||||
return executor; |
|
||||
} |
|
||||
|
|
||||
@Bean |
|
||||
public ExecutorService executorService() { |
|
||||
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) executor(); |
|
||||
return executor.getThreadPoolExecutor(); |
|
||||
} |
|
||||
|
|
||||
} |
|
@ -1,49 +0,0 @@ |
|||||
package com.epmet.config; |
|
||||
|
|
||||
import com.epmet.properties.ThreadProperties; |
|
||||
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.annotation.EnableAsync; |
|
||||
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; |
|
||||
|
|
||||
import java.util.concurrent.Executor; |
|
||||
import java.util.concurrent.ExecutorService; |
|
||||
import java.util.concurrent.ThreadPoolExecutor; |
|
||||
|
|
||||
/** |
|
||||
* 线程池配置类 |
|
||||
*/ |
|
||||
@Configuration |
|
||||
@EnableConfigurationProperties(ThreadProperties.class) |
|
||||
@EnableAsync |
|
||||
public class AsyncConfig { |
|
||||
|
|
||||
@Autowired |
|
||||
private ThreadProperties threadProperties; |
|
||||
|
|
||||
@Bean |
|
||||
public Executor executor() { |
|
||||
ThreadProperties.ThreadPoolProperties threadPoolProps = threadProperties.getThreadPool(); |
|
||||
|
|
||||
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor(); |
|
||||
executor.setCorePoolSize(threadPoolProps.getCorePoolSize()); |
|
||||
executor.setMaxPoolSize(threadPoolProps.getMaxPoolSize()); |
|
||||
executor.setQueueCapacity(threadPoolProps.getQueueCapacity()); |
|
||||
executor.setThreadNamePrefix("epmet-resi-group-"); |
|
||||
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
|
|
||||
// CALLER_RUNS:不在新线程中执行任务,而是由调用者所在的线程来执行
|
|
||||
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy()); //对拒绝task的处理策略
|
|
||||
executor.setKeepAliveSeconds(threadPoolProps.getKeepAlive()); |
|
||||
executor.initialize(); |
|
||||
return executor; |
|
||||
} |
|
||||
|
|
||||
@Bean |
|
||||
public ExecutorService executorService() { |
|
||||
ThreadPoolTaskExecutor executor = (ThreadPoolTaskExecutor) executor(); |
|
||||
return executor.getThreadPoolExecutor(); |
|
||||
} |
|
||||
|
|
||||
} |
|
Loading…
Reference in new issue