如何统计一个Bean中的方法调用次数
导读
「如何统计一个Bean中的方法调用次数」看似基础,实际藏着不少细节,下面按结论优先的方式展开。
核心解析
通过AOP即可实现,通过AOP对Bean进行代理,在每次执行方法前或者后进行几次计数统计。这个主要就是考虑好如何避免并发情况下不准,以及如何使用AOP实现代理。
主要的代码如下:
首先我们先自定义一个注解,有了这个注解之后,你可以在想要统计的方法上加上这个注解:
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author example
**/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MethodCallCount {
}
接下来定义一个切面,来对这个注解进行增强处理:
@Aspect
@Component
public class MethodCallCounterAspect {
private Map<String, AtomicInteger> methodCallCountMap = new ConcurrentHashMap<>();
@Around("@annotation(com.alice.chuang.java.bagu.MethodCallCount)")
public Object facade(ProceedingJoinPoint pjp) throws Exception {
Method method = ((MethodSignature) pjp.getSignature()).getMethod();
String methodName = method.getName();
try{
return pjp.proceed();
} catch (Throwable e) {
//异常处理
} finally{
//计数+1
AtomicInteger counter = methodCallCountMap.computeIfAbsent(methodName, k -> new AtomicInteger(0));
counter.incrementAndGet();
}
}
public int getMethodCallCount(String methodName) {
AtomicInteger counter = methodCallCountMap.get(methodName);
return (counter != null) ? counter.get() : 0;
}
}
有了以上注解和切面后,只需要在我们想要统计的方法上使用该注解就行了:
@Service
public class 示例作者TestServiceImpl implements 示例作者TestService {
@MethodCallCount
@Override
public 示例作者TestResponse test(示例作者TestRequest aliceTestRequest) {
}
以上,当test方法被调用时,就会自动统计调用次数。
但是这里有个容易忽略的点,这个统计结果只在内存中有效,如果应用发生重启,就会归零了。如果想要持久化保存,就需要考虑持久化存储了,如存在mysql或者redis中。
另外,如果并发特别高,对统计结果要求没那么精确, 可以用LongAdder替代AtomicInteger
要点小结
- 这个主要就是考虑好如何避免并发情况下不准,以及如何使用AOP实现代理。
- 但是这里有个容易忽略的点,这个统计结果只在内存中有效,如果应用发生重启,就会归零了。
由站内小编编辑整理