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?
The problem arised from
@Autowired
private SynchronizationHelper synchronizationHelper;
The bean synchronizationHelper
was autowired with many beans etc., which somehow (I do not clearly understand how exactly) disabled the behavior of asyncExceptionHandler
.
I created a simple @Service
bean which only does what I need in my method handleUncaughtException
(which is updating the state of my task in DB), which I autowired the same way, and now everything works fine...
I do not time for extra investigations here, if somebody has any idea it would be welcome.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.