I have a configuration class as such:
@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;
}
}
And the ExceptionHandler here:
@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.(...)
(...)
}
}
}
The problem is that on uncaught exception (in method annotated with @Async
), it does not go through the method handleUncaughtException
, even though getAsyncUncaughtExceptionHandler()
returns the right bean.
Any idea?
UPDATE
I figured out that removing the autowiring in my class AsyncExceptionHandler (which is not what I want), it then enters into the method handleUncaughtException
on uncaught exception.
Why is that?