我有一个这样的配置类:
@Configuration
@EnableAsync
@EnableScheduling
@EnableTransactionManagement
public class SpringAsyncConfiguration implements AsyncConfigurer {
@Autowired
private AppConfigProperties appConfigProperties;
@Autowired
private AsyncExceptionHandler asyncExceptionHandler;
@Bean("asyncExecutor")
@Override
public Executor getAsyncExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
executor.setCorePoolSize(appConfigProperties.getThreadpoolCorePoolSize());
executor.setMaxPoolSize(appConfigProperties.getThreadpoolMaxPoolSize());
executor.setQueueCapacity(appConfigProperties.getThreadpoolQueueCapacity());
executor.setThreadNamePrefix("threadPoolExecutor-");
executor.initialize();
return executor;
}
@Override
public AsyncUncaughtExceptionHandler getAsyncUncaughtExceptionHandler() {
return asyncExceptionHandler;
}
}
以及这里的 ExceptionHandler:
@Component
@Slf4j
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
@Autowired
private SynchronizationHelper synchronizationHelper;
@Override
public void handleUncaughtException(Throwable throwable, Method method, Object... obj) {
log.error("*** ASYNC Exception message - " + throwable);
if("synchronize".equals(method.getName())) {
synchronizationHelper.(...)
(...)
}
}
}
问题是,在未捕获的异常上(在用 注释的方法中) ,即使返回正确的 bean @Async
,它也不会通过方法。handleUncaughtException
getAsyncUncaughtExceptionHandler()
任何想法?
更新
我发现删除我的类 AsyncExceptionHandler 中的自动装配(这不是我想要的),然后它进入handleUncaughtException
未捕获异常的方法。
这是为什么?