TimeAnnotation

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");
//		}
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值