@Target({java.lang.annotation.ElementType.METHOD, java.lang.annotation.ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface ExecuteMonitor { public abstract long timeout(); } import org.springframework.aop.Pointcut; import org.springframework.aop.support.ComposablePointcut; import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; import org.springframework.beans.factory.FactoryBean; public class ExecutePointcut implements FactoryBean<Pointcut> { public Pointcut getObject() throws Exception { ComposablePointcut cp = new ComposablePointcut( AnnotationMatchingPointcut.forClassAnnotation(ExecuteMonitor.class)); cp.union( AnnotationMatchingPointcut.forMethodAnnotation(ExecuteMonitor.class)); return cp; } public Class<?> getObjectType() { return Pointcut.class; } public boolean isSingleton() { return true; } } import java.lang.reflect.Method; import org.aopalliance.intercept.MethodInvocation; import org.apache.commons.lang.SystemUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.aop.support.DelegatingIntroductionInterceptor; public class ExecuteInterceptor extends DelegatingIntroductionInterceptor { private static Logger logger = LoggerFactory.getLogger(ExecuteInterceptor.class); private long getTimeout(Method m) { ExecuteMonitor em = (ExecuteMonitor)m.getAnnotation(ExecuteMonitor.class); if (em == null) { em = (ExecuteMonitor)m.getDeclaringClass().getAnnotation(ExecuteMonitor.class); } if (em == null) { return -1L; } return em.timeout(); } private void printArguments(StringBuilder sb, Object[] args) { int i = 0; sb.append("{"); for (Object arg : args) { sb.append(i++).append("->"); sb.append(String.valueOf(arg)); sb.append(SystemUtils.LINE_SEPARATOR); } sb.append("}"); } public Object invoke(MethodInvocation mi) throws Throwable { logger.debug("invoke..." + mi.toString()); long timeout = getTimeout(mi.getMethod()); long l = 0L; if (timeout > 0L) l = System.currentTimeMillis(); try { long timeused; StringBuilder sb; return super.invoke(mi); } finally { if (l > 0L) { long timeused = System.currentTimeMillis() - l; if (timeused >= timeout) { StringBuilder sb = new StringBuilder(""); sb.append(mi.getMethod().toString()); sb.append(" executed in ").append(timeused) .append("ms (> ").append(timeout).append( "ms), Arguments :"); printArguments(sb, mi.getArguments()); logger.info(sb.toString()); sb = null; } } } } }
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"> <property name="proxyTargetClass" value="true"></property> <property name="advisorBeanNamePrefix" value="autoproxy"></property> </bean> <bean id="executeInterceptor" class="com.xxxxx.core.monitor.ExecuteInterceptor" /> <bean id="executePointcut" class="com.xxxxx.core.monitor.ExecutePointcut" /> <bean id="autoproxy.execute" class="org.springframework.aop.support.DefaultPointcutAdvisor"> <property name="pointcut" ref="executePointcut" /> <property name="advice" ref="executeInterceptor" /> </bean>