package com.foxconn.ipebg.report.component;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value = ElementType.METHOD)
@Retention(value = RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface TimeAnnotation {
String name() default "";
}
package com.foxconn.ipebg.report.component;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
/**
*
* @author H2017596
* @Description 统计方法执行时间
*
*/
@Component
@Aspect
public class TimeInterceptor {
private static Logger logger = Logger.getLogger(TimeInterceptor.class);
/**
* one minutes
*/
// private static final long ONE_MINUTES = 60 * 1000;
@Pointcut("@annotation(com.foxconn.ipebg.report.component.TimeAnnotation)")
private void timeAspect() {}
// public static final String POINT = "execution (* com.foxconn.ipebg.report.export.*.*(..))";
/**
* @Description: 统计方法执行的耗时Around环绕通知
* @param proceedingJoinPoint
* @return
*/
@Around("timeAspect()")
public Object timeAround(ProceedingJoinPoint proceedingJoinPoint) {
Object object = null;
Object[] args = proceedingJoinPoint.getArgs();
long startTime = System.currentTimeMillis();
try {
object = proceedingJoinPoint.proceed(args);
} catch (Throwable e) {
logger.error("统计某方法执行耗时环绕通知出错", e);
}
long endTime = System.currentTimeMillis();
MethodSignature signature = (MethodSignature) proceedingJoinPoint.getSignature();
String methodName = signature.getDeclaringTypeName() + "." + signature.getName();
this.printExecTime(methodName, startTime, endTime);
return object;
}
/**
* @param methodName
* @param startTime
* @param endTime
*/
private void printExecTime(String methodName, long startTime, long endTime) {
long diffTime = endTime - startTime;
// if (diffTime > ONE_MINUTES) {
logger.warn("-----" + methodName + " METHOD EXECUTION TIME:" + diffTime + " ms");
// }
}
}