Struts2 内建拦截器:Timer使用
2014-02-22 16:56
260 查看
看了一下Timer这个自带的拦截器,可以用于记录Action的执行时间,感觉可以用来记日志。
记录每一个Action完整的耗时,便于后续性能分析消耗之类的事情,所以记一下这个东西怎么玩。
首先随便写一个action,为了便于看结果,写一个线程sleep的代码,看看时间记录结果:
然后在Struts.xml配一下这个action:
最后执行一下看看结果:
2014-2-22 16:53:14 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Executed action [//timerLog!sleepWhile] took 1063 ms.
2014-2-22 16:53:21 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Executed action [//timerLog!sleepWhile] took 1000 ms.
2014-2-22 16:53:25 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Executed action [//timerLog!sleepWhile] took 1000 ms.
可以看到这个结果和我们预期的是一样的,而且第一次会比1000ms多执行一点点时间,是初次加载使用action需要初始化耗时,后来的就完全是action本身的执行耗时。
记录每一个Action完整的耗时,便于后续性能分析消耗之类的事情,所以记一下这个东西怎么玩。
首先随便写一个action,为了便于看结果,写一个线程sleep的代码,看看时间记录结果:
public class TimerInterceptor extends ActionSupport { public String sleepWhile() throws InterruptedException { Thread.sleep(1000); return SUCCESS; } }
然后在Struts.xml配一下这个action:
<action name="timerLog" class="com.kewen.TimerInterceptor" method="sleepWhile"> <result name="success">/index.jsp</result> <interceptor-ref name="timer"/> </action>
最后执行一下看看结果:
2014-2-22 16:53:14 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Executed action [//timerLog!sleepWhile] took 1063 ms.
2014-2-22 16:53:21 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Executed action [//timerLog!sleepWhile] took 1000 ms.
2014-2-22 16:53:25 com.opensymphony.xwork2.util.logging.commons.CommonsLogger info
信息: Executed action [//timerLog!sleepWhile] took 1000 ms.
可以看到这个结果和我们预期的是一样的,而且第一次会比1000ms多执行一点点时间,是初次加载使用action需要初始化耗时,后来的就完全是action本身的执行耗时。
相关文章推荐
- Struts2-使用myeclipse时,控制台不输出timer拦截器中的信息的解决方案
- Struts2 使用拦截器后参数无法传递问题的解决
- Struts2拦截器的使用
- struts2使用fileUpload拦截器实现文件上传
- Struts2拦截器的使用 (详解)(转)
- Struts2使用15 -- 拦截器5 -- 实例
- Struts2中为什么要使用拦截器?拦截器的使用原理以及如何配置拦截器。
- Struts2之拦截器原理分析及使用-下
- struts2拦截器使用(用户session失效后的统一页面指向)
- struts2拦截器的使用(二)
- struts2_拦截器的使用
- Struts2使用拦截器完成权限控制示例
- struts2拦截器的使用
- 在struts2中使用拦截器(Interceptor)控制登录和权限
- Struts2拦截器的使用 (详解)
- Struts2拦截器的使用 (详解)
- struts2中拦截器使用的代码实例
- Struts2 使用拦截器限制上传文件类型
- Struts2拦截器的使用
- Struts2的拦截器使用