Tomcat从零开始(十四)session的store

来源:互联网 发布:unity3d 语音聊天实现 编辑:程序博客网 时间:2024/05/18 01:58

Store

Store的介绍

上节课我们说了,session不仅可以被存在 内存的hashmap中,还可以被持久化到  file 和 database中,而store就是完成这个功能的,store的接口在org.apache.catalina.Store。下面我们来看看它的源码。
package org.apache.catalina; import java.beans.PropertyChangeListener; import java.io.IOException;  public interface Store {    public String getInfo();    public Manager getManager();    public void setManager(Manager manager);    public int getSize() throws IOException;    public void addPropertyChangeListener(PropertyChangeListener  listener);    public String[] keys() throws IOException;    public Session load(String id) throws ClassNotFoundException, IOException;    public void remove(String id) throws IOException;    public void clear() throws IOException;    pubiic void removePropertyChangeListener(PropertyChangeListener listener);    public void save(Session session) throws IOException; } 
我们看看其中的注释,是save和load的。我们就能大概了解两个方法的功能了,前者是用来将Session存储的,后者是从文件或者database中读取。
    /**     * Save the specified Session into this Store.  Any previously saved     * information for the associated session identifier is replaced.     *     * @param session Session to be saved     *     * @exception IOException if an input/output error occurs     */    public void save(Session session) throws IOException;    /**     * Load and return the Session associated with the specified session     * identifier from this Store, without removing it.  If there is no     * such stored Session, return <code>null</code>.     *     * @param id Session identifier of the session to load     *     * @exception ClassNotFoundException if a deserialization error occurs     * @exception IOException if an input/output error occurs     */    public Session load(String id)        throws ClassNotFoundException, IOException;
既然有Store接口,那么肯定有Base,StoreBase这个类(拥有两个子类FileStore,JDBCStore),没有实现save ,load这两个方法,因为这两个方法在不同的类中  实现的方法是不同的。那我们来看看StoreBase的代码,我们会发现,它实现了runnable接口,那么我们就看看他的run()方法。
    public void run() {        // Loop until the termination semaphore is set        while (!threadDone) {            threadSleep();            processExpires();        }    }    protected void processExpires() {        long timeNow = System.currentTimeMillis();        String[] keys = null;        if(!started)            return;        try {            keys = keys();        } catch (IOException e) {            log (e.toString());            e.printStackTrace();            return;        }        for (int i = 0; i < keys.length; i++) {            try {                StandardSession session = (StandardSession) load(keys[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) {                    if ( ( (PersistentManagerBase) manager).isLoaded( keys[i] )) {                        // recycle old backup session                        session.recycle();                    } else {                        // expire swapped out session                        session.expire();                    }                    remove(session.getId());                }            } catch (IOException e) {                log (e.toString());                e.printStackTrace();            } catch (ClassNotFoundException e) {                log (e.toString());                e.printStackTrace();            }        }    }

我们看到run方法,调用了processExpires方法,而根据读代码,我们能看到这个一个单独的线程,来定期检查超时的Session。

FileStore

这个就是存储到文件,session对象的标识符作为名字,以.session作为扩展名。这个我就不多说了,去tomcat官网去看API就妥了。它的save是使用 序列化机制讲session存储的。这对象序列化自己去看吧。

JDBCStore

这个同样 JDBC连接数据库 。  这个看看API就都懂了。实现没什么说的。


明天交上一个  测试代码。

原创粉丝点击