第九章:session管理
StandardContext 和StandardManager一一对应。
Context context = new StandardContext();
Manager manager = new StandardManager();
context.setManager(manager);
而每一个session中都必须有一个Manager:
public StandardSession(Manager manager) {//通过唯一的构造方法限制。
super();
this.manager = manager;
if (manager instanceof ManagerBase)
this.debug = ((ManagerBase) manager).getDebug();
}
Manager中存放一个HashMap用于存放session
=========================================================================
1.StandardManager是一个线程定时轮询判断session 是不是过期:
/**
* The background thread that checks for session timeouts and shutdown.
*/
public void run() {
// Loop until the termination semaphore is set
while (!threadDone) {
threadSleep();//轮询的间隔时间private int checkInterval = 60;
processExpires();
}
}
/**
* Invalidate all sessions that have expired.
*/
private void processExpires() {
long timeNow = System.currentTimeMillis();
Session sessions[] = findSessions();
for (int i = 0; i < sessions.length; i++) {
StandardSession session = (StandardSession) sessions[i];
if (!session.isValid())
continue;
int maxInactiveInterval = session.getMaxInactiveInterval();
if (maxInactiveInterval < 0)
continue;
int timeIdle = // Truncate, do not round up
(int) ((timeNow - session.getLastAccessedTime()) / 1000L);
if (timeIdle >= maxInactiveInterval) {
try {
session.expire();
} catch (Throwable t) {
log(sm.getString("standardManager.expireException"), t);
}
}
}
}
2.通过序列化存放在文件中:如下是对应的操作. /**
* Load any currently active sessions that were previously unloaded
* to the appropriate persistence mechanism, if any. If persistence is not
* supported, this method returns without doing anything.
*
* @exception ClassNotFoundException if a serialized class cannot be
* found during the reload
* @exception IOException if an input/output error occurs
*/
public void load() throws ClassNotFoundException, IOException {}
/**
* Save any currently active sessions in the appropriate persistence
* mechanism, if any. If persistence is not supported, this method
* returns without doing anything.
*
* @exception IOException if an input/output error occurs
*/
public void unload() throws IOException {
3.在StandardManager的父类ManagerBase中:
/**
* The set of currently active Sessions for this Manager, keyed by
* session identifier.存放活着的session
*/
protected HashMap sessions = new HashMap();
/**
* The set of previously recycled Sessions for this Manager.
*/存放死掉的session
protected ArrayList recycled = new ArrayList();
4.PersistentManagerBase 是ManagerBase的另一个子类。在学习:以下供参考(137):
在运行的时候,StandardManager将session对象存放在内存中。但是,当停止的时候,它将Session对象存放到文件中。当它再次启动的时候,重新载入Session对象。
PersistentManagerBase类作为一个管理器组件将Session对象存放到二级存储器中。它有两个直接子类:PersistentManager和DistributedManager类(DistributedManager)类只在Tomcat4中有。
5. Session在哪里被使用:
HttpRequestBase.getSession(boolean create){
doGetSession(create){
manager = context.getManager();
session = manager.findSession(requestedSessionId);
或
session = manager.createSession();
}
}
第一次进行:createSession()
第二次访问,如果浏览器有传递(好像是浏览器的本身的一种机制:会纪录上次response传来的sessionId,并在这次request一起发送给server)sessionId则findSession(sessionId);
tomcat study 第九章:session管理
最新推荐文章于 2024-11-02 20:00:45 发布