您的位置:首页 > 编程语言 > Java开发

spring mvc inteceptor 拦截器实现计算controller 的执行时间

2016-10-25 20:29 471 查看
最近在做一个程序的性能测试分析,UAT 压力测试感觉比较慢,但又没有系统的测试真正的瓶颈在什么地方,客户也不愿意花时间去专业的测试,就用chrome浏览器,看timeline 测试,很笨吧,没办法,客户就这么干,期间也用过jmeter测试,但也只是说结果不理想,也没给出具体的参数。一个真正好的完整的测试,应该包括从发起请求开始,DNS 解析,路由寻址时间,http server 响应时间,应用程序处理时间,包括数据库执行时间,以及IO 分析,当然网络带宽也要分析。由于我们是负责程序部分的,所以程序执行时间部分的操作日志由我们自己来负责分析,用到spring
mvc写的程序,所以理所当然的想到用spring inteceptor 来实现,具体看每个controller 的执行时间是多少,这中间包括了数据库部分。

什么是 spring inteceptor , SpringMVC 中的Interceptor 拦截请求是通过HandlerInterceptor 来实现的。在SpringMVC 中定义一个Interceptor 非常简单,主要有两种方式,第一种方式是要定义的Interceptor类要实现了Spring 的HandlerInterceptor 接口,或者是这个类继承实现了HandlerInterceptor 接口的类,比如Spring 已经提供的实现了HandlerInterceptor 接口的抽象类HandlerInterceptorAdapter
;第二种方式是实现Spring的WebRequestInterceptor接口,或者是继承实现了WebRequestInterceptor的类。

这里我采用第一种方式来实现,首先实现HandlerInterceptor 接口:


 程序代码

package com.inteceptor;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import org.springframework.web.servlet.HandlerInterceptor;  

public class TimeInteceptor implements HandlerInterceptor{

    private static final Logger logger = Logger.getLogger(TimeInteceptor.class);

    //before the actual handler will be executed

    public boolean preHandle(HttpServletRequest request, 

        HttpServletResponse response, Object handler)

        throws Exception {

        long startTime = System.currentTimeMillis();

        request.setAttribute("startTime", startTime);

        return true;

    }

    //after the handler is executed

    public void postHandle(

        HttpServletRequest request, HttpServletResponse response, 

        Object handler, ModelAndView modelAndView)

        throws Exception {

        long startTime = (Long)request.getAttribute("startTime");

        long endTime = System.currentTimeMillis();

        long executeTime = endTime - startTime;

        //modified the exisitng modelAndView

        modelAndView.addObject("executeTime",executeTime);

        //log it

        if(logger.isDebugEnabled()){

           logger.debug("[" + handler + "] executeTime : " + executeTime + "ms");

        }

    }

    public void afterCompletion(HttpServletRequest arg0,

            HttpServletResponse arg1, Object arg2, Exception arg3)

            throws Exception {

        // TODO Auto-generated method stub

        

    }

}

写了这个inteceptor 之后,怎么让它生效去拦截请求呢? 需要在spring mvc  的配置文件中去配置:


 程序代码

<?xml  version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="http://www.springframework.org/schema/beans   

        http://www.springframework.org/schema/beans/spring-beans.xsd  

        http://www.springframework.org/schema/context   

        http://www.springframework.org/schema/context/spring-context.xsd  

        http://www.springframework.org/schema/mvc   

        http://www.springframework.org/schema/mvc/spring-mvc.xsd"

    default-autowire="byName">

     

     <mvc:interceptors>

        <!-- 使用bean定义一个Interceptor,直接定义在mvc:interceptors根下面的Interceptor将拦截所有的请求 -->

        <bean class="com.inteceptor.AllInteceptor"/>

        <mvc:interceptor>

            <mvc:mapping path="/**"/>            

            <!-- 需排除拦截的地址 -->  

            <mvc:exclude-mapping path="/" />

            <mvc:exclude-mapping path="/test" />

            <!-- 定义在mvc:interceptor下面的表示是对特定的请求才进行拦截的 -->

            <bean class="com.inteceptor.TimeInteceptor"/>

        </mvc:interceptor>

    </mvc:interceptors>

     

</beans>

这样就能按配置的规则对URL 进行拦截,并打印出程序执行的时间。

除了用inteceptor做拦截来计算时间之外,还可以实现很多其他功能,比如定义时间在晚上23-24点之间服务器处于维护状态,转到维护界面等,都可以用这种方法实现,而且在  inteceptor 中还可以注入service 等查询数据库。很方便的。 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: