通过拦截器来统计每个action的执行时间

struts2中已经有此拦截器了,但是这个拦截器的配置太麻烦,还要配置是否开启和日志的级别。本人认为太复杂,没有必要。

统计每个action的执行时间,在测试开发的过程中需要用到。所以将此拦截器的代码简化,并将log4j的日志级别提高到info。一旦测试通过在实际的生产环境中就直接将此拦截器从配置文件中去掉即可。详细的java代码如下:

  1. package com.work.core.interceptor;
  2. /*
  3.  * Copyright (c) 2002-2006 by OpenSymphony
  4.  * All rights reserved.
  5.  */
  6. import org.apache.commons.logging.Log;
  7. import org.apache.commons.logging.LogFactory;
  8. import com.opensymphony.xwork2.ActionInvocation;
  9. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
  10. /**
  11.  * Timer简化版。
  12.  * @author wangmj
  13.  */
  14. public class TimerInterceptor extends AbstractInterceptor {
  15.     /**
  16.      * 
  17.      */
  18.     private static final long serialVersionUID = 6017311502566041661L;
  19.     private static final Log log = LogFactory.getLog(TimerInterceptor.class);
  20.     public String intercept(ActionInvocation invocation) throws Exception {
  21.         long startTime = System.currentTimeMillis();//计算开始日期
  22.         String result = invocation.invoke();
  23.         long executionTime = System.currentTimeMillis() - startTime;
  24.         StringBuffer message = new StringBuffer(100);
  25.         message.append("Executed action [");
  26.         String namespace = invocation.getProxy().getNamespace();
  27.         if ((namespace != null) && (namespace.trim().length() > 0)) {
  28.             message.append(namespace).append("/");
  29.         }
  30.         message.append(invocation.getProxy().getActionName());
  31.         message.append("!");
  32.         message.append(invocation.getProxy().getMethod());
  33.         message.append("] took ").append(executionTime).append(" ms.");
  34.         if (log.isInfoEnabled()) {
  35.             log.info(message.toString());
  36.         }
  37.         return result;
  38.     }
  39. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值