Spring Async Uncaught Exception handler

后端 未结 4 1385
不思量自难忘°
不思量自难忘° 2020-12-09 09:08
@Override
@Async
public void asyncExceptionTest() {
    int i=1/0;
}

How can I log this using Spring Async framework without having to put try catc

4条回答
  •  -上瘾入骨i
    2020-12-09 09:36

    You can use standard Spring AOP approach

    @Aspect
    @Component
    @Slf4j
    public class AsyncHandler {
    
       @Around("@annotation(org.springframework.scheduling.annotation.Async)")
       private Object handle(ProceedingJoinPoint pjp) throws Throwable {
           try {
               Object retVal = pjp.proceed();
               return retVal;
           } catch (Throwable e) {
               log.error("in ASYNC, method: " + pjp.getSignature().toLongString() + ", args: " + AppStringUtils.transformToWellFormattedJsonString(pjp.getArgs()) + ", exception: "+ e, e);
               throw e;
           }
       }
    
    }
    

提交回复
热议问题