服务端业务线程池优雅使用
/**
* 线程池配置
*
* @author chenjintian
* @date 2019/9/27
*/
@Configuration
@EnableAsync
@Slf4j
public class ThreadPoolConfig {
private static final String TRACE_ID = "traceId";
private CommonThreadPoolProperties commonThreadPoolProperties;
private RedisOperatorThreadPoolProperties redisOperatorThreadPoolProperties;
@Autowired
public ThreadPoolConfig(CommonThreadPoolProperties commonThreadPoolProperties, RedisOperatorThreadPoolProperties redisOperatorThreadPoolProperties) {
this.commonThreadPoolProperties = commonThreadPoolProperties;
this.redisOperatorThreadPoolProperties = redisOperatorThreadPoolProperties;
}
/**
* 公共线程池
*
* <p> 如果觉得不是很重要的服务可以统一用一个线程池
*/
@Bean(value = "commonThreadPoolTaskExecutor")
public ThreadPoolTaskExecutor commonThreadPoolTaskExecutor() {
return getThreadPoolTaskExecutor(commonThreadPoolProperties);
}
/**
* redis 操作线程池
*/
@Bean(value = "redisOperatorThreadPoolTaskExecutor")
public ThreadPoolTaskExecutor redisOperatorThreadPoolTaskExecutor() {
// todo 监听线程池使用的情况
return getThreadPoolTaskExecutor(redisOperatorThreadPoolProperties);
}
/**
* 根据线程池的属性值获取对应线程的设置
*
* @param baseThreadPoolProperties 线程池配置信息
* @return 线程池
*/
private ThreadPoolTaskExecutor getThreadPoolTaskExecutor(BaseThreadPoolProperties baseThreadPoolProperties) {
ThreadPoolTaskExecutor threadPoolTaskExecutor = new ThreadPoolTaskExecutor();
if (StringUtils.isNotBlank(baseThreadPoolProperties.getThreadGroupName())) {
threadPoolTaskExecutor.setThreadGroupName(baseThreadPoolProperties.getThreadGroupName());
}
if (StringUtils.isNotBlank(baseThreadPoolProperties.getThreadNamePrefix())) {
threadPoolTaskExecutor.setThreadNamePrefix(baseThreadPoolProperties.getThreadNamePrefix());
}
if (null != baseThreadPoolProperties.getCorePoolSize() && baseThreadPoolProperties.getCorePoolSize() > 0) {
threadPoolTaskExecutor.setCorePoolSize(baseThreadPoolProperties.getCorePoolSize());
}
if (null != baseThreadPoolProperties.getQueueCapacity() && baseThreadPoolProperties.getQueueCapacity() > 0) {
threadPoolTaskExecutor.setQueueCapacity(baseThreadPoolProperties.getQueueCapacity());
}
if (null != baseThreadPoolProperties.getMaxPoolSize() && baseThreadPoolProperties.getMaxPoolSize() > 0) {
threadPoolTaskExecutor.setMaxPoolSize(baseThreadPoolProperties.getMaxPoolSize());
}
if (null != baseThreadPoolProperties.getKeepAliveSeconds() && baseThreadPoolProperties.getKeepAliveSeconds() > 0) {
threadPoolTaskExecutor.setKeepAliveSeconds(baseThreadPoolProperties.getKeepAliveSeconds());
}
// 设置拒绝策略
if (StringUtils.isNotBlank(baseThreadPoolProperties.getRejectedPolicy())) {
ThreadPoolRejectedPolicyEnum threadPoolRejectedPolicyEnum = ThreadPoolRejectedPolicyEnum.toEnum(baseThreadPoolProperties.getRejectedPolicy());
if (null != threadPoolRejectedPolicyEnum) {
threadPoolTaskExecutor.setRejectedExecutionHandler(threadPoolRejectedPolicyEnum.getRejectedExecutionHandler());
} else {
log.warn("Not exist rejectedPolicy:{}, return threadPoolRejectedPolicyEnum null.", baseThreadPoolProperties.getRejectedPolicy());
}
}
// 设置MdcRunnable
threadPoolTaskExecutor.setTaskDecorator(new MdcTaskDecorator());
return threadPoolTaskExecutor;
}
/**
* 主线程traceId传入到子线程(注意:线程池池化)
*/
private static class MdcTaskDecorator implements TaskDecorator {
@Override
public Runnable decorate(Runnable runnable) {
String traceId = MDC.get(TRACE_ID);
return () -> {
try {
MDC.put(TRACE_ID, traceId);
runnable.run();
} finally {
MDC.remove(TRACE_ID);
}
};
}
}
}
上次更新: 2023/03/22, 15:21:20