Created
April 29, 2016 03:39
-
-
Save andyzhuangyy/bba273397f41f3ca48a7fb365dc71d54 to your computer and use it in GitHub Desktop.
aop, use aspectj calculate count of method called
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Aspect | |
public final class MethodCounter { | |
private static final InternalLogger LOG = InternalLoggerFactory.getInstance(MethodCounter.class); | |
public MethodCounter() { | |
} | |
@Around( | |
// @checkstyle StringLiteralsConcatenation (2 lines) | |
"(execution(* *(..)) || initialization(*.new(..)))" | |
+ " && @annotation(com.yongche.admin2.annotation.AnnoCounter)" | |
) | |
public Object wrapMethod(final ProceedingJoinPoint point) throws Throwable { | |
final Method method = | |
MethodSignature.class.cast(point.getSignature()).getMethod(); | |
return this.wrap(point, method, method.getAnnotation(AnnoCounter.class)); | |
} | |
private Object wrap(final ProceedingJoinPoint point, final Method method, | |
final AnnoCounter annotation) throws Throwable { | |
Class<?> c = point.getClass(); | |
String name = annotation.name() + "_counter"; | |
Counter counter = MonitorHelper.getCounter(c, name); | |
try { | |
final Object result = point.proceed(); | |
return result; | |
// @checkstyle IllegalCatch (1 line) | |
} catch (final Throwable ex) { | |
LOG.debug("wrap method throw exception.", ex); | |
throw ex; | |
} finally { | |
counter.increment(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment