Struts2 内建拦截器:Timer使用

看了一下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本身的执行耗时。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值