Announcement Announcement Module
Collapse
No announcement yet.
Appropriate place to track response times? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Appropriate place to track response times?

    If I wanted to track response times per request where would the correct place be to implement this? I was thinking an interceptor or an advice might work, but what would be the final "thing" to execute per request?

  • #2
    Hi, you can use

    an interceptor

    Code:
    public class MeasurementInterceptor extends HandlerInterceptorAdapter {
    	@Override
    	public void postHandle(HttpServletRequest request, HttpServletResponse response,
    			Object handler, ModelAndView modelAndView) throws Exception {
    		long startTime = (Long) request.getAttribute("startTime");
    		request.removeAttribute("startTime");
    		
    		long endTime = System.currentTimeMillis();
    		modelAndView.addObject("handlingTime", endTime - startTime);
    	}
    
    	@Override
    	public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
    			Object handler) throws Exception {
    		long startTime = System.currentTimeMillis();
    		request.setAttribute("startTime", startTime);
    		return true;
    	}
    }
    servlet.xml:

    HTML Code:
    	<bean
    		class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    		<property name="interceptors">
    			<list>
    				<ref bean="measurementInterceptor" />
    			</list>
    		</property>
    	</bean>
    .jsp

    HTML Code:
    <html>
    <head>
    <title>Response times</title>
    </head>
    <body>
    ${handlingTime} ms
    </body>
    </html>

    Comment

    Working...
    X