Tomer
Tomer

Reputation: 2448

Handle exceptions using aop

I am using AOP with costume annotations, to add a timer to a method.

  @Around(value = "@annotation(benchmark)")
  public void func(final ProceedingJoinPoint joinPoint, final Benchmark benchmark) throws Throwable {
       //wrap the call to the method with timer
       final long t0 = System.currentTimeMillis();
       logger.error(t0 + " ");
       joinPoint.proceed();
       final long elapsed = (System.currentTimeMillis() - t0);
       logger.error(elapsed + " ");
  }

I want to be able to do something when exception is thrown from the annotated method. And I am not sure what is the right way...

I read and saw that there is :

@AfterThrowing(pointcut = "execution(* com.mycompany.package..* (..))", throwing = "ex")

As far as I understand @AfterThrowing doesn't give me what I want, I need somehow to cache exception only from method that are annotated with the benchmark annotation.

Any idea?

Thanks!

Upvotes: 0

Views: 477

Answers (2)

abishkar bhattarai
abishkar bhattarai

Reputation: 7641

<aop:config>
        <aop:aspect id="testInterceptor" ref="testid">
            <aop:pointcut expression="execution(* com.mycompany.package..* (..))"
                id="pointcutid" />

            <aop:after-throwing pointcut-ref="pointcutid"
                throwing="err" method="func" />


        </aop:aspect>
    </aop:config>


<bean id="testid" class="com.XXX.XXX.ClassNameHavingfuncMethod">  </bean>

When error is throwing from com.mycompany.package class methods then func method is called.

Upvotes: 0

hudi
hudi

Reputation: 16525

in excecution you can specify which annotation you want to catch for example:

@AfterThrowing(pointcut = "execution(@org.aspectj.lang.annotation.Around * com.mycompany.package..* (..))", throwing = "ex")

Upvotes: 2

Related Questions