Tomcat的Session管理(二) - Session后台处理
2016-02-04 11:26
513 查看
Tomcat会开启一个后台线程每隔一段时间检查Session的有效性,这个线程是在Tomcat启动的时候当StardardEngine启动时随之启动的。可以参看StardardEngine的基类ContainerBase的#threadStart()方法:
Java代码
protected void threadStart() {
if (thread != null)
return;
if (backgroundProcessorDelay <= 0)
return;
threadDone = false;
String threadName = "ContainerBackgroundProcessor[" + toString() + "]";
// 开启后台的监控线程
thread = new Thread(new ContainerBackgroundProcessor(), threadName);
thread.setDaemon(true);
thread.start();
}
这个方法启动了一个ContainerBackgroundProcessor类的线程,这个类重写的#run()方法中包括了对Session的有效性监控。具体的细节就不详细陈述了。每隔一段时间,此线程就会启动一次并调用了ManageBase的#backgroundProcess()方法。其源代码如下:
Java代码
public void backgroundProcess() {
count = (count + 1) % processExpiresFrequency;
if (count == 0)
processExpires();
}
每隔一段时间就会调用processExpires()方法去判断Session的有效性。
Java代码
public void processExpires() {
// 现在的时间
long timeNow = System.currentTimeMillis();
// 所有的Session对象
Session sessions[] = findSessions();
int expireHere = 0;
if (log.isDebugEnabled())
log.debug("Start expire sessions " + getName() + " at " + timeNow + " sessioncount "
+ sessions.length);
for (int i = 0; i < sessions.length; i++) {
// 判断并设定Session是否失效
if (sessions[i] != null && !sessions[i].isValid()) {
expireHere++;
}
}
long timeEnd = System.currentTimeMillis();
if (log.isDebugEnabled())
log.debug("End expire sessions " + getName() + " processingTime " + (timeEnd - timeNow)
+ " expired sessions: " + expireHere);
processingTime += (timeEnd - timeNow);
}
此方法最终调用了isValid()去判断和设定Session是否失效,源代码如下所示:
Java代码
public boolean isValid() {
// 是否过期
if (this.expiring) {
return true;
}
// 是否有效
if (!this.isValid) {
return false;
}
// 正在使用中并且访问数大于0
if (ACTIVITY_CHECK && accessCount.get() > 0) {
return true;
}
if (maxInactiveInterval >= 0) {
// 判断Session是否过期
long timeNow = System.currentTimeMillis();
int timeIdle = (int) ((timeNow - thisAccessedTime) / 1000L);
if (timeIdle >= maxInactiveInterval) {
// 设定Session过期
expire(true);
}
}
return (this.isValid);
}
转载自:http://zddava.iteye.com/blog/311136
Java代码
protected void threadStart() {
if (thread != null)
return;
if (backgroundProcessorDelay <= 0)
return;
threadDone = false;
String threadName = "ContainerBackgroundProcessor[" + toString() + "]";
// 开启后台的监控线程
thread = new Thread(new ContainerBackgroundProcessor(), threadName);
thread.setDaemon(true);
thread.start();
}
这个方法启动了一个ContainerBackgroundProcessor类的线程,这个类重写的#run()方法中包括了对Session的有效性监控。具体的细节就不详细陈述了。每隔一段时间,此线程就会启动一次并调用了ManageBase的#backgroundProcess()方法。其源代码如下:
Java代码
public void backgroundProcess() {
count = (count + 1) % processExpiresFrequency;
if (count == 0)
processExpires();
}
每隔一段时间就会调用processExpires()方法去判断Session的有效性。
Java代码
public void processExpires() {
// 现在的时间
long timeNow = System.currentTimeMillis();
// 所有的Session对象
Session sessions[] = findSessions();
int expireHere = 0;
if (log.isDebugEnabled())
log.debug("Start expire sessions " + getName() + " at " + timeNow + " sessioncount "
+ sessions.length);
for (int i = 0; i < sessions.length; i++) {
// 判断并设定Session是否失效
if (sessions[i] != null && !sessions[i].isValid()) {
expireHere++;
}
}
long timeEnd = System.currentTimeMillis();
if (log.isDebugEnabled())
log.debug("End expire sessions " + getName() + " processingTime " + (timeEnd - timeNow)
+ " expired sessions: " + expireHere);
processingTime += (timeEnd - timeNow);
}
此方法最终调用了isValid()去判断和设定Session是否失效,源代码如下所示:
Java代码
public boolean isValid() {
// 是否过期
if (this.expiring) {
return true;
}
// 是否有效
if (!this.isValid) {
return false;
}
// 正在使用中并且访问数大于0
if (ACTIVITY_CHECK && accessCount.get() > 0) {
return true;
}
if (maxInactiveInterval >= 0) {
// 判断Session是否过期
long timeNow = System.currentTimeMillis();
int timeIdle = (int) ((timeNow - thisAccessedTime) / 1000L);
if (timeIdle >= maxInactiveInterval) {
// 设定Session过期
expire(true);
}
}
return (this.isValid);
}
转载自:http://zddava.iteye.com/blog/311136
相关文章推荐
- Tomcat的Session管理(一) - Session的生成
- Web服务访问慢解决方案
- 细说部分servlet知识点(会陆续补充)
- 细说Tomcat服务器目录
- 微信派运行tomcat页面访问不了
- 错误记录--更改tomcat端口号方法,Several ports (8005, 8080, 8009)
- 初学Java Web,搭建开发环境(Eclipse+Tomcat+MySQL)
- 设置tomcat的默认jdk
- 基于 Eclipse + Tomcat 的 Java Servlet Web 开发环境搭建与配置
- virgo tomcat下载
- 同一Tomcat 多个端口部署不同的项目
- 关于使用线程池造成tomcat进程无法正常退出的一点思考
- 搭建nginx+tomcat+Java的负载均衡环境
- 配置Tomcat/部署工程/工程名及应用名修改/代理服务器
- J2EE开发 eclipse中将web工程部署到Tomcat的webApps目录下
- Windows下各种java相关开发环境安装Tomcat+Ant
- tomcat日志配置,log信息如何输出到同一个文件
- tomcat 源码学习
- apache代理多个tomcat实例
- linux查看Tomcat连接数、统计某字段出现的次数