I've got the following controller advice:
@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 the NotCachedException is thrown from a method annotated with @Async, the exception is not handled properly.
@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 config of the Executor:
@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 in order to make it work with @Async annotated methods?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…