Пример перехватчика системного монитора.Он регистрирует каждый раз, когда запрос занимает более 2 секунд.
public class PerformanceMonitorHandlerInterceptor implements HandlerInterceptor {
private static Logger logger = Logger.getLogger( PerformanceMonitorHandlerInterceptor.class );
/** Default to not active and 2 seconds. */
private boolean active = true;
/** The threshold. */
private long threshold = 2000;
/**
* Starts the StopWatch to time request.
* @param handler the handler
* @param request the request
* @param response the response
* @return true, if pre handle
* @throws Exception the exception
* @see org.springframework.web.servlet.HandlerInterceptor#preHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
* java.lang.Object)
*/
@SuppressWarnings("unused")
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// Start Watch
if ( this.active ) {
String name = this.createRequestTraceName( request );
StopWatch stopWatch = new StopWatch( name );
stopWatch.start( name );
StopWatchHolder.setStopWatch( stopWatch );
}
// Continue with request
return true;
}
/**
* Warn if method takes longer than threshold milliseconds.
* @param handler the handler
* @param request the request
* @param modelAndView the model and view
* @param response the response
* @throws Exception the exception
* @see org.springframework.web.servlet.HandlerInterceptor#postHandle(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
* java.lang.Object, org.springframework.web.servlet.ModelAndView)
*/
@SuppressWarnings("unused")
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if ( this.active ) {
// Stop Watch
StopWatch stopWatch = StopWatchHolder.getStopWatch();
stopWatch.stop();
// Check threshold and log if over
if ( stopWatch.getLastTaskTimeMillis() >= this.threshold ) {
logger.warn( stopWatch.shortSummary() );
}
// Set ThreadLocal to null
StopWatchHolder.clear();
}
}
/**
* Not implemented.
* @param handler the handler
* @param exception the exception
* @param request the request
* @param response the response
* @throws Exception the exception
* @see org.springframework.web.servlet.HandlerInterceptor#afterCompletion(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse,
* java.lang.Object, java.lang.Exception)
*/
@SuppressWarnings("unused")
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception {
// not implemented
}
/**
* Creates the request trace name.
* @param request the request
* @return trace name
*/
protected String createRequestTraceName(HttpServletRequest request) {
StringBuilder sb = new StringBuilder();
sb.append( "url: [" );
sb.append( request.getRequestURL() );
sb.append( "]" );
sb.append( " query: [" );
sb.append( request.getQueryString() );
sb.append( "]" );
return sb.toString();
}
/**
* Sets the threshold.
* @param threshold The threshold to set.
*/
public void setThreshold(long threshold) {
this.threshold = threshold;
}
/**
* Sets the active.
* @param active The active to set.
*/
public void setActive(boolean active) {
this.active = active;
}
}
Затем подключите бин к отображению URL вашего бина:
<bean id="handlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping">
<property name="interceptors">
<list>
<ref bean="performanceMonitorHandlerInterceptor" />
</list>
</property>
</bean>