在业务开发中,如果需要异步处理,除了使用mq的方式外,还有一种就是借助线程池的方式。
为了统一大家使用线程池的使用,我们可以通过组件的方式,统一大家的使用习惯。比如线程池定义、线程池上下文处理等。
比如,场景的spring自带的@Async注解,就是基于线程池的方式处理的。
对此,我们需要对@Async的使用方式进行统一,以免造成错用。
1.线程池可配置
@Value("${frame.threadpool.corePoolSize:10}")
private int corePoolSize;
@Value("${frame.threadpool.maxPoolSize:20}")
private int maxPoolSize;
@Value("${frame.threadpool.queueCapacity:50}")
private int queueCapacity;
@Value("${frame.threadpool.idletime:300}")
private int idleTime;
其中配置中心,基于apollo或者nacos即可。
2.bean定义
@Bean
public Executor taskExecutor() {
log.info("创建框架定义异步线程池");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//mdc传递