/**
* Cleans up a request of thread locals
*/
public void cleanupRequest(HttpServletRequest request) {
// 获取request域中计数器
Integer counterVal = (Integer) request.getAttribute(CLEANUP_RECURSION_COUNTER);
if (counterVal != null) {
// 这个计数器的值不为空就把它减一
counterVal -= 1;
// 重新放入request域,用CLEARUP_RECURSION_COUNTER这个常量保存
request.setAttribute(CLEANUP_RECURSION_COUNTER, counterVal);
// 关键:如果这个计数器在减一之后的值仍然大于0,那么就不释放它所占用的内存,记录一条日志就直接返回了
if (counterVal > 0 ) {
if (log.isDebugEnabled()) {
log.debug("skipping cleanup counter="+counterVal);
}
return;
}
}
// 否则就clearUp掉
// always clean up the thread request, even if an action hasn't been executed
try {
dispatcher.cleanUpRequest(request);
} finally {
ActionContext.setContext(null);
Dispatcher.setInstance(null);
}
}
相信看到这里,大家大致已经明白了这个计数器存在的意义:记录Action被请求的次数,如果请求的次数非常频繁,说明这个Action被调用的次数非常多,那么就暂时不释放掉它所占用的内存,反之,如果只请求了一次或者是几次,那么在这个Action执行完毕后就会释放掉它所占用的内存。
跟进源码看一下具体的实现:
public class ActionContext implements Serializable {
static ThreadLocal<ActionContext> actionContext = new ThreadLocal<ActionContext>();
// 处理请求的Action的name
public static final String ACTION_NAME = "com.opensymphony.xwork2.ActionContext.name";
// 与这个Action相关的值栈
public static final String VALUE_STACK = ValueStack.VALUE_STACK;
// session相关
public static final String SESSION = "com.opensymphony.xwork2.ActionContext.session";
// application域
public static final String APPLICATION = "com.opensymphony.xwork2.ActionContext.application";
...
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/jisuanjixue/article-78438-5.html
美国佬喜欢欺负小字辈