Struts2性能调优拦截器

2014-11-24 08:49:43 ? 作者: ? 浏览: 1

当我们在工作中需要实现某些小需求时,不妨先进行下简单的调研,看看正在使用的开源框架是否已经具备了我们需要的功能,这样就不用重复发明轮子了。


下面以性能测试为例,看看如何调查Struts2框架是否具备这种功能。


1. struts-default.xml


因为Struts2的许多核心功能都是基于内部拦截器来实现的,所以我们首先要看看它是否有性能调优相关的拦截器。这就需要查看strut2-core-2.3.1.2.jar中的默认配置文件struts-default.xml了。


































Struts2像个百宝箱一样内置了很多拦截器,可以看到profiling很可能就是符合我们需求的拦截器,那现在就打开源码一探究竟。


2. ProfilingActivationInterceptor


org.apache.struts2.interceptor.ProfilingActivationInterceptor.java


public class ProfilingActivationInterceptor extendsAbstractInterceptor {

private String profilingKey = "profiling";
private boolean devMode;

@Inject(StrutsConstants.STRUTS_DEVMODE)
public void setDevMode(String mode) {
this.devMode = "true".equals(mode);
}

@Override
public String intercept(ActionInvocationinvocation) throws Exception {
if (devMode) {
Object val =invocation.getInvocationContext().getParameters().get(profilingKey);
if (val != null) {
String sval = (val instanceof String (String)val : ((String[])val)[0]);
boolean enable = "yes".equalsIgnoreCase(sval)|| "true".equalsIgnoreCase(sval);
UtilTimerStack.setActive(enable);
invocation.getInvocationContext().getParameters().remove(profilingKey);
}
}
return invocation.invoke();

}

}


从源码中可以看到,只要浏览器发过来的HTTP请求参数中包含profiling=true或者yes,性能拦截器就会开启Timer工具类,打印出Action的执行消耗时间。


-->

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: