StopWatchAdvice extends AbstractServiceAdvice { @Around("serviceMethods()") public Object logMethodTime(ProceedingJoinPoint pjp) throws Throwable { Class<?> targetClass = pjp.getTarget().getClass(); MethodSignature targetMethodSignature = (MethodSignature) pjp.getSignature(); Method targetMethod = targetMethodSignature.getMethod(); if (targetMethod.getDeclaringClass().isInterface()) { targetMethod = pjp.getTarget().getClass().getMethod(pjp.getSignature().getName(), targetMethod.getParameterTypes()); } if ((AnnotationUtils.findAnnotation(targetClass, SuppressLogging.class) == null) && (AnnotationUtils.findAnnotation(targetMethod, SuppressLogging.class) == null) && (LOGGER.isInfoEnabled())) { StopWatch stopWatch = new StopWatch(); stopWatch.start(); Object returnValue = pjp.proceed(); long durationMilliseconds = stopWatch.getTime(); LOGGER.info("javaMethod=\"{}.{}\" javaMethodDurationTimeInMilliseconds={} javaMethodDurationTimeFormatted=\"{}\"", targetClass.getName(), targetMethodSignature.getName(), durationMilliseconds, HerdDateUtils.formatDuration(durationMilliseconds)); return returnValue; } else { return pjp.proceed(); } }  @Around("serviceMethods()") Object logMethodTime(ProceedingJoinPoint pjp);  }
@Test public void testLogMethodTime() throws Throwable { ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint(new StopWatchAdviceTest(), StopWatchAdviceTest.class.getDeclaredMethod("mockMethod")); stopWatchAdvice.logMethodTime(joinPoint); }
@Test public void testLogMethodTimeTargetMethodIsInterface() throws Throwable { ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint(new Command() { @Override public void execute() throws Exception { return; } }, Command.class.getDeclaredMethod("execute")); stopWatchAdvice.logMethodTime(joinPoint); }
@Test public void testLogMethodTimeTargetMethodTargetClassSuppressLogging() throws Throwable { ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint(new MockClassThatSuppressLogging(), MockClassThatSuppressLogging.class.getDeclaredMethod("mockMethod")); stopWatchAdvice.logMethodTime(joinPoint); }
@Test public void testLogMethodTimeTargetMethodTargetMethodSuppressLogging() throws Throwable { ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint(new StopWatchAdviceTest(), StopWatchAdviceTest.class.getDeclaredMethod("mockMethodThatSuppressLogging")); stopWatchAdvice.logMethodTime(joinPoint); }
@Test public void testLogMethodTimeWithInfoLoggingDisabled() throws Throwable { ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint(new StopWatchAdviceTest(), StopWatchAdviceTest.class.getDeclaredMethod("mockMethod")); LogLevel origLogLevel = getLogLevel("org.finra.herd.core.StopWatchAdvice"); setLogLevel("org.finra.herd.core.StopWatchAdvice", LogLevel.OFF); try { stopWatchAdvice.logMethodTime(joinPoint); } finally { setLogLevel("org.finra.herd.core.StopWatchAdvice", origLogLevel); } }
@Test public void testLogMethodTimeWithInfoLoggingEnabled() throws Throwable { ProceedingJoinPoint joinPoint = getMockedProceedingJoinPoint(new StopWatchAdviceTest(), StopWatchAdviceTest.class.getDeclaredMethod("mockMethod")); LogLevel origLogLevel = getLogLevel("org.finra.herd.core.StopWatchAdvice"); setLogLevel("org.finra.herd.core.StopWatchAdvice", LogLevel.INFO); try { stopWatchAdvice.logMethodTime(joinPoint); } finally { setLogLevel("org.finra.herd.core.StopWatchAdvice", origLogLevel); } }