Reputation: 2418
I need to ensure that a particular method execution time is less than the specified value. I would like to do some thing like the following
@Around("execution(* com.foo.bar.executeTask2(..))" + " && someExternalParams(500)")
@Around("execution(* com.foo.bar.executeTask(..))" + " && someExternalParams(5)")
public void checkSLA(ProceedingJoinPoint joinPoint, int maxtime) throws Throwable {
//get staarttime
joinPoint.proceed();
// get endtime
if(endtime - starttime > maxtime)
System.out.println("Task took longertime");
}
How can I achieve this with Spring AOP. One solution is to read the maxtime from file. Any thoughts?
Upvotes: 2
Views: 177
Reputation: 12400
Hi you can make a @SLA
annotation and read that value from it in your aspect. You must obviously change advise to match that annotation and mark all your methods with @SLA
annotation.
It's little bit different from your approach but I think it's even better to make annotation for this kind of aspect. This way you don't have to update your pointcut every time you want handling new method. Just add annotation to this method. See example below.
Annotation:
@Target(METHOD)
@Retention(RUNTIME)
public @interface SLA {
int maxtime();
}
Aspect:
@Aspect
public SLAAscpet {
@Around("execution(@com.foo.bar.SLA * *(..))")
public void aroundSLAMethod(final JoinPoint joinPoint) {
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
SLA annotation = signature.getMethod().getAnnotation(SLA.class)
int maxtime = annotation.maxtime()
...
}
}
Usage:
...
@SLA(maxtime=500)
public void someMethodForSLA() {
...
}
...
Upvotes: 1
Reputation: 42849
Your aspect is Spring managed, so I would read from the properties right in my aspect bean.
@Aspect
public class MyAspect {
@Value("${my.system.property.for.max.time}")
private int maxTime;
@Around("execution(* com.foo.bar.executeTask(..))")
public void checkSLA(ProceedingJoinPoint joinPoint) throws Throwable {
//get staarttime
joinPoint.proceed();
// get endtime
if(endtime - starttime > maxTime)
System.out.println("Task took longertime");
}
}
}
Upvotes: 1