StoreStore的介紹
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就都懂了。實現沒什麼說的。明天交上一個 測試代碼。