那些年,我踩过的坑!**
1.Async注解*
Async使用时需要自定义线程池,不重写的话会使用默认的线程池,线程数量固定,可能会导致一些任务积压。
重写代码
@Component
public class MyAsyncConfigurer implements AsyncConfigurer {
private static final Logger LOGGER = LoggerFactory.getLogger(MyAsyncConfigurer.class);
@Override
public Executor getAsyncExecutor() {
// 定义一个最大为150个线程数量的线程池
ExecutorService service = Executors.newFixedThreadPool(150);
return service;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return new MyAsyncExceptionHandler();
}
class MyAsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... objects) {
LOGGER.error("method name is: {}." + method.getName());
LOGGER.error("exception message is {}." + throwable.getMessage());
}
}
}