Spring MVC。有时http请求运行时间太长

时间:2014-06-25 10:57:01

标签: java spring http tomcat spring-mvc

我正在使用HandlerInterceptor来计算执行请求的时间。大多数http请求到控制器,只返回String,运行大约20ms,但2%的请求运行250-1000ms。服务器Tomcat 7.

拦截器代码

public class Interceptor implements HandlerInterceptor {
    private static final String START_TIME_OF_REQUEST = "startTimeOfRequest";

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        request.setAttribute(START_TIME_OF_REQUEST, System.currentTimeMillis());
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        Long timeFinish = System.currentTimeMillis();
        Long timeStart = (Long)request.getAttribute(START_TIME_OF_REQUEST);
        logTime(timeFinish - timeStart);
    }
}

控制器代码:

@Controller
public class TestController {

    @RequestMapping("/testRequest")
    public String testRequest() {
        return "{\"message\":\"ok\"}";
    }
}

请求时间(ms)的日志文件:

 [catalina-exec-152] INFO  - 12
 [catalina-exec-191] INFO  - 14
 [catalina-exec-193] INFO  - 42
 [catalina-exec-187] INFO  - 13
 [catalina-exec-200] INFO  - 14
 [catalina-exec-207] INFO  - 17
 [catalina-exec-151] INFO  - 13
 [catalina-exec-204] INFO  - 15
 [catalina-exec-208] INFO  - 72
 [catalina-exec-177] INFO  - 15
 [catalina-exec-186] INFO  - 460
 [catalina-exec-191] INFO  - 269
 [catalina-exec-158] INFO  - 16
 [catalina-exec-151] INFO  - 58
 [catalina-exec-170] INFO  - 21
 [catalina-exec-193] INFO  - 329

感谢您的帮助。

0 个答案:

没有答案