I have a service class with @Async method and If it's calling method throwing any exception then the @ControllerAdvice will not call for global exception handling. But for other classes and services it will call advice and sending email properly.
@Service
public class FileScanServiceImpl implements FileScanService {
@Override
@Async
public void scanFileScheduler() throws MQException {
try{
messageProducer.putFileNameToMQ(fileName);
} catch (Exception e) {
ExceptionUtility.handleException(e, currentFile);
}
}
The ExceptionUtility is used for checking instance on exception and doing some functionality there and throwing custom exception.
public static void handleException(Exception e throws MQException {
String errMsg = "";
if (e instanceof MQException) {
// some functionality
throw new MQException(subject, errMsg);
}
}
And this is my @ControlleAdvice
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MQException.class)
@ResponseBody
public void handleMQException(HttpServletRequest request, MQException ex) {
// send email
}
}
It there any solution for @Async which will call @ControllerAdvice for global exception, also the existing functionality will not break.