tomcat study 第九章:session管理

第九章: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);


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值