spingboot异步线程池配置
- 启动类开始开启异步注解@EnableAsync ;这种扫描对应的是整个项目的包,也可以将这个注解放在某个具体的类,通过@ComponentScan 扫描具体的包路径
- 配置异步线程池
@Configuration
public class AsyncConfig implements AsyncConfigurer {
@Value("${core-pool-size}")
private int corePoolSize;
@Value("${max-pool-size}")
private int maxPoolSize;
@Value("${queue-capacity}")
private int queueCapacity;
@Override
@Bean("taskExecutor")
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor();
taskExecutor.setCorePoolSize(corePoolSize);
taskExecutor.setMaxPoolSize(maxPoolSize);
taskExecutor.setQueueCapacity(queueCapacity);
taskExecutor.initialize();
return taskExecutor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return null;
}
}
3.异步方法添加异步线程池引用
@Async("taskExecutor")
public void refreshPlayersCache(String activityId) {
}