001/* 002 * Copyright 2002-2017 the original author or authors. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * https://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.springframework.aop.interceptor; 018 019import org.aopalliance.intercept.MethodInvocation; 020import org.apache.commons.logging.Log; 021 022import org.springframework.util.StopWatch; 023 024/** 025 * Simple AOP Alliance {@code MethodInterceptor} for performance monitoring. 026 * This interceptor has no effect on the intercepted method call. 027 * 028 * <p>Uses a {@code StopWatch} for the actual performance measuring. 029 * 030 * @author Rod Johnson 031 * @author Dmitriy Kopylenko 032 * @author Rob Harrop 033 * @see org.springframework.util.StopWatch 034 * @see JamonPerformanceMonitorInterceptor 035 */ 036@SuppressWarnings("serial") 037public class PerformanceMonitorInterceptor extends AbstractMonitoringInterceptor { 038 039 /** 040 * Create a new PerformanceMonitorInterceptor with a static logger. 041 */ 042 public PerformanceMonitorInterceptor() { 043 } 044 045 /** 046 * Create a new PerformanceMonitorInterceptor with a dynamic or static logger, 047 * according to the given flag. 048 * @param useDynamicLogger whether to use a dynamic logger or a static logger 049 * @see #setUseDynamicLogger 050 */ 051 public PerformanceMonitorInterceptor(boolean useDynamicLogger) { 052 setUseDynamicLogger(useDynamicLogger); 053 } 054 055 056 @Override 057 protected Object invokeUnderTrace(MethodInvocation invocation, Log logger) throws Throwable { 058 String name = createInvocationTraceName(invocation); 059 StopWatch stopWatch = new StopWatch(name); 060 stopWatch.start(name); 061 try { 062 return invocation.proceed(); 063 } 064 finally { 065 stopWatch.stop(); 066 writeToLog(logger, stopWatch.shortSummary()); 067 } 068 } 069 070}