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就都懂了。实现没什么说的。
明天交上一个 测试代码。
- Tomcat从零开始(十四)session的store
- Tomcat从零开始(十五)session管理的代码
- Tomcat从零开始(十三)Session第二部分
- 从零开始学Makefile(十四)
- Tomcat从零开始(十二)Session第一部分
- Tomcat从零开始(一)
- Tomcat从零开始(五)
- 在Apache Tomcat 7设置redis作为session store
- Tomcat从零开始(六)容器
- Tomcat从零开始(八)Lifecycle
- Tomcat从零开始(九)Logger
- Tomcat从零开始(十)Loader
- Tomcat从零开始(二)支持servlet的服务器
- 从零开始学android<android事件的处理方式.二十四.>
- Tomcat从零开始(七)tomcat容器概述
- Tomcat Session的管理
- Tomcat 的session管理
- Tomcat的Session管理
- ASCII码对照表(未雨绸缪,以备后患)
- Oracle中游标使用效率比较
- java排序之伍------插入排序代码
- reDuh建立HTTP隧道
- 关于冒泡的总结
- Tomcat从零开始(十四)session的store
- WPF-24:绘制正多边形
- UVA - 662 Fast Food
- 属性的声明(NMTOKEN的使用)
- 黑马程序员——正则表达式
- VirtualWire library for Arduino and other boards
- 网络子系统1_socket文件系统相关的初始化
- 有道理,却又不去做
- Python遍历字典的几种方式