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

Struts2 内建拦截器:Timer使用

2014-02-22 16:56 260 查看
看了一下Timer这个自带的拦截器,可以用于记录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本身的执行耗时。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: