tomcat(9)Session管理
生活随笔
收集整理的這篇文章主要介紹了
tomcat(9)Session管理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
【0】README
0.0)本文部分描述轉自“深入剖析tomcat”,旨在學習“tomcat-Session管理” 的基礎知識;
0.1)Catalina通過一個稱為Session 管理器的組件來管理建立的Session對象,該組件由org.apache.catalina.Manager接口來表示。(干貨——catalina通過Session管理器組件來管理Session對象)
0.2)Session管理器:需要與一個Context容器相關聯,且必須與一個Context容器關聯;(干貨——intro to Session Manager)
0.3)Session管理器:負責創建,更新,銷毀Session對象,當有請求到來時,要會返回一個有效的Session對象;
0.4)default case:Session管理器會將其所管理的 Session對象存放在內存中。但在tomcat中,Session管理器也可以將Session 對象進行持久化,存儲到文件存儲器或通過jdbc寫入到database;
0.5)for complete source code, please visit ?https://github.com/pacosonTang/HowTomcatWorks/tree/master/chapter9;
【1】Session對象 1)intro to Session object:Session對象由 javax.servlet.http.HttpSession接口表示; 2)UML類圖實例:
【1.1】Session接口 1)Session接口是作為 Catalina內部的外觀類使用的。Session 接口的標準實現 StandardSession類也實現了 javax.servlet.http.HttpSession 接口; public interface Session { public static final String SESSION_CREATED_EVENT = "createSession"; public static final String SESSION_DESTROYED_EVENT = "destroySession"; public String getAuthType(); public void setAuthType(String authType); public long getCreationTime(); public void setCreationTime(long time); public String getId(); public void setId(String id); public String getInfo(); public long getLastAccessedTime(); public Manager getManager(); public void setManager(Manager manager); public int getMaxInactiveInterval(); public void setMaxInactiveInterval(int interval); public void setNew(boolean isNew); public Principal getPrincipal(); public void setPrincipal(Principal principal); public HttpSession getSession(); public void setValid(boolean isValid); public boolean isValid(); public void access(); public void addSessionListener(SessionListener listener); public void expire(); public Object getNote(String name); public Iterator getNoteNames(); public void recycle();public void removeNote(String name);public void removeSessionListener(SessionListener listener);public void setNote(String name, Object value); } 2)Session對象總是存在于Session管理器中的:可以使用 getManager()方法或 setManager() 方法將Session實例和某個 Session管理器相關聯; 2.1)對某個Session實例來說:在與其Session 管理器相關聯的某個Context實例內,該Session對象有一個唯一的標識符,可以通過 setId() 方法和 getId() 方法來訪問該 Session 標識符; 2.2)Session管理器會調用 getLastAccessedTime()方法:根據返回值來判斷一個Session對象的有效性;(Session管理器會調用setValid()方法來設置或重置其有效性); 2.3)每當訪問一個Session實例時: 會調用其 access()方法 來修改 Session對象的最后訪問時間; 2.4)最后:Session管理器會調用Session對象的 expire()方法將其設置為過期, 也可以通過getSession() 方法獲取一個經過 Session 外觀類包裝的 HttpSession 對象;
【1.2】StandardSession類 1)StandardSession類實現了 java.lang.Serilizable接口,便于序列化Session對象;(干貨——StandardSession類實現了 java.lang.Serilizable接口,便于序列化Session對象) 2)StandardSession類的構造函數接收Manager接口的一個實例:迫使一個Session對象必須擁有一個Session管理器實例;(干貨——注意迫使二字) <span style="font-size:18px;">public StandardSession(Manager manager);</span> 3)下面是StandardSession實例的一些比較重要的私有變量,用于維護該StandardSession實例的一些狀態。注意,帶有transient關鍵字的變量是無法序列化的; Attention)在tomcat5中,上述變量是受保護的,而tomcat4中,它們都是私有變量。每個變量都有與之對應的一個訪問器和一個轉變器(修改器)(get 和 set 方法) class StandardSessionimplements HttpSession, Session, Serializable { public StandardSession(Manager manager) { super();this.manager = manager;if (manager instanceof ManagerBase)this.debug = ((ManagerBase) manager).getDebug();}private static final String NOT_SERIALIZED ="___NOT_SERIALIZABLE_EXCEPTION___"; private HashMap attributes = new HashMap();private transient String authType = null; private transient Method containerEventMethod = null; private static final Class containerEventTypes[] = { String.class, Object.class }; private long creationTime = 0L; private transient int debug = 0;private transient boolean expiring = false;private transient StandardSessionFacade facade = null;private String id = null;private static final String info = "StandardSession/1.0";private long lastAccessedTime = creationTime;private transient ArrayList listeners = new ArrayList(); private Manager manager = null;private int maxInactiveInterval = -1;private boolean isNew = false;private boolean isValid = false;private transient HashMap notes = new HashMap();private transient Principal principal = null;private static StringManager sm =StringManager.getManager(Constants.Package); private static HttpSessionContext sessionContext = null;private transient PropertyChangeSupport support =new PropertyChangeSupport(this); private long lastUsedTime = creationTime; } 4)其中 getSession()方法會通過一個自身實例來創建 StandardSessionFacade 類的一個實例,并將其返回;(干貨——如果是單例模式的話,該方法寫的就很有問題,當然只是小生我一個小小的猜想) public HttpSession getSession() {if(facade==null){facade = new StandardSessionFacade(this);}return facade; }5)設置Session為過期:若Session管理器中的某個Session對象 在某個時間長度內都沒有被訪問 的話,會被 Session 管理器設置為過期,這個時間長度由變量 maxInactiveInterval 的值來指定;(干貨——將一個Session對象設置為過期主要是通過Session接口的expire() 方法來完成的); public void expire(boolean notify) {// Mark this session as "being expired" if neededif (expiring)return;expiring = true;setValid(false);// Remove this session from our manager's active sessionsif (manager != null)manager.remove(this);// Unbind any objects associated with this sessionString keys[] = keys();for (int i = 0; i < keys.length; i++)removeAttribute(keys[i], notify);// Notify interested session event listenersif (notify) {fireSessionEvent(Session.SESSION_DESTROYED_EVENT, null);}// Notify interested application event listeners// FIXME - Assumes we call listeners in reverse orderContext context = (Context) manager.getContainer();Object listeners[] = context.getApplicationListeners();if (notify && (listeners != null)) {HttpSessionEvent event =new HttpSessionEvent(getSession());for (int i = 0; i < listeners.length; i++) {int j = (listeners.length - 1) - i;if (!(listeners[j] instanceof HttpSessionListener))continue;HttpSessionListener listener =(HttpSessionListener) listeners[j]; try {fireContainerEvent(context, // highlight line."beforeSessionDestroyed",listener);listener.sessionDestroyed(event); // destroy session.fireContainerEvent(context, // also highlight line."afterSessionDestroyed",listener);} catch (Throwable t) {try {fireContainerEvent(context,"afterSessionDestroyed",listener);} catch (Exception e) {;}// FIXME - should we do anything besides log these?log(sm.getString("standardSession.sessionEvent"), t);}}}// We have completed expire of this sessionexpiring = false;if ((manager != null) && (manager instanceof ManagerBase)) {recycle();} }
private void fireContainerEvent(Context context,String type, Object data)throws Exception {if (!"org.apache.catalina.core.StandardContext".equals(context.getClass().getName())) {return; // Container events are not supported}// NOTE: Race condition is harmless, so do not synchronizeif (containerEventMethod == null) {containerEventMethod =context.getClass().getMethod("fireContainerEvent", // terrific line.containerEventTypes);}Object containerEventParams[] = new Object[2];containerEventParams[0] = type;containerEventParams[1] = data;containerEventMethod.invoke(context, containerEventParams);}
【1.3】StandardSessionFacade類 1)為了傳遞一個 Session對象給servlet實例,Catalina會實例化 StandardSession 類,填充該Session對象,然后再將其傳給 servlet實例;(干貨——但是,請注意下面的但是) 2)但是(呵呵):Catalina傳遞的是Session的 外觀類 StandardSessionFacade的實例,該類僅僅實現了 javax.servlet.http.HttpSession接口中的方法; 3)這樣,servlet程序員就不能將 HttpSession對象向下轉換為 ?StandardSession 類型,也阻止了 servlet程序員訪問一些敏感方法;
【2】Manager 1)Catalina通過一個稱為Session 管理器的組件來管理建立的Session對象,該組件由org.apache.catalina.Manager接口來表示。 2)Catalina中:org.apache.catalina.session包中有一個名為 ManagerBase的工具類,該類提供了常見功能的實現。ManagerBase類有兩個直接子類,分別是 StandardManager 類 和 PersistentManagerBase類;(干貨——引入ManagerBase的工具類) 3)當Catalina運行時:StandardManager實例會將Session對象存儲在內存中,但是,當Catalina關閉時,它(Manager)會將當前內存中的所有Session對象存儲到一個文件中。當再次啟動Catalina時,又會將這些Session 對象重新載入到內存中; 4)PersistentManagerBase類:該類會將 Session對象存儲到輔助存儲器中,其類圖如下:(干貨——PersistentManagerBase類會將 Session對象存儲到輔助存儲器中)
【2.1】Manager接口 1)intro to Manager interface:Session管理器是 Manager接口的實例; 2)Manager接口的源代碼 public interface Manager { public Container getContainer(); public void setContainer(Container container); public DefaultContext getDefaultContext(); public void setDefaultContext(DefaultContext defaultContext); public boolean getDistributable(); public void setDistributable(boolean distributable); public String getInfo(); public int getMaxInactiveInterval(); public void setMaxInactiveInterval(int interval); public void add(Session session); public Session createEmptySession(); public void addPropertyChangeListener(PropertyChangeListener listener); public Session createSession(); public Session findSession(String id) throws IOException; public Session[] findSessions(); public void load() throws ClassNotFoundException, IOException; public void remove(Session session); public void removePropertyChangeListener(PropertyChangeListener listener); public void unload() throws IOException; } 對上述代碼的分析(Analysis):
A1)getContainer()方法和setContainer()方法:以便將一個Manager實現與一個 Context容器相關聯; A2)createSession()方法:用來創建一個Session實例; A3)add()方法:會將一個 Session實例添加到 Session池中; A4)remove()方法:則會將一個 Session實例從 Session池中移除; A5)getMaxInactiveInterval()方法和 setMaxInactiveInterval()方法:用來獲取或設置一個時間長度,單位為秒; A6)Session管理器: 會以此作為一個Session對象的最長存活時間; A7)load()方法和unload() 方法:用來將Session對象持久化到輔助存儲器中(干貨——load方法和unload方法) A7.1)load方法:該方法會將介質中的Session對象重新載入到內存中; A7.2)unload方法:該方法會將當前活動的 Session對象存儲到 Manager 實現指定的介質中; 【2.2】ManagerBase類(org.apache.catalina.session.ManagerBase) 1)ManagerBase.createSession()方法:該方法會創建一個新的Session對象, 每個Session對象都有一個 唯一的標識符; 2)generateSessionId()方法:可以通過 ManagerBase類受保護的方法generateSessionId() 方法來返回一個唯一的標識符; Attention)一個活動的Session對象:指的是有效的,還未過期的Session對象;(干貨——活動的Session對象的定義) 3)某個Context容器的 Session管理器會管理該 Context容器中所有活動的Session對象。這些活動的Session對象都存儲在一個名為sessions 的 HashMap變量中: protected HashMap sessions = new HashMap(); 4)add方法,remove方法,findSession方法 public void add(Session session) {synchronized (sessions) {sessions.put(session.getId(), session);if( sessions.size() > maxActive ) {maxActive=sessions.size();}}} public void remove(Session session) {synchronized (sessions) {sessions.remove(session.getId());}} public Session[] findSessions() {Session results[] = null;synchronized (sessions) {results = new Session[sessions.size()];results = (Session[]) sessions.values().toArray(results);}return (results);} public Session findSession(String id) throws IOException {if (id == null)return (null);synchronized (sessions) {Session session = (Session) sessions.get(id);return (session);}} 【2.3】StandardManger類 1)intro to StandardManager:StandardManager類是Manager接口的標準實現,該類將 Session 存儲于內存中; 1.1)該類還實現了Lifecycle接口:這樣就可以由與其關聯的Context容器來啟動和關閉;其中stop方法的實現會調用 unload() 方法,以便將有效的Session對象序列化到一個名為"SESSION.ser"的文件中,而且每個Context容器都會產生這樣一個文件;(干貨——引入了SESSION.ser文件,unload() 方法持久化Session對象到文件) 1.2)SESSION.ser文件:位于環境變量 CATALINA_HOME指定的目錄下的work目錄中; 看個荔枝)在tomcat4或tomcat5中,如果運行了實例應用程序,就可以在 CATALINA_HOME/work/Standalone/localhost/examples 目錄下找到 SESSION.ser文件; 1.3)當StandardManager實例再次啟動時:這些Session 對象會通過調用load() 方法重新讀入內存中;(干貨——load()方法將持久化文件中的Session對象重新讀入內存) 2)Session管理器:還要負責銷毀那些已經失效的Session對象,在tomcat4中的 StandardManager類中,這項工作是由一個專門的線程來完成的; public void run() { //org.apache.catalina.session.StandardManager// Loop until the termination semaphore is setwhile (!threadDone) {threadSleep();processExpires();}} 對上述代碼的分析(Analysis): A1)threadSleep()方法:會使線程休眠一段時間,長度由checkInterval指定,default==60 秒; A2)processExpir()方法:會遍歷由 Session管理器管理的所有Session對象,將Session實例的lastAccessedTime屬性值與當前時間進行比較,如果兩者之間的差值超過了變量 maxInactiveInterval 指定的數值,則會調用 Session接口的 expire() 方法使這個Session實例過期; private void processExpires() { // org.apache.catalina.session.StandardManager.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.getLastUsedTime()) / 1000L);if (timeIdle >= maxInactiveInterval) {try {expiredSessions++;session.expire();// highlight line.} catch (Throwable t) {log(sm.getString("standardManager.expireException"), t);}}}} Attention) A1)在tomcat5中,StandardManager類已不再實現 java.lang.Runnable接口。其中的 backgroundporocess() 方法會直接調用 tomcat5 中StandardManager對象的 processExpires()方法; public voiid backgroundProcess() { //org.apache.catalina.session.StandardManager.backgroundProcess()processExpire(); } public void expire() { //org.apache.catalina.sessoin.StandardManager.expire()expire(true);} public void expire(boolean notify) { // the same as the above.// Mark this session as "being expired" if neededif (expiring)return;expiring = true;setValid(false);// Remove this session from our manager's active sessionsif (manager != null)manager.remove(this);// Unbind any objects associated with this sessionString keys[] = keys();for (int i = 0; i < keys.length; i++)removeAttribute(keys[i], notify);// Notify interested session event listenersif (notify) {fireSessionEvent(Session.SESSION_DESTROYED_EVENT, null);}// Notify interested application event listeners// FIXME - Assumes we call listeners in reverse orderContext context = (Context) manager.getContainer();Object listeners[] = context.getApplicationListeners();if (notify && (listeners != null)) {HttpSessionEvent event =new HttpSessionEvent(getSession());for (int i = 0; i < listeners.length; i++) {int j = (listeners.length - 1) - i;if (!(listeners[j] instanceof HttpSessionListener))continue;HttpSessionListener listener =(HttpSessionListener) listeners[j];try {fireContainerEvent(context,"beforeSessionDestroyed",listener);listener.sessionDestroyed(event);fireContainerEvent(context,"afterSessionDestroyed",listener);} catch (Throwable t) {try {fireContainerEvent(context,"afterSessionDestroyed",listener);} catch (Exception e) {;}// FIXME - should we do anything besides log these?log(sm.getString("standardSession.sessionEvent"), t);}}}// We have completed expire of this sessionexpiring = false;if ((manager != null) && (manager instanceof ManagerBase)) {recycle();}} 【2.4】PersistentManagerBase類(參見上圖) 1)intro to PersistentManagerBase:該類是所有持久化Session管理器的 父類; 1.1)StandardManager實例 和 持久化Session管理器(PersistentManagerBase)的區別:在于后者中存儲器的表示形式,即存儲Session 對象的輔助存儲器的形式,使用Store對象來表示; 2)在持久化Session管理器中:Session對象可以備份,也可以換出;(干貨——Session對象可以備份,也可以換出);如果server destroyed,就可以從存儲器中獲取活動的Session對象; 2.1)當Session對象被換出:他會被移動到存儲器中,因為當前活動的Session對象數超過了上限值,或者這個Session對象閑置了過長時間;(干貨——換出是為了節省內存空間) 2.2)tomcat4中,PersistentManagerBase類實現了java.lang.Runnable 接口:使用一個專門的線程來執行備份和換出活動Session對象的任務;(run() 方法如下 )(干貨——注意是tomcat4,使用一個專門的線程來執行備份和換出活動Session對象的任務) public void run() { //org.apache.catalina.session.PersistentManagerBase.run()while(!threadDone) {threadSleep();processExpires(); // 檢查Session對象是否過期.processPersistenceChecks();} } public void processPersistenceChecks(){ // org.apache.catalina.session.PersistentManagerBase.processPersistenceChecks()processMaxIdleSwaps(); // highlight line.(下面我們只以processMaxIdleSwaps方法為例進行調用過程的分析,其他兩個方法類似)processMaxActiveSwaps(); // highlight line.processMaxIdleBackups(); // highlight line. } /*** Swap idle sessions out to Store if they are idle too long.若session空閑太久則換出*/protected void processMaxIdleSwaps() {if (!isStarted() || maxIdleSwap < 0)return;Session sessions[] = findSessions();long timeNow = System.currentTimeMillis();// Swap out all sessions idle longer than maxIdleSwap// FIXME: What's preventing us from mangling a session during// a request?if (maxIdleSwap >= 0) {for (int i = 0; i < sessions.length; i++) {StandardSession session = (StandardSession) sessions[i];if (!session.isValid())continue;long lastAccessed = ((StandardSession)session).getLastUsedTime();int timeIdle = // Truncate, do not round up(int) ((timeNow - lastAccessed) / 1000L);if (timeIdle > maxIdleSwap && timeIdle > minIdleSwap) {if (debug > 1)log(sm.getString("persistentManager.swapMaxIdle",session.getId(), new Integer(timeIdle)));try {swapOut(session); // highlight line.} catch (IOException e) {; // This is logged in writeSession()}}}}}protected void swapOut(Session session) throws IOException { //換出操作if (store == null ||!session.isValid() ||isSessionStale(session, System.currentTimeMillis()))return;((StandardSession)session).passivate();writeSession(session); // highlight line.super.remove(session);session.recycle();}protected void writeSession(Session session) throws IOException {if (store == null ||!session.isValid() ||isSessionStale(session, System.currentTimeMillis()))return;try {store.save(session); //highlight line.sessionSwapIgnore.remove(session.getId());} catch (IOException e) {log(sm.getString("persistentManager.serializeError", session.getId(), e));throw e;}}
3)tomcat5中,PersistentManagerBase類不再實現 java.lang.Runnable 接口。備份和換出Session對象的任務由其 backgroundProcess() 管理器完成,后者由其相關聯的 StandardContext 實例周期性地調用; (下面分析 換出和備份Session實例的實現過程)
4)PersistentManagerBase類換出Session實例 4.1)換出條件:只有當活動Session對象的數量超過了變量 maxActiveSessions指定的上限值,或者該Session對象閑置了很長時間后,才會被換出;(干貨——Session對象被換出的condition) 4.1.1)當活動的Session實例數量過多時:PersistentManagerBase會將Session對象換出直到數量等于?maxActiveSessions;(參見 processMaxActiveSwaps()方法) 4.1.2)當某個Session對象閑置了過長時間:PersistentManagerBase會依據兩個變量來決定是否將這個Session換出,這兩個變量是minIdleSwap and maxIdleSwap;如果某個Session對象的 lastAccessedTime 屬性的值超過了 minIdleSwap 和 maxIdleSwap 的值,就會將這個Session換出;(干貨——為了防止換出Session對象,將maxIdleSwap設置為負數,參見 processMaxIdleSwaps() 方法); 4.2)如何查找Session實例:因為Session對象可以被換出,所以他既可能駐留在內存中,也可能駐留在存儲器中,因此,findSession(String id ) 方法首先會在內存中查找是否存在該Session對象,若沒有找到,才會在存儲器中繼續find。(PersistentManagerBase.findSession() 方法的源代碼如下)(干貨——Session對象被換出后,可能駐留在內存中,也可能駐留在存儲器中) public Session findSession(String id) throws IOException { //org.apache.catalina.session.PersistentManagerBase.findSession()Session session = super.findSession(id);if (session != null)return (session);// See if the Session is in the Storesession = swapIn(id); // highlight line.return (session);}protected Session swapIn(String id) throws IOException { // the same as the above.if (store == null)return null;if (sessionSwapIgnore.contains(id)) {return null;}Session session = null;try {session = store.load(id); // highlight line.} catch (ClassNotFoundException e) {log(sm.getString("persistentManager.deserializeError", id, e));throw new IllegalStateException(sm.getString("persistentManager.deserializeError", id, e));}if (session == null) {sessionSwapIgnore.put(id,id);return (null);}if (!session.isValid()|| isSessionStale(session, System.currentTimeMillis())) {log("session swapped in is invalid or expired");session.expire(); // highlight line.store.remove(id); // highlight line.sessionSwapIgnore.put(id,id);return (null);}if(debug > 2)log(sm.getString("persistentManager.swapIn", id));session.setManager(this);// To make sure the listener knows about it.((StandardSession)session).tellNew();add(session);((StandardSession)session).activate();return (session);}
5)PersistentManagerBase類備份Session實例 5.1)備份條件:PersistentManagerBase實例 僅僅會備份那些空閑時間超過了變量 maxIdleBackup 的Session對象。 5.2)processMaxIdleBackups()方法:負責對該Session對象進行備份操作;
【2.5】PersistentManager類 1)intro to?PersistentManager類:該類繼承了?PersistentManagerBase類,僅僅是多添加了兩個屬性而已; public final class PersistentManager extends PersistentManagerBase { //org.apache.catalina.session.PersistentManager private static final String info = "PersistentManager/1.0"; protected static String name = "PersistentManager"; public String getInfo() {return (this.info);} public String getName() {return (name);}} 【2.6】DistributedManager類(應用與集群環境) 1)intro to?DistributedManager類:繼承自PersistentManagerBase類,用于兩個或多個節點的集群環境; 1.1)一個節點表示部署的一臺 tomcat server:而集群中的節點 可以在同一臺機器上,也可以在不同的machine上; 1.2)每個節點必須使用 DistributedManager實例作為其Session管理器:這樣才能支持復制 Session對象,這也是DistributedManager的主要功能?;(干貨——DistributedManager的主要功能) 2)DistributedManager會向其他節點發送消息:為了實現copy Session對象的目的,當創建或銷毀Session對象時,DistributedManager會向其他節點發送消息;(干貨——DistributedManager會向其他節點發送消息) 2.2)而且:集群中的節點也必須能夠接受其他節點發送的消息。這樣,http 請求才能到達集群中的任意節點; 3)catalina在org.apache.catlina.cluster中提供了一些工具:這些工具用于 與集群中其他節點的DistributedManager實例發送和接收消息;其中,ClusterSender 類用于向集群中的其他節點發送消息,ClusterReceiver 則用于接收集群中其他節點發送的消息;(干貨——catalina在org.apache.catlina.cluster中提供了一些工具,如ClusterSender and ClusterReceiver) 4)DistributedManager.createSession()方法: 要創建一個Sesssion對象,存儲在當前?DistributedManager 實例中,并使用 ClusterSender實例向其他節點發送消息; public Session createSession() {Session session = super.createSession(); // step1.ObjectOutputStream oos = null;ByteArrayOutputStream bos = null;ByteArrayInputStream bis = null;try {bos = new ByteArrayOutputStream();oos = new ObjectOutputStream(new BufferedOutputStream(bos));((StandardSession)session).writeObjectData(oos);oos.close();byte[] obs = bos.toByteArray();clusterSender.send(obs); // step2.if(debug > 0)log("Replicating Session: "+session.getId());} catch (IOException e) {log("An error occurred when replicating Session: "+session.getId());}return (session);} 對以上代碼的分析(Analysis): step1)調用超類的 createSession()方法為自身創建一個 Session對象; step2)使用ClusterSender實例,以字節數組的形式將該Session對象發送到集群中的其他節點; 5)DistributedManager類還實現了 java.lang.Runnable 接口: 這樣就可以使用一個 專門的thread 來檢查 Session對象是否過期,并從集群中的其他節點接收消息;(其run() 方法的源代碼如下)? public void run() { // org.apache.catalina.session.DistributedManager.run()// Loop until the termination semaphore is setwhile (!threadDone) {threadSleep();processClusterReceiver(); // 以該方法為例進行說明.processExpires();processPersistenceChecks();}} /*** Called from our background thread to process new received Sessions**/public void processClusterReceiver() { //org.apache.catalina.session.DistributedManager.processClusterReceiver()Object[] objs = clusterReceiver.getObjects();StandardSession _session = null;ByteArrayInputStream bis = null;Loader loader = null;ClassLoader classLoader = null;ObjectInputStream ois = null;byte[] buf = new byte[5000];ReplicationWrapper repObj = null;for(int i=0; i < objs.length;i++) {try {bis = new ByteArrayInputStream(buf);repObj = (ReplicationWrapper)objs[i];buf = repObj.getDataStream();bis = new ByteArrayInputStream(buf, 0, buf.length);if (container != null)loader = container.getLoader();if (loader != null)classLoader = loader.getClassLoader();if (classLoader != null)ois = new CustomObjectInputStream(bis,classLoader);elseois = new ObjectInputStream(bis);_session = (StandardSession) super.createSession();_session.readObjectData(ois);_session.setManager(this);if (debug > 0)log("Loading replicated session: "+_session.getId());} catch (IOException e) {log("Error occurred when trying to read replicated session: "+e.toString());} catch (ClassNotFoundException e) {log("Error occurred when trying to read replicated session: "+e.toString());} finally {if (ois != null) {try {ois.close();bis = null;} catch (IOException e) {;}}}}} 【3】存儲器(存儲器是org.apache.catalina.Store接口的實例) 1)intro to 存儲器:是為Session管理器管理的 Session對象提供持久化存儲器的一個組件;(干貨——intro to 存儲器——org.apache.catalina.Store接口的實例) 2)Store接口的源代碼如下: public interface Store { // org.apache.catalina.Storepublic 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; public void removePropertyChangeListener(PropertyChangeListener listener); public void save(Session session) throws IOException; } 3)Store接口中有兩個方法:save()方法和 load() 方法(干貨——注意save方法和 load方法,它們的作用分別是什么) 3.1)save方法:用于將指定的 Session對象存儲到某種持久性存儲器中; 3.2)load方法:會從存儲器中,依據Session對象 的標識符將該Session對象載入(加載)到內存中; 補充)keys()方法:會以字符串數組的形式返回所有 Session 對象的標識符; 4)其UML 類圖如下所示:
【3.1】StoreBase類圖(見上圖) 1)intro to?StoreBase:該類是一個抽象類,提供了一些基本功能,有兩個子類; 2)StoreBase類:并沒有實現Store接口的save()方法 和 load()方法,因為,這兩個方法依賴于持久化Session對象的存儲器的類型; 3)在tomcat4中:StoreBase類使用另一個線程周期性地檢查 Session對象:從活動的Session的集合中移除過期的Session對象;(下面是 tomcat4 中 StoreBase.run()方法的源代碼,注意是tomcat4) public void run() { // org.apache.catalina.session.StoreBase.run()// Loop until the termination semaphore is setwhile (!threadDone) {threadSleep();processExpires();}} 對以上代碼的分析(Analysis): A1)processExpires方法:會獲取所有活動的 Session對象,檢查 每個Session對象的 lastAccessedTime屬性值,刪除那些長時間不活動的Session對象;源代碼如下:(干貨——再次提及了processExpires方法,注意其功能) protected void processExpires() {// org.apache.catalina.session.StoreBase.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 == null) {continue;}if (!session.isValid()) {continue;}int maxInactiveInterval = session.getMaxInactiveInterval();if (maxInactiveInterval < 0) {continue;}int timeIdle = // Truncate, do not round up(int) ((timeNow - session.getLastUsedTime()) / 1000L);if (timeIdle >= maxInactiveInterval) {if ( ( (PersistentManagerBase) manager).isLoaded( keys[i] )) {// recycle old backup sessionsession.recycle();} else {// expire swapped out sessionsession.expire();}remove(session.getId());}} catch (Exception e) {log ("Session: "+keys[i]+"; "+e.toString());try {remove(keys[i]);} catch (IOException e2) {log (e2.toString());e2.printStackTrace();}}}}
Attention)在tomcat5中,不再使用專用的線程調用 processExpire()方法 ,而是,相關聯的 PersistentManagerBase實例的 backgroundProcess()方法會周期性地調用 processExpires() 方法;
【3.2】FileStore類 1)intro to FileStore:FileStore類會將 Session對象存儲到某個文件中,文件名會使用Session對象的 標識符再加上一個后綴 ".session" 構成;(干貨——FileStore類的作用) 2)文件位于臨時的工作dir下:可以調用FileStore.setDirectory() 方法進行修改臨時目錄的位置; (干貨——理清FileStore.save and FileStore.load方法的作用)
3)save方法:使用 java.io.ObjectOutputStream類將 Session對象進行序列化,故所有存儲在Session實例中的對象都要實現 java.lang.Serializable接口; 4)load方法:該方法使用 java.io.ObjectInputStream 類實現 Session 對象的反序列化;
【3.3】JDBCStore類 1)intro to JDBCStore:該類將Session對象通過JDBC 存入 db中;(干貨——JDBCStore類的作用)
【4】app program 1)intro to app program:該應用程序中的Context容器使用StandardManager實例來管理Session對象,為了測試該應用程序,可以使用第3個示例servlet:SessionServlet; public class SessionServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {System.out.println("SessionServlet -- service");response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<html>");out.println("<head><title>SessionServlet</title></head>");out.println("<body>");String value = request.getParameter("value");HttpSession session = request.getSession(true);out.println("<br>the previous value is " +(String) session.getAttribute("value"));out.println("<br>the current value is " + value);session.setAttribute("value", value);out.println("<br><hr>");out.println("<form>");out.println("New Value: <input name=value>");out.println("<input type=submit>");out.println("</form>");out.println("</body>");out.println("</html>");} }
【4.1】Bootstrap類 1)源代碼如下
public static void main(String[] args) {//invoke: http://localhost:8080/myApp/SessionSystem.setProperty("catalina.base", System.getProperty("user.dir"));Connector connector = new HttpConnector();Wrapper wrapper1 = new SimpleWrapper();wrapper1.setName("Session");wrapper1.setServletClass("SessionServlet");Context context = new StandardContext();// StandardContext's start method adds a default mappercontext.setPath("/myApp");context.setDocBase("myApp");context.addChild(wrapper1);// context.addServletMapping(pattern, name);// note that we must use /myApp/Session, not just /Session// because the /myApp section must be the same as the path, so the cookie will// be sent back.context.addServletMapping("/myApp/Session", "Session");// add ContextConfig. This listener is important because it configures// StandardContext (sets configured to true), otherwise StandardContext// won't startLifecycleListener listener = new SimpleContextConfig();((Lifecycle) context).addLifecycleListener(listener);// here is our loaderLoader loader = new WebappLoader();// associate the loader with the Contextcontext.setLoader(loader);connector.setContainer(context);// add a ManagerManager manager = new StandardManager(); // highlight line.context.setManager(manager); // highlight line.try {connector.initialize();((Lifecycle) connector).start();((Lifecycle) context).start();// make the application wait until we press a key.System.in.read();((Lifecycle) context).stop();}catch (Exception e) {e.printStackTrace();}} } 【4.2】SimpleWrapperValve類 1)servlet示例可以調用javax.servlet.http.HttpServletRequest接口的getSession() 方法獲取Session對象:當調用 getSession() 方法時,request對象必須調用與Context 容器相關聯的Session管理器。Session管理器組件要么創建一個新的session 對象,要么返回一個已經存在的session對象。request對象為了能夠訪問Session管理器,他必須能夠訪問Context容器。 2)為了達到這個目的:在 SimpleWrapperValve類的invoke方法中,需要調用 org.apache.catalina.Request接口的setContext()方法,并傳入 Context實例; Attention)SimpleWrapperValve.invoke方法會調用被請求的servlet實例的service方法,所以,必須在調用service方法之前設置 Context實例; public void invoke(Request request, Response response, ValveContext valveContext)throws IOException, ServletException {SimpleWrapper wrapper = (SimpleWrapper) getContainer();ServletRequest sreq = request.getRequest();ServletResponse sres = response.getResponse();Servlet servlet = null;HttpServletRequest hreq = null;if (sreq instanceof HttpServletRequest)hreq = (HttpServletRequest) sreq;HttpServletResponse hres = null;if (sres instanceof HttpServletResponse)hres = (HttpServletResponse) sres;//-- new addition -----------------------------------Context context = (Context) wrapper.getParent(); // highlight line.request.setContext(context); // highlight line.//-------------------------------------// Allocate a servlet instance to process this requesttry {servlet = wrapper.allocate();if (hres!=null && hreq!=null) {servlet.service(hreq, hres);}else {servlet.service(sreq, sres);}}catch (ServletException e) {}} 補充)org.apache.catalina.connector.HttpRequestBase類的私有方法:doGetSession() 會調用Context接口的 getManager() 方法來獲取 Session管理器對象:(當獲取了Session管理器對象之后,就可以獲取到Session對象了,或直接創建一個新Session) Manager manager = null; if(contect != null)manager = context.getManager();
【4.3】打印結果 1)console info E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src>java -cp .;lib/servlet.jar;lib/catalina_4_1_24.jar;lib/catalina-5.5.4.jar;lib/naming-common. jar;lib/commons-collections.jar;lib/naming-resources.jar;lib/;lib/catalina.jar;E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\webroot com/tomca t/chapter9/startup/Bootstrap HttpConnector Opening server socket on all host IP addresses HttpConnector[8080] Starting background thread WebappLoader[/myApp]: Deploying class repositories to work directory E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src\work\_\_\myApp Starting Wrapper Session StandardManager[/myApp]: Seeding random number generator class java.security.SecureRandom StandardManager[/myApp]: Seeding of random number generator has been completed SessionServlet -- service SessionServlet -- service SessionServlet -- service SessionServlet -- service 2)訪問信息
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
【1】Session對象 1)intro to Session object:Session對象由 javax.servlet.http.HttpSession接口表示; 2)UML類圖實例:
【1.1】Session接口 1)Session接口是作為 Catalina內部的外觀類使用的。Session 接口的標準實現 StandardSession類也實現了 javax.servlet.http.HttpSession 接口; public interface Session { public static final String SESSION_CREATED_EVENT = "createSession"; public static final String SESSION_DESTROYED_EVENT = "destroySession"; public String getAuthType(); public void setAuthType(String authType); public long getCreationTime(); public void setCreationTime(long time); public String getId(); public void setId(String id); public String getInfo(); public long getLastAccessedTime(); public Manager getManager(); public void setManager(Manager manager); public int getMaxInactiveInterval(); public void setMaxInactiveInterval(int interval); public void setNew(boolean isNew); public Principal getPrincipal(); public void setPrincipal(Principal principal); public HttpSession getSession(); public void setValid(boolean isValid); public boolean isValid(); public void access(); public void addSessionListener(SessionListener listener); public void expire(); public Object getNote(String name); public Iterator getNoteNames(); public void recycle();public void removeNote(String name);public void removeSessionListener(SessionListener listener);public void setNote(String name, Object value); } 2)Session對象總是存在于Session管理器中的:可以使用 getManager()方法或 setManager() 方法將Session實例和某個 Session管理器相關聯; 2.1)對某個Session實例來說:在與其Session 管理器相關聯的某個Context實例內,該Session對象有一個唯一的標識符,可以通過 setId() 方法和 getId() 方法來訪問該 Session 標識符; 2.2)Session管理器會調用 getLastAccessedTime()方法:根據返回值來判斷一個Session對象的有效性;(Session管理器會調用setValid()方法來設置或重置其有效性); 2.3)每當訪問一個Session實例時: 會調用其 access()方法 來修改 Session對象的最后訪問時間; 2.4)最后:Session管理器會調用Session對象的 expire()方法將其設置為過期, 也可以通過getSession() 方法獲取一個經過 Session 外觀類包裝的 HttpSession 對象;
【1.2】StandardSession類 1)StandardSession類實現了 java.lang.Serilizable接口,便于序列化Session對象;(干貨——StandardSession類實現了 java.lang.Serilizable接口,便于序列化Session對象) 2)StandardSession類的構造函數接收Manager接口的一個實例:迫使一個Session對象必須擁有一個Session管理器實例;(干貨——注意迫使二字) <span style="font-size:18px;">public StandardSession(Manager manager);</span> 3)下面是StandardSession實例的一些比較重要的私有變量,用于維護該StandardSession實例的一些狀態。注意,帶有transient關鍵字的變量是無法序列化的; Attention)在tomcat5中,上述變量是受保護的,而tomcat4中,它們都是私有變量。每個變量都有與之對應的一個訪問器和一個轉變器(修改器)(get 和 set 方法) class StandardSessionimplements HttpSession, Session, Serializable { public StandardSession(Manager manager) { super();this.manager = manager;if (manager instanceof ManagerBase)this.debug = ((ManagerBase) manager).getDebug();}private static final String NOT_SERIALIZED ="___NOT_SERIALIZABLE_EXCEPTION___"; private HashMap attributes = new HashMap();private transient String authType = null; private transient Method containerEventMethod = null; private static final Class containerEventTypes[] = { String.class, Object.class }; private long creationTime = 0L; private transient int debug = 0;private transient boolean expiring = false;private transient StandardSessionFacade facade = null;private String id = null;private static final String info = "StandardSession/1.0";private long lastAccessedTime = creationTime;private transient ArrayList listeners = new ArrayList(); private Manager manager = null;private int maxInactiveInterval = -1;private boolean isNew = false;private boolean isValid = false;private transient HashMap notes = new HashMap();private transient Principal principal = null;private static StringManager sm =StringManager.getManager(Constants.Package); private static HttpSessionContext sessionContext = null;private transient PropertyChangeSupport support =new PropertyChangeSupport(this); private long lastUsedTime = creationTime; } 4)其中 getSession()方法會通過一個自身實例來創建 StandardSessionFacade 類的一個實例,并將其返回;(干貨——如果是單例模式的話,該方法寫的就很有問題,當然只是小生我一個小小的猜想) public HttpSession getSession() {if(facade==null){facade = new StandardSessionFacade(this);}return facade; }5)設置Session為過期:若Session管理器中的某個Session對象 在某個時間長度內都沒有被訪問 的話,會被 Session 管理器設置為過期,這個時間長度由變量 maxInactiveInterval 的值來指定;(干貨——將一個Session對象設置為過期主要是通過Session接口的expire() 方法來完成的); public void expire(boolean notify) {// Mark this session as "being expired" if neededif (expiring)return;expiring = true;setValid(false);// Remove this session from our manager's active sessionsif (manager != null)manager.remove(this);// Unbind any objects associated with this sessionString keys[] = keys();for (int i = 0; i < keys.length; i++)removeAttribute(keys[i], notify);// Notify interested session event listenersif (notify) {fireSessionEvent(Session.SESSION_DESTROYED_EVENT, null);}// Notify interested application event listeners// FIXME - Assumes we call listeners in reverse orderContext context = (Context) manager.getContainer();Object listeners[] = context.getApplicationListeners();if (notify && (listeners != null)) {HttpSessionEvent event =new HttpSessionEvent(getSession());for (int i = 0; i < listeners.length; i++) {int j = (listeners.length - 1) - i;if (!(listeners[j] instanceof HttpSessionListener))continue;HttpSessionListener listener =(HttpSessionListener) listeners[j]; try {fireContainerEvent(context, // highlight line."beforeSessionDestroyed",listener);listener.sessionDestroyed(event); // destroy session.fireContainerEvent(context, // also highlight line."afterSessionDestroyed",listener);} catch (Throwable t) {try {fireContainerEvent(context,"afterSessionDestroyed",listener);} catch (Exception e) {;}// FIXME - should we do anything besides log these?log(sm.getString("standardSession.sessionEvent"), t);}}}// We have completed expire of this sessionexpiring = false;if ((manager != null) && (manager instanceof ManagerBase)) {recycle();} }
private void fireContainerEvent(Context context,String type, Object data)throws Exception {if (!"org.apache.catalina.core.StandardContext".equals(context.getClass().getName())) {return; // Container events are not supported}// NOTE: Race condition is harmless, so do not synchronizeif (containerEventMethod == null) {containerEventMethod =context.getClass().getMethod("fireContainerEvent", // terrific line.containerEventTypes);}Object containerEventParams[] = new Object[2];containerEventParams[0] = type;containerEventParams[1] = data;containerEventMethod.invoke(context, containerEventParams);}
【1.3】StandardSessionFacade類 1)為了傳遞一個 Session對象給servlet實例,Catalina會實例化 StandardSession 類,填充該Session對象,然后再將其傳給 servlet實例;(干貨——但是,請注意下面的但是) 2)但是(呵呵):Catalina傳遞的是Session的 外觀類 StandardSessionFacade的實例,該類僅僅實現了 javax.servlet.http.HttpSession接口中的方法; 3)這樣,servlet程序員就不能將 HttpSession對象向下轉換為 ?StandardSession 類型,也阻止了 servlet程序員訪問一些敏感方法;
【2】Manager 1)Catalina通過一個稱為Session 管理器的組件來管理建立的Session對象,該組件由org.apache.catalina.Manager接口來表示。 2)Catalina中:org.apache.catalina.session包中有一個名為 ManagerBase的工具類,該類提供了常見功能的實現。ManagerBase類有兩個直接子類,分別是 StandardManager 類 和 PersistentManagerBase類;(干貨——引入ManagerBase的工具類) 3)當Catalina運行時:StandardManager實例會將Session對象存儲在內存中,但是,當Catalina關閉時,它(Manager)會將當前內存中的所有Session對象存儲到一個文件中。當再次啟動Catalina時,又會將這些Session 對象重新載入到內存中; 4)PersistentManagerBase類:該類會將 Session對象存儲到輔助存儲器中,其類圖如下:(干貨——PersistentManagerBase類會將 Session對象存儲到輔助存儲器中)
【2.1】Manager接口 1)intro to Manager interface:Session管理器是 Manager接口的實例; 2)Manager接口的源代碼 public interface Manager { public Container getContainer(); public void setContainer(Container container); public DefaultContext getDefaultContext(); public void setDefaultContext(DefaultContext defaultContext); public boolean getDistributable(); public void setDistributable(boolean distributable); public String getInfo(); public int getMaxInactiveInterval(); public void setMaxInactiveInterval(int interval); public void add(Session session); public Session createEmptySession(); public void addPropertyChangeListener(PropertyChangeListener listener); public Session createSession(); public Session findSession(String id) throws IOException; public Session[] findSessions(); public void load() throws ClassNotFoundException, IOException; public void remove(Session session); public void removePropertyChangeListener(PropertyChangeListener listener); public void unload() throws IOException; } 對上述代碼的分析(Analysis):
A1)getContainer()方法和setContainer()方法:以便將一個Manager實現與一個 Context容器相關聯; A2)createSession()方法:用來創建一個Session實例; A3)add()方法:會將一個 Session實例添加到 Session池中; A4)remove()方法:則會將一個 Session實例從 Session池中移除; A5)getMaxInactiveInterval()方法和 setMaxInactiveInterval()方法:用來獲取或設置一個時間長度,單位為秒; A6)Session管理器: 會以此作為一個Session對象的最長存活時間; A7)load()方法和unload() 方法:用來將Session對象持久化到輔助存儲器中(干貨——load方法和unload方法) A7.1)load方法:該方法會將介質中的Session對象重新載入到內存中; A7.2)unload方法:該方法會將當前活動的 Session對象存儲到 Manager 實現指定的介質中; 【2.2】ManagerBase類(org.apache.catalina.session.ManagerBase) 1)ManagerBase.createSession()方法:該方法會創建一個新的Session對象, 每個Session對象都有一個 唯一的標識符; 2)generateSessionId()方法:可以通過 ManagerBase類受保護的方法generateSessionId() 方法來返回一個唯一的標識符; Attention)一個活動的Session對象:指的是有效的,還未過期的Session對象;(干貨——活動的Session對象的定義) 3)某個Context容器的 Session管理器會管理該 Context容器中所有活動的Session對象。這些活動的Session對象都存儲在一個名為sessions 的 HashMap變量中: protected HashMap sessions = new HashMap(); 4)add方法,remove方法,findSession方法 public void add(Session session) {synchronized (sessions) {sessions.put(session.getId(), session);if( sessions.size() > maxActive ) {maxActive=sessions.size();}}} public void remove(Session session) {synchronized (sessions) {sessions.remove(session.getId());}} public Session[] findSessions() {Session results[] = null;synchronized (sessions) {results = new Session[sessions.size()];results = (Session[]) sessions.values().toArray(results);}return (results);} public Session findSession(String id) throws IOException {if (id == null)return (null);synchronized (sessions) {Session session = (Session) sessions.get(id);return (session);}} 【2.3】StandardManger類 1)intro to StandardManager:StandardManager類是Manager接口的標準實現,該類將 Session 存儲于內存中; 1.1)該類還實現了Lifecycle接口:這樣就可以由與其關聯的Context容器來啟動和關閉;其中stop方法的實現會調用 unload() 方法,以便將有效的Session對象序列化到一個名為"SESSION.ser"的文件中,而且每個Context容器都會產生這樣一個文件;(干貨——引入了SESSION.ser文件,unload() 方法持久化Session對象到文件) 1.2)SESSION.ser文件:位于環境變量 CATALINA_HOME指定的目錄下的work目錄中; 看個荔枝)在tomcat4或tomcat5中,如果運行了實例應用程序,就可以在 CATALINA_HOME/work/Standalone/localhost/examples 目錄下找到 SESSION.ser文件; 1.3)當StandardManager實例再次啟動時:這些Session 對象會通過調用load() 方法重新讀入內存中;(干貨——load()方法將持久化文件中的Session對象重新讀入內存) 2)Session管理器:還要負責銷毀那些已經失效的Session對象,在tomcat4中的 StandardManager類中,這項工作是由一個專門的線程來完成的; public void run() { //org.apache.catalina.session.StandardManager// Loop until the termination semaphore is setwhile (!threadDone) {threadSleep();processExpires();}} 對上述代碼的分析(Analysis): A1)threadSleep()方法:會使線程休眠一段時間,長度由checkInterval指定,default==60 秒; A2)processExpir()方法:會遍歷由 Session管理器管理的所有Session對象,將Session實例的lastAccessedTime屬性值與當前時間進行比較,如果兩者之間的差值超過了變量 maxInactiveInterval 指定的數值,則會調用 Session接口的 expire() 方法使這個Session實例過期; private void processExpires() { // org.apache.catalina.session.StandardManager.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.getLastUsedTime()) / 1000L);if (timeIdle >= maxInactiveInterval) {try {expiredSessions++;session.expire();// highlight line.} catch (Throwable t) {log(sm.getString("standardManager.expireException"), t);}}}} Attention) A1)在tomcat5中,StandardManager類已不再實現 java.lang.Runnable接口。其中的 backgroundporocess() 方法會直接調用 tomcat5 中StandardManager對象的 processExpires()方法; public voiid backgroundProcess() { //org.apache.catalina.session.StandardManager.backgroundProcess()processExpire(); } public void expire() { //org.apache.catalina.sessoin.StandardManager.expire()expire(true);} public void expire(boolean notify) { // the same as the above.// Mark this session as "being expired" if neededif (expiring)return;expiring = true;setValid(false);// Remove this session from our manager's active sessionsif (manager != null)manager.remove(this);// Unbind any objects associated with this sessionString keys[] = keys();for (int i = 0; i < keys.length; i++)removeAttribute(keys[i], notify);// Notify interested session event listenersif (notify) {fireSessionEvent(Session.SESSION_DESTROYED_EVENT, null);}// Notify interested application event listeners// FIXME - Assumes we call listeners in reverse orderContext context = (Context) manager.getContainer();Object listeners[] = context.getApplicationListeners();if (notify && (listeners != null)) {HttpSessionEvent event =new HttpSessionEvent(getSession());for (int i = 0; i < listeners.length; i++) {int j = (listeners.length - 1) - i;if (!(listeners[j] instanceof HttpSessionListener))continue;HttpSessionListener listener =(HttpSessionListener) listeners[j];try {fireContainerEvent(context,"beforeSessionDestroyed",listener);listener.sessionDestroyed(event);fireContainerEvent(context,"afterSessionDestroyed",listener);} catch (Throwable t) {try {fireContainerEvent(context,"afterSessionDestroyed",listener);} catch (Exception e) {;}// FIXME - should we do anything besides log these?log(sm.getString("standardSession.sessionEvent"), t);}}}// We have completed expire of this sessionexpiring = false;if ((manager != null) && (manager instanceof ManagerBase)) {recycle();}} 【2.4】PersistentManagerBase類(參見上圖) 1)intro to PersistentManagerBase:該類是所有持久化Session管理器的 父類; 1.1)StandardManager實例 和 持久化Session管理器(PersistentManagerBase)的區別:在于后者中存儲器的表示形式,即存儲Session 對象的輔助存儲器的形式,使用Store對象來表示; 2)在持久化Session管理器中:Session對象可以備份,也可以換出;(干貨——Session對象可以備份,也可以換出);如果server destroyed,就可以從存儲器中獲取活動的Session對象; 2.1)當Session對象被換出:他會被移動到存儲器中,因為當前活動的Session對象數超過了上限值,或者這個Session對象閑置了過長時間;(干貨——換出是為了節省內存空間) 2.2)tomcat4中,PersistentManagerBase類實現了java.lang.Runnable 接口:使用一個專門的線程來執行備份和換出活動Session對象的任務;(run() 方法如下 )(干貨——注意是tomcat4,使用一個專門的線程來執行備份和換出活動Session對象的任務) public void run() { //org.apache.catalina.session.PersistentManagerBase.run()while(!threadDone) {threadSleep();processExpires(); // 檢查Session對象是否過期.processPersistenceChecks();} } public void processPersistenceChecks(){ // org.apache.catalina.session.PersistentManagerBase.processPersistenceChecks()processMaxIdleSwaps(); // highlight line.(下面我們只以processMaxIdleSwaps方法為例進行調用過程的分析,其他兩個方法類似)processMaxActiveSwaps(); // highlight line.processMaxIdleBackups(); // highlight line. } /*** Swap idle sessions out to Store if they are idle too long.若session空閑太久則換出*/protected void processMaxIdleSwaps() {if (!isStarted() || maxIdleSwap < 0)return;Session sessions[] = findSessions();long timeNow = System.currentTimeMillis();// Swap out all sessions idle longer than maxIdleSwap// FIXME: What's preventing us from mangling a session during// a request?if (maxIdleSwap >= 0) {for (int i = 0; i < sessions.length; i++) {StandardSession session = (StandardSession) sessions[i];if (!session.isValid())continue;long lastAccessed = ((StandardSession)session).getLastUsedTime();int timeIdle = // Truncate, do not round up(int) ((timeNow - lastAccessed) / 1000L);if (timeIdle > maxIdleSwap && timeIdle > minIdleSwap) {if (debug > 1)log(sm.getString("persistentManager.swapMaxIdle",session.getId(), new Integer(timeIdle)));try {swapOut(session); // highlight line.} catch (IOException e) {; // This is logged in writeSession()}}}}}protected void swapOut(Session session) throws IOException { //換出操作if (store == null ||!session.isValid() ||isSessionStale(session, System.currentTimeMillis()))return;((StandardSession)session).passivate();writeSession(session); // highlight line.super.remove(session);session.recycle();}protected void writeSession(Session session) throws IOException {if (store == null ||!session.isValid() ||isSessionStale(session, System.currentTimeMillis()))return;try {store.save(session); //highlight line.sessionSwapIgnore.remove(session.getId());} catch (IOException e) {log(sm.getString("persistentManager.serializeError", session.getId(), e));throw e;}}
3)tomcat5中,PersistentManagerBase類不再實現 java.lang.Runnable 接口。備份和換出Session對象的任務由其 backgroundProcess() 管理器完成,后者由其相關聯的 StandardContext 實例周期性地調用; (下面分析 換出和備份Session實例的實現過程)
4)PersistentManagerBase類換出Session實例 4.1)換出條件:只有當活動Session對象的數量超過了變量 maxActiveSessions指定的上限值,或者該Session對象閑置了很長時間后,才會被換出;(干貨——Session對象被換出的condition) 4.1.1)當活動的Session實例數量過多時:PersistentManagerBase會將Session對象換出直到數量等于?maxActiveSessions;(參見 processMaxActiveSwaps()方法) 4.1.2)當某個Session對象閑置了過長時間:PersistentManagerBase會依據兩個變量來決定是否將這個Session換出,這兩個變量是minIdleSwap and maxIdleSwap;如果某個Session對象的 lastAccessedTime 屬性的值超過了 minIdleSwap 和 maxIdleSwap 的值,就會將這個Session換出;(干貨——為了防止換出Session對象,將maxIdleSwap設置為負數,參見 processMaxIdleSwaps() 方法); 4.2)如何查找Session實例:因為Session對象可以被換出,所以他既可能駐留在內存中,也可能駐留在存儲器中,因此,findSession(String id ) 方法首先會在內存中查找是否存在該Session對象,若沒有找到,才會在存儲器中繼續find。(PersistentManagerBase.findSession() 方法的源代碼如下)(干貨——Session對象被換出后,可能駐留在內存中,也可能駐留在存儲器中) public Session findSession(String id) throws IOException { //org.apache.catalina.session.PersistentManagerBase.findSession()Session session = super.findSession(id);if (session != null)return (session);// See if the Session is in the Storesession = swapIn(id); // highlight line.return (session);}protected Session swapIn(String id) throws IOException { // the same as the above.if (store == null)return null;if (sessionSwapIgnore.contains(id)) {return null;}Session session = null;try {session = store.load(id); // highlight line.} catch (ClassNotFoundException e) {log(sm.getString("persistentManager.deserializeError", id, e));throw new IllegalStateException(sm.getString("persistentManager.deserializeError", id, e));}if (session == null) {sessionSwapIgnore.put(id,id);return (null);}if (!session.isValid()|| isSessionStale(session, System.currentTimeMillis())) {log("session swapped in is invalid or expired");session.expire(); // highlight line.store.remove(id); // highlight line.sessionSwapIgnore.put(id,id);return (null);}if(debug > 2)log(sm.getString("persistentManager.swapIn", id));session.setManager(this);// To make sure the listener knows about it.((StandardSession)session).tellNew();add(session);((StandardSession)session).activate();return (session);}
5)PersistentManagerBase類備份Session實例 5.1)備份條件:PersistentManagerBase實例 僅僅會備份那些空閑時間超過了變量 maxIdleBackup 的Session對象。 5.2)processMaxIdleBackups()方法:負責對該Session對象進行備份操作;
【2.5】PersistentManager類 1)intro to?PersistentManager類:該類繼承了?PersistentManagerBase類,僅僅是多添加了兩個屬性而已; public final class PersistentManager extends PersistentManagerBase { //org.apache.catalina.session.PersistentManager private static final String info = "PersistentManager/1.0"; protected static String name = "PersistentManager"; public String getInfo() {return (this.info);} public String getName() {return (name);}} 【2.6】DistributedManager類(應用與集群環境) 1)intro to?DistributedManager類:繼承自PersistentManagerBase類,用于兩個或多個節點的集群環境; 1.1)一個節點表示部署的一臺 tomcat server:而集群中的節點 可以在同一臺機器上,也可以在不同的machine上; 1.2)每個節點必須使用 DistributedManager實例作為其Session管理器:這樣才能支持復制 Session對象,這也是DistributedManager的主要功能?;(干貨——DistributedManager的主要功能) 2)DistributedManager會向其他節點發送消息:為了實現copy Session對象的目的,當創建或銷毀Session對象時,DistributedManager會向其他節點發送消息;(干貨——DistributedManager會向其他節點發送消息) 2.2)而且:集群中的節點也必須能夠接受其他節點發送的消息。這樣,http 請求才能到達集群中的任意節點; 3)catalina在org.apache.catlina.cluster中提供了一些工具:這些工具用于 與集群中其他節點的DistributedManager實例發送和接收消息;其中,ClusterSender 類用于向集群中的其他節點發送消息,ClusterReceiver 則用于接收集群中其他節點發送的消息;(干貨——catalina在org.apache.catlina.cluster中提供了一些工具,如ClusterSender and ClusterReceiver) 4)DistributedManager.createSession()方法: 要創建一個Sesssion對象,存儲在當前?DistributedManager 實例中,并使用 ClusterSender實例向其他節點發送消息; public Session createSession() {Session session = super.createSession(); // step1.ObjectOutputStream oos = null;ByteArrayOutputStream bos = null;ByteArrayInputStream bis = null;try {bos = new ByteArrayOutputStream();oos = new ObjectOutputStream(new BufferedOutputStream(bos));((StandardSession)session).writeObjectData(oos);oos.close();byte[] obs = bos.toByteArray();clusterSender.send(obs); // step2.if(debug > 0)log("Replicating Session: "+session.getId());} catch (IOException e) {log("An error occurred when replicating Session: "+session.getId());}return (session);} 對以上代碼的分析(Analysis): step1)調用超類的 createSession()方法為自身創建一個 Session對象; step2)使用ClusterSender實例,以字節數組的形式將該Session對象發送到集群中的其他節點; 5)DistributedManager類還實現了 java.lang.Runnable 接口: 這樣就可以使用一個 專門的thread 來檢查 Session對象是否過期,并從集群中的其他節點接收消息;(其run() 方法的源代碼如下)? public void run() { // org.apache.catalina.session.DistributedManager.run()// Loop until the termination semaphore is setwhile (!threadDone) {threadSleep();processClusterReceiver(); // 以該方法為例進行說明.processExpires();processPersistenceChecks();}} /*** Called from our background thread to process new received Sessions**/public void processClusterReceiver() { //org.apache.catalina.session.DistributedManager.processClusterReceiver()Object[] objs = clusterReceiver.getObjects();StandardSession _session = null;ByteArrayInputStream bis = null;Loader loader = null;ClassLoader classLoader = null;ObjectInputStream ois = null;byte[] buf = new byte[5000];ReplicationWrapper repObj = null;for(int i=0; i < objs.length;i++) {try {bis = new ByteArrayInputStream(buf);repObj = (ReplicationWrapper)objs[i];buf = repObj.getDataStream();bis = new ByteArrayInputStream(buf, 0, buf.length);if (container != null)loader = container.getLoader();if (loader != null)classLoader = loader.getClassLoader();if (classLoader != null)ois = new CustomObjectInputStream(bis,classLoader);elseois = new ObjectInputStream(bis);_session = (StandardSession) super.createSession();_session.readObjectData(ois);_session.setManager(this);if (debug > 0)log("Loading replicated session: "+_session.getId());} catch (IOException e) {log("Error occurred when trying to read replicated session: "+e.toString());} catch (ClassNotFoundException e) {log("Error occurred when trying to read replicated session: "+e.toString());} finally {if (ois != null) {try {ois.close();bis = null;} catch (IOException e) {;}}}}} 【3】存儲器(存儲器是org.apache.catalina.Store接口的實例) 1)intro to 存儲器:是為Session管理器管理的 Session對象提供持久化存儲器的一個組件;(干貨——intro to 存儲器——org.apache.catalina.Store接口的實例) 2)Store接口的源代碼如下: public interface Store { // org.apache.catalina.Storepublic 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; public void removePropertyChangeListener(PropertyChangeListener listener); public void save(Session session) throws IOException; } 3)Store接口中有兩個方法:save()方法和 load() 方法(干貨——注意save方法和 load方法,它們的作用分別是什么) 3.1)save方法:用于將指定的 Session對象存儲到某種持久性存儲器中; 3.2)load方法:會從存儲器中,依據Session對象 的標識符將該Session對象載入(加載)到內存中; 補充)keys()方法:會以字符串數組的形式返回所有 Session 對象的標識符; 4)其UML 類圖如下所示:
【3.1】StoreBase類圖(見上圖) 1)intro to?StoreBase:該類是一個抽象類,提供了一些基本功能,有兩個子類; 2)StoreBase類:并沒有實現Store接口的save()方法 和 load()方法,因為,這兩個方法依賴于持久化Session對象的存儲器的類型; 3)在tomcat4中:StoreBase類使用另一個線程周期性地檢查 Session對象:從活動的Session的集合中移除過期的Session對象;(下面是 tomcat4 中 StoreBase.run()方法的源代碼,注意是tomcat4) public void run() { // org.apache.catalina.session.StoreBase.run()// Loop until the termination semaphore is setwhile (!threadDone) {threadSleep();processExpires();}} 對以上代碼的分析(Analysis): A1)processExpires方法:會獲取所有活動的 Session對象,檢查 每個Session對象的 lastAccessedTime屬性值,刪除那些長時間不活動的Session對象;源代碼如下:(干貨——再次提及了processExpires方法,注意其功能) protected void processExpires() {// org.apache.catalina.session.StoreBase.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 == null) {continue;}if (!session.isValid()) {continue;}int maxInactiveInterval = session.getMaxInactiveInterval();if (maxInactiveInterval < 0) {continue;}int timeIdle = // Truncate, do not round up(int) ((timeNow - session.getLastUsedTime()) / 1000L);if (timeIdle >= maxInactiveInterval) {if ( ( (PersistentManagerBase) manager).isLoaded( keys[i] )) {// recycle old backup sessionsession.recycle();} else {// expire swapped out sessionsession.expire();}remove(session.getId());}} catch (Exception e) {log ("Session: "+keys[i]+"; "+e.toString());try {remove(keys[i]);} catch (IOException e2) {log (e2.toString());e2.printStackTrace();}}}}
Attention)在tomcat5中,不再使用專用的線程調用 processExpire()方法 ,而是,相關聯的 PersistentManagerBase實例的 backgroundProcess()方法會周期性地調用 processExpires() 方法;
【3.2】FileStore類 1)intro to FileStore:FileStore類會將 Session對象存儲到某個文件中,文件名會使用Session對象的 標識符再加上一個后綴 ".session" 構成;(干貨——FileStore類的作用) 2)文件位于臨時的工作dir下:可以調用FileStore.setDirectory() 方法進行修改臨時目錄的位置; (干貨——理清FileStore.save and FileStore.load方法的作用)
3)save方法:使用 java.io.ObjectOutputStream類將 Session對象進行序列化,故所有存儲在Session實例中的對象都要實現 java.lang.Serializable接口; 4)load方法:該方法使用 java.io.ObjectInputStream 類實現 Session 對象的反序列化;
【3.3】JDBCStore類 1)intro to JDBCStore:該類將Session對象通過JDBC 存入 db中;(干貨——JDBCStore類的作用)
【4】app program 1)intro to app program:該應用程序中的Context容器使用StandardManager實例來管理Session對象,為了測試該應用程序,可以使用第3個示例servlet:SessionServlet; public class SessionServlet extends HttpServlet {public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {System.out.println("SessionServlet -- service");response.setContentType("text/html");PrintWriter out = response.getWriter();out.println("<html>");out.println("<head><title>SessionServlet</title></head>");out.println("<body>");String value = request.getParameter("value");HttpSession session = request.getSession(true);out.println("<br>the previous value is " +(String) session.getAttribute("value"));out.println("<br>the current value is " + value);session.setAttribute("value", value);out.println("<br><hr>");out.println("<form>");out.println("New Value: <input name=value>");out.println("<input type=submit>");out.println("</form>");out.println("</body>");out.println("</html>");} }
【4.1】Bootstrap類 1)源代碼如下
public static void main(String[] args) {//invoke: http://localhost:8080/myApp/SessionSystem.setProperty("catalina.base", System.getProperty("user.dir"));Connector connector = new HttpConnector();Wrapper wrapper1 = new SimpleWrapper();wrapper1.setName("Session");wrapper1.setServletClass("SessionServlet");Context context = new StandardContext();// StandardContext's start method adds a default mappercontext.setPath("/myApp");context.setDocBase("myApp");context.addChild(wrapper1);// context.addServletMapping(pattern, name);// note that we must use /myApp/Session, not just /Session// because the /myApp section must be the same as the path, so the cookie will// be sent back.context.addServletMapping("/myApp/Session", "Session");// add ContextConfig. This listener is important because it configures// StandardContext (sets configured to true), otherwise StandardContext// won't startLifecycleListener listener = new SimpleContextConfig();((Lifecycle) context).addLifecycleListener(listener);// here is our loaderLoader loader = new WebappLoader();// associate the loader with the Contextcontext.setLoader(loader);connector.setContainer(context);// add a ManagerManager manager = new StandardManager(); // highlight line.context.setManager(manager); // highlight line.try {connector.initialize();((Lifecycle) connector).start();((Lifecycle) context).start();// make the application wait until we press a key.System.in.read();((Lifecycle) context).stop();}catch (Exception e) {e.printStackTrace();}} } 【4.2】SimpleWrapperValve類 1)servlet示例可以調用javax.servlet.http.HttpServletRequest接口的getSession() 方法獲取Session對象:當調用 getSession() 方法時,request對象必須調用與Context 容器相關聯的Session管理器。Session管理器組件要么創建一個新的session 對象,要么返回一個已經存在的session對象。request對象為了能夠訪問Session管理器,他必須能夠訪問Context容器。 2)為了達到這個目的:在 SimpleWrapperValve類的invoke方法中,需要調用 org.apache.catalina.Request接口的setContext()方法,并傳入 Context實例; Attention)SimpleWrapperValve.invoke方法會調用被請求的servlet實例的service方法,所以,必須在調用service方法之前設置 Context實例; public void invoke(Request request, Response response, ValveContext valveContext)throws IOException, ServletException {SimpleWrapper wrapper = (SimpleWrapper) getContainer();ServletRequest sreq = request.getRequest();ServletResponse sres = response.getResponse();Servlet servlet = null;HttpServletRequest hreq = null;if (sreq instanceof HttpServletRequest)hreq = (HttpServletRequest) sreq;HttpServletResponse hres = null;if (sres instanceof HttpServletResponse)hres = (HttpServletResponse) sres;//-- new addition -----------------------------------Context context = (Context) wrapper.getParent(); // highlight line.request.setContext(context); // highlight line.//-------------------------------------// Allocate a servlet instance to process this requesttry {servlet = wrapper.allocate();if (hres!=null && hreq!=null) {servlet.service(hreq, hres);}else {servlet.service(sreq, sres);}}catch (ServletException e) {}} 補充)org.apache.catalina.connector.HttpRequestBase類的私有方法:doGetSession() 會調用Context接口的 getManager() 方法來獲取 Session管理器對象:(當獲取了Session管理器對象之后,就可以獲取到Session對象了,或直接創建一個新Session) Manager manager = null; if(contect != null)manager = context.getManager();
【4.3】打印結果 1)console info E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src>java -cp .;lib/servlet.jar;lib/catalina_4_1_24.jar;lib/catalina-5.5.4.jar;lib/naming-common. jar;lib/commons-collections.jar;lib/naming-resources.jar;lib/;lib/catalina.jar;E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\webroot com/tomca t/chapter9/startup/Bootstrap HttpConnector Opening server socket on all host IP addresses HttpConnector[8080] Starting background thread WebappLoader[/myApp]: Deploying class repositories to work directory E:\bench-cluster\cloud-data-preprocess\HowTomcatWorks\src\work\_\_\myApp Starting Wrapper Session StandardManager[/myApp]: Seeding random number generator class java.security.SecureRandom StandardManager[/myApp]: Seeding of random number generator has been completed SessionServlet -- service SessionServlet -- service SessionServlet -- service SessionServlet -- service 2)訪問信息
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎
總結
以上是生活随笔為你收集整理的tomcat(9)Session管理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 大脚插件如何修改快捷键(大脚插件快捷图标
- 下一篇: tomcat(supplement)Ht