com.tianque.session; java.util.Date; java.util.HashMap; java.util.Map; javax.servlet.ServletContext; javax.servlet.http.HttpSession; javax.servlet.http.HttpSessionContext; com.tianque.session.zookeeper.ZkSessionChangeListener; com.tianque.session.zookeeper.ZkSessionHelper; HttpSession { SessionMetaData meta; AbstractSession(){ meta SessionMetaData(); meta.setCreateTime( Date().getTime()); meta.setLastAccessedTime(meta.getCreateTime()); meta.setIsnew(); meta.setMaxInactiveInterval(()SessionChangeListener.getTimeout()); } setLastAccessedTime( lastAccessedTime) { meta.setLastAccessedTime(lastAccessedTime); } getCreationTime() { meta.getCreateTime(); } getId() { meta.getSid(); } setId( sid){ meta.setSid(sid); } getLastAccessedTime() { meta.getLastAccessedTime(); } ServletContext getServletContext() { ; } setMaxInactiveInterval( interval) { meta.setMaxInactiveInterval(interval); } getMaxInactiveInterval() { meta.getMaxInactiveInterval(); } HttpSessionContext getSessionContext() { ; } invalidate() { } isNew() { meta.isIsnew(); } SessionMetaData getMeta() { meta; } setMeta(SessionMetaData meta) { .meta meta; } }
com.tianque.session; java.io.IOException; java.util.Date; javax.servlet.Filter; javax.servlet.FilterChain; javax.servlet.FilterConfig; javax.servlet.ServletException; javax.servlet.ServletRequest; javax.servlet.ServletResponse; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; Filter{ init(FilterConfig filterConfig) ServletException { } doFilter(ServletRequest request, ServletResponse response, FilterChain chain) IOException, ServletException { doFilterInternal((HttpServletRequest)request,(HttpServletResponse)response); chain.doFilter(request, response); } doFilterInternal(HttpServletRequest request,HttpServletResponse response); destroy() { } }
com.tianque.session; java.util.HashMap; java.util.Map; javax.servlet.http.Cookie; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpSession; { Map sessions HashMap(); AbstractSessionManager(){} HttpSession getSession( sid){ (HttpSession)sessions.get(sid); } addSession(HttpSession session, sid){ .sessions.put(sid, session); } loadSession(); Map getAllSession(){ sessions; } getSessionIdByCookie(HttpServletRequest request){ Cookie[] cookies request.getCookies(); (cookies ) { ; } ( i ; i cookies.length; i) { Cookie cookie cookies[i]; (.equals(cookie.getName())) { sid cookie.getValue(); sid; } } ; } }
com.tianque.session; javax.servlet.ServletContextEvent; javax.servlet.ServletContextListener; ServletContextListener { timeout; getTimeout(){ timeout; } contextInitialized(ServletContextEvent sce) { timeoutStrsce.getServletContext().getInitParameter(); (timeoutStrtimeoutStr.equals()){ timeout.parseLong(timeoutStr); }{ timeout; } subscribeSession(); } contextDestroyed(ServletContextEvent sce) { release(); } subscribeSession(); release(); }
com.tianque.session; java.io.Serializable; javax.servlet.ServletContext; Serializable{ createTime; sid; lastAccessedTime; maxInactiveInterval; isnew; getCreateTime() { createTime; } setCreateTime( createTime) { .createTime createTime; } getSid() { sid; } setSid( sid) { .sid sid; } getLastAccessedTime() { lastAccessedTime; } setLastAccessedTime( lastAccessedTime) { .lastAccessedTime lastAccessedTime; } getMaxInactiveInterval() { maxInactiveInterval; } setMaxInactiveInterval( maxInactiveInterval) { .maxInactiveInterval maxInactiveInterval; } isIsnew() { isnew; } setIsnew( isnew) { .isnew isnew; } }
com.tianque.session; java.util.UUID; { generateSid(){ UUID.randomUUID().toString(); } }
com.tianque.session.zookeeper; org.I0Itec.zkclient.ZkClient; { zkServer ; ZkClient zkClient ZkClient(zkServer); ZkClient getInstance(){ zkClient; } }
com.tianque.session.zookeeper; java.util.Enumeration; java.util.HashMap; java.util.Map; javax.servlet.http.HttpServletRequest; com.tianque.session.AbstractSession; com.tianque.session.AbstractSessionManager; AbstractSession { Map attributes HashMap(); getAttribute( name) { .attributes.get(name); } getValue( name) { ; } Enumeration getAttributeNames() { ; } [] getValueNames() { ; } setAttribute( name, value) { .attributes.put(name, value); ZkSessionHelper.setAttribute(.getId(), name, value); } putValue( name, value) { } removeAttribute( name) { .attributes.remove(name); ZkSessionHelper.removeAttribute(.getId(), name); } removeValue( name) { } localSetAttribute( name, value){ .attributes.put(name, value); } }
com.tianque.session.zookeeper; org.I0Itec.zkclient.IZkDataListener; IZkDataListener { handleDataChange( arg0, arg1) Exception { namearg0.substring(arg0.lastIndexOf()); valuearg1; prefixarg0.substring(,arg0.lastIndexOf()); sidprefix.substring(prefix.lastIndexOf()); ((ZkSession)ZkSessionManager.getInstance().getSession(sid)).localSetAttribute(name, value); } handleDataDeleted( arg0) Exception { } }
com.tianque.session.zookeeper; java.util.HashMap; java.util.HashSet; java.util.Iterator; java.util.List; java.util.Map; java.util.Set; org.I0Itec.zkclient.IZkChildListener; org.I0Itec.zkclient.IZkDataListener; org.I0Itec.zkclient.ZkClient; com.tianque.session.SessionChangeListener; com.tianque.session.SessionMetaData; SessionChangeListener { Map, SetIZkDataListener sissionDataListeners HashMap, SetIZkDataListener(); Map,Map, SetIZkDataListener attributeDataListeners HashMap,Map, SetIZkDataListener(); subscribeSessionAttributeChange(List zkSessions){ ZkClient zkClientZkConnectionSingleton.getInstance(); ( i,lenzkSessions.size();ilen;i){ sid()zkSessions.get(i); List zkSessionAttributeszkClient.getChildren(ZkSessionHelper.rootsid); Map, SetIZkDataListener attributesDataListenerattributeDataListeners.get(sid); ( j,sizezkSessionAttributes.size();jsize;j){ namezkSessionAttributes.get(j); IZkDataListener newDataListener ZkSessionAttributeDataListener(); (attributesDataListener){ attributesDataListener HashMap, SetIZkDataListener(); SetIZkDataListener attributeDataListener HashSetIZkDataListener(); attributeDataListener.add(newDataListener); attributesDataListener.put(name, attributeDataListener); attributeDataListeners.put(sid, attributesDataListener); }{ SetIZkDataListener attributeDataListenerattributesDataListener.get(name); (attributeDataListener){ IteratorIZkDataListener itattributeDataListener.iterator(); (it.hasNext()){ zkClient.unsubscribeDataChanges(ZkSessionHelper.rootsidname, it.next()); } }{ attributeDataListener HashSetIZkDataListener(); attributeDataListener.add(newDataListener); } } zkClient.subscribeDataChanges(ZkSessionHelper.rootsidname,newDataListener ); } } } subscribeSessionDataChange(List zkSessions){ ZkClient zkClientZkConnectionSingleton.getInstance(); ( i,lenzkSessions.size();ilen;i){ sid()zkSessions.get(i); SetIZkDataListener listenerSetsissionDataListeners.get(sid); (listenerSet){ IteratorIZkDataListener itlistenerSet.iterator(); (it.hasNext()){ zkClient.unsubscribeDataChanges(ZkSessionHelper.rootsid, it.next()); } } IZkDataListener newDataListener ZkSessionDataListener(); (listenerSet){ listenerSet HashSetIZkDataListener(); listenerSet.add(newDataListener); sissionDataListeners.put(sid, listenerSet); }{ listenerSet.add(newDataListener); } zkClient.subscribeDataChanges(ZkSessionHelper.rootsid,newDataListener ); } subscribeSessionAttributeChange(zkSessions); } subscribeSession() { ZkClient zkClientZkConnectionSingleton.getInstance(); (zkClient.exists(ZkSessionHelper.root)){ zkClient.createPersistent(ZkSessionHelper.root); } ZkSessionManager.getInstance().loadSession(); ZkSessionCleaner().start(); List zkSessionszkClient.getChildren(ZkSessionHelper.root); subscribeSessionDataChange(zkSessions); zkClient.subscribeChildChanges(ZkSessionHelper.root, IZkChildListener() { handleChildChange( parentPath, List currentChilds) Exception { subscribeSessionDataChange(currentChilds); Map sessionsZkSessionManager.getInstance().getAllSession(); ( sid: sessions.keySet()){ has; ( j, lencurrentChilds.size();jlen;j){ ((()sid).equals(currentChilds.get(j))){ has; ; } } (has){ sessions.remove(sid); } } ( j, lencurrentChilds.size();jlen;j){ has; zkSid()currentChilds.get(j); ( sid: sessions.keySet()){ ((()sid).equals(zkSid)){ has; ; } } (has){ SessionMetaData metazkClient.readData(ZkSessionHelper.rootzkSid); ZkSession session ZkSession(); session.setMeta(meta); ZkSessionManager.getInstance().addSession(session, session.getId()); List keyszkClient.getChildren(ZkSessionHelper.rootzkSid); ( i,sizekeys.size();isize;i){ valzkClient.readData(keys.get(i)); session.localSetAttribute(keys.get(i), val); } } } } }); } release() { } }
com.tianque.session.zookeeper; java.util.Date; java.util.List; org.I0Itec.zkclient.ZkClient; com.tianque.session.SessionMetaData; Thread { run() { ZkClient clientZkConnectionSingleton.getInstance(); (){ List sessionListclient.getChildren(ZkSessionHelper.root); ( i,lensessionList.size();ilen;i){ sidsessionList.get(i); SessionMetaData metaclient.readData(ZkSessionHelper.rootsid); ZkSession session ZkSession(); (( Date().getTime() meta.getLastAccessedTime())meta.getMaxInactiveInterval()){ client.deleteRecursive(ZkSessionHelper.rootsid); } } { Thread.sleep(); } (InterruptedException e) { } } } }
com.tianque.session.zookeeper; org.I0Itec.zkclient.IZkDataListener; org.I0Itec.zkclient.ZkClient; com.tianque.session.SessionMetaData; IZkDataListener { handleDataChange( arg0, arg1) Exception { ZkClient clientZkConnectionSingleton.getInstance(); sidarg0.substring(arg0.lastIndexOf()); ZkSession session(ZkSession)ZkSessionManager.getInstance().getSession(sid); SessionMetaData metaclient.readData(arg0); session.setMeta(meta); } handleDataDeleted( arg0) Exception { } }
com.tianque.session.zookeeper; java.util.Date; javax.servlet.http.Cookie; javax.servlet.http.HttpServletRequest; javax.servlet.http.HttpServletResponse; javax.servlet.http.HttpSession; org.I0Itec.zkclient.ZkClient; com.tianque.session.AbstractSession; com.tianque.session.AbstractSessionFilter; com.tianque.session.AbstractSessionManager; com.tianque.session.SessionChangeListener; com.tianque.session.SidGenerator; AbstractSessionFilter { newSession(HttpServletRequest request, HttpServletResponse response) { HttpSession session ZkSession(); sidSidGenerator.generateSid(); ((AbstractSession)session).setId(sid); ZkSessionManager.getInstance().addSession(session, sid); ZkSessionHelper.addSession(((AbstractSession)session).getMeta()); Cookie cookie Cookie(,sid); cookie.setMaxAge(()SessionChangeListener.getTimeout()); response.addCookie(cookie); request.setAttribute(, sid); } doFilterInternal(HttpServletRequest request, HttpServletResponse response) { AbstractSessionManager sessionManagerZkSessionManager.getInstance(); sidsessionManager.getSessionIdByCookie((HttpServletRequest)request); (sid sid.equals()){ newSession((HttpServletRequest)request,(HttpServletResponse)response); }{ AbstractSession session(AbstractSession)sessionManager.getSession(sid); (session){ session.setLastAccessedTime( Date().getTime()); ZkClient clientZkConnectionSingleton.getInstance(); client.writeData(ZkSessionHelper.rootsid, session.getMeta()); }{ newSession((HttpServletRequest)request,(HttpServletResponse)response); } } } }
com.tianque.session.zookeeper; org.I0Itec.zkclient.ZkClient; com.tianque.session.SessionMetaData; { root; setAttribute( sid, name, value){ ZkClient clientZkConnectionSingleton.getInstance(); (client.exists(rootsidname)){ client.createPersistent(rootsidname); } client.writeData(rootsidname, value); } removeAttribute( sid, name){ ZkClient clientZkConnectionSingleton.getInstance(); (client.exists(rootsidname)){ client.delete(rootsidname); } } addSession(SessionMetaData meta){ ZkClient clientZkConnectionSingleton.getInstance(); client.createPersistent(rootmeta.getSid()); client.writeData(rootmeta.getSid(), meta); } }
com.tianque.session.zookeeper; java.util.List; org.I0Itec.zkclient.ZkClient; com.tianque.session.AbstractSessionManager; com.tianque.session.SessionMetaData; AbstractSessionManager { AbstractSessionManager instance ZkSessionManager(); loadSession() { ZkClient clientZkConnectionSingleton.getInstance(); List sessionListclient.getChildren(ZkSessionHelper.root); ( i,lensessionList.size();ilen;i){ sidsessionList.get(i); SessionMetaData metaclient.readData(ZkSessionHelper.rootsid); ZkSession session ZkSession(); session.setId(sid); session.setMeta(meta); List attributeListclient.getChildren(ZkSessionHelper.rootsid); ( j,sizeattributeList.size();jsize;j){ nameattributeList.get(j); valueclient.readData(ZkSessionHelper.rootsidname); session.localSetAttribute(name, value); } AbstractSessionManager sessionManagerZkSessionManager.getInstance(); sessionManager.addSession(session, sid); } } AbstractSessionManager getInstance(){ instance; } }