调用@AfterThrowing 处理程序方法后如何避免记录异常 [英] How to avoid logging exception after we have call to @AfterThrowing handler method

查看:35
本文介绍了调用@AfterThrowing 处理程序方法后如何避免记录异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须全局处理异常,所以我使用的是 Spring AOP @AfterThrowing.

I have to handle exception globally so I am using Spring AOP @AfterThrowing.

以下是代码

@Component
public class SomeJob {
@Handled
@Scheduled(fixedRate = 5000)
public void doSomething() {
    System.out.println("Inside doSomething");
    Integer a=null;
    a.byteValue();
}}

.

@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface Handled {}

异常处理

@Aspect
@Component
public class ExceptionHandlerAspect {
@Pointcut("@annotation(log.reduce.demo.aop.Handled)")
public void handledMethods() {
}

@AfterThrowing(pointcut = "handledMethods()", throwing = "ex")
public void handleTheException(Exception ex) {
    System.out.println("Inside handleTheException");
}}

以下是输出

Inside doSomething
Inside handleTheException
[2m2021-05-21 16:15:29.115[0;39m [31mERROR[0;39m [35m9044[0;39m [2m---[0;39m [2m[   scheduling-1][0;39m [36mo.s.s.s.TaskUtils$LoggingErrorHandler   [0;39m [2m:[0;39m Unexpected error occurred in scheduled task
java.lang.NullPointerException: null
at log.reduce.demo.aop.SomeJob.doSomething(SomeJob.java:13) ~[classes/:na]
at log.reduce.demo.aop.SomeJob$$FastClassBySpringCGLIB$$3f86fd70.invoke(<generated>) ~[classes/:na]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218) ~[spring-core-5.3.6.jar:5.3.6]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:779) ~[spring-aop-5.3.6.jar:5.3.6]
.
.
.
. java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_281]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_281]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_281]
at java.lang.Thread.run(Thread.java:748) [na:1.8.0_281]

如何避免在堆栈跟踪上方打印?当有任何异常时,我只想记录用户定义的文本

How to avoid printing above stacktrace? I just want to log user define text when there is any exception something like below only

Inside doSomething
Inside handleTheException

在 handleTheException 方法执行完成后打印堆栈跟踪.那么如何跳过/限制该日志记录?

Stacktrace is getting printed after handleTheException method execution completion. So how to skip/restrict that logging?

推荐答案

如您所见,@ÀfterThrowing 注释确实在引发异常时调用了您的 handleTheException 方法但是在抛出异常之后这样做(到那时捕获它为时已晚.

As you can see the @ÀfterThrowing annotation does indeed invoke your handleTheException method when the exception is thrown but does so after the exception has been thrown (by which time it is too late to catch it.

一个解决方案可能是使用 @Around 类似的东西:

On e solution could be to use @Around something like:

@Around(pointcut = "handledMethods()")
public void handleTheException(ProceedingJoinPoint pjp) {
    System.out.println("Inside handleTheException");
    try {
        pjp.proceed();
    }
    catch(Exception e) {
        // do something with e
    }

}}

这个方法会在 doSomething 开始之前进入,并使用 proceed() 方法在你的某个时间手动执行 doSomething()选择,但现在在 try...catch 块中,允许您捕获异常并自行处理.

This method will be entered before doSomething starts and uses the proceed()method to manually execute doSomething() at a time of your choosing, but now within a try...catch block allowing you to catch the exception and handle it yourself.

https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop-ataspectj-around-advice

这篇关于调用@AfterThrowing 处理程序方法后如何避免记录异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆