Spring @ExceptionHandler and multithreading

I have the following controller tip:

@ControllerAdvice
public class ExceptionHandlerAdvice {

    @ExceptionHandler(NotCachedException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public ModelAndView handleNotCachedException(NotCachedException ex) {
        LOGGER.warn("NotCachedException: ", ex);
        return generateModelViewError(ex.getMessage());
    }

}

It works great most of the time, but when a NotCachedException is thrown from a method annotated with @Async, the exception is not handled correctly.

@RequestMapping(path = "", method = RequestMethod.PUT)
@Async
public ResponseEntity<String> store(@Valid @RequestBody FeedbackRequest request, String clientSource) {
    cachingService.storeFeedback(request, ClientSource.from(clientSource));
    return new ResponseEntity<>(OK);
}

Here is the configuration of the Contractor:

@SpringBootApplication
@EnableAsync
public class Application {

private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);

    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
        SettingsConfig settings = context.getBean(SettingsConfig.class);
        LOGGER.info("{} ({}) started", settings.getArtifact(), settings.getVersion());
        createCachingIndex(cachingService);
    }

    @Bean(name = "matchingStoreExecutor")
    public Executor getAsyncExecutor() {
        int nbThreadPool = 5;
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(nbThreadPool);
        executor.setMaxPoolSize(nbThreadPool * 2);
        executor.setQueueCapacity(nbThreadPool * 10);
        executor.setThreadNamePrefix("matching-store-executor-");
        executor.initialize();
        return executor;
    }

}

What can I do to make it work with annotated @Async methods?

+6
source share
1 answer

The default exception handling engine does not work with @Async Enabled. To handle an exception thrown from methods annotated with @Async, you need to implement a custom AsyncExceptionHandler like.

public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler{
    @Override
    public void handleUncaughtException(Throwable ex, Method method, Object... params) {
        // Here goes your exception handling logic.

    }
}

customExceptionHandler Application

@EnableAsync
public class Application implements AsyncConfigurer {
     @Override Executor getAsyncExecutor(){
      // your ThreadPoolTaskExecutor configuration goes here. 
}


@Override
public AsyncUncaughExceptionHandler getAsyncUncaughtExceptionHandler(){
   return new AsyncExceptionHandler();
}

. , AsyncExceptionHandler AsyncConfigurer Application.

+5

All Articles