项目架构—线程池配置
1.@EnableAsync开启异步,@Async 注解在service内的方法上,可以实现在controller的异步调用,调用的被@Async注解的方法会在一个单独线程内运行,适合即使返回,异步解耦,service慢慢去处理
2.@Async 注解的方法只能 返回void或者future类型的返回值,其他值会使 注解无效,因为不能异步执行
3.被@Async 的方法在独立线程调用,不能被@ControllerAdvice全局异常处理器捕获,所以需要自己设置异常处理
package com.example.demo.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import java.lang.reflect.Method;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
/**
*@author liuxingying
*@description 线程池配置
*@since 2022/5/17
*/
@Configuration
public class AsynConfig implements AsyncConfigurer {
private static Logger log = LoggerFactory.getLogger(AsynConfig.class);
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
// 设置核心线程数
executor.setCorePoolSize(10);
// 设置最大线程数
executor.setMaxPoolSize(100);
// 设置任务队列容量
executor.setQueueCapacity(500);
// 设置线程名称前缀
executor.setThreadNamePrefix("AsyncExecutor-");
// 设置拒绝策略
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
@Bean(name = "getQueeExcutor")
public Executor getQueeExcutor() {
log.info("getAsyncExecutor:{}", "hh");
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(360);
executor.setMaxPoolSize(360);
executor.setQueueCapacity(1000);
executor.setThreadNamePrefix(EvConstant.EV_ASYNC_EXCUTOR);
// executor.setRejectedExecutionHandler(ThreadPoolExecutor.DiscardOldestPolicy());
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new MyAsyncUncaughtExceptionHandler();
}
/**
*@author liuxingying
*@description 自定义未捕获异常的内部类 这里根据业务自定义
*@since 2022/5/17
*/
static class MyAsyncUncaughtExceptionHandler implements AsyncUncaughtExceptionHandler{
@Override
public void handleUncaughtException(Throwable ex, Method method, Object... objects) {
log.info("class#method: " + method.getDeclaringClass().getName() + "#" + method.getName());
log.info("type : " + ex.getClass().getName());
log.info("exception : " + ex.getMessage());
}
}
}
注意事项:
如果出现 object is not an instance of declaring class ,其中一种情况是项目引入了spring-cloud-starter-sleuth ,并且在实现 AsyncConfigurer接口的类中 构造自定义线程池 如上方的@Bean(name = “getQueeExcutor”) 时候,会报上面的错,需要将自定义线程池单独在其他配置类注入spring