Chinaunix首页 | 论坛 | 博客
  • 博客访问: 6535593
  • 博文数量: 915
  • 博客积分: 17977
  • 博客等级: 上将
  • 技术积分: 8846
  • 用 户 组: 普通用户
  • 注册时间: 2005-08-26 09:59
个人简介

一个好老好老的老程序员了。

文章分类

全部博文(915)

文章存档

2022年(9)

2021年(13)

2020年(10)

2019年(40)

2018年(88)

2017年(130)

2015年(5)

2014年(12)

2013年(41)

2012年(36)

2011年(272)

2010年(1)

2009年(53)

2008年(65)

2007年(47)

2006年(81)

2005年(12)

分类: Java

2011-06-07 21:50:35

Tomcat的Session管理(二) - Session后台处理
Tomcat会开启一个后台线程每隔一段时间检查Session的有效性,这个线程是在Tomcat启动的时候当StardardEngine启动时随之启动的。可以参看StardardEngine的基类ContainerBase的#threadStart()方法:

Java代码 复制代码 收藏代码
  1. protected void threadStart() {   
  2.     if (thread != null)   
  3.         return;   
  4.     if (backgroundProcessorDelay <= 0)   
  5.         return;   
  6.        
  7.     threadDone = false;   
  8.     String threadName = "ContainerBackgroundProcessor[" + toString() + "]";   
  9.     // 开启后台的监控线程   
  10.     thread = new Thread(new ContainerBackgroundProcessor(), threadName);   
  11.     thread.setDaemon(true);   
  12.     thread.start();   
  13.   
  14. }  
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代码 复制代码 收藏代码
  1. public void backgroundProcess() {   
  2.     count = (count + 1) % proces***piresFrequency;   
  3.     if (count == 0)   
  4.         proces***pires();   
  5. }  
public void backgroundProcess() { count = (count + 1) % proces***piresFrequency; if (count == 0) proces***pires(); }

每隔一段时间就会调用proces***pires()方法去判断Session的有效性。

Java代码 复制代码 收藏代码
  1. public void proces***pires() {   
  2.     // 现在的时间   
  3.     long timeNow = System.currentTimeMillis();   
  4.     // 所有的Session对象   
  5.     Session sessions[] = findSessions();   
  6.     int expireHere = 0;   
  7.   
  8.     if (log.isDebugEnabled())   
  9.         log.debug("Start expire sessions " + getName() + " at " + timeNow + " sessioncount "  
  10.                 + sessions.length);   
  11.     for (int i = 0; i < sessions.length; i++) {   
  12.         // 判断并设定Session是否失效   
  13.         if (sessions[i] != null && !sessions[i].isValid()) {   
  14.             expireHere++;   
  15.         }   
  16.     }   
  17.     long timeEnd = System.currentTimeMillis();   
  18.     if (log.isDebugEnabled())   
  19.         log.debug("End expire sessions " + getName() + " processingTime " + (timeEnd - timeNow)   
  20.                 + " expired sessions: " + expireHere);   
  21.     processingTime += (timeEnd - timeNow);   
  22.   
  23. }  
public void proces***pires() { // 现在的时间 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代码 复制代码 收藏代码
  1. public boolean isValid() {   
  2.     // 是否过期   
  3.     if (this.expiring) {   
  4.         return true;   
  5.     }   
  6.     // 是否有效   
  7.     if (!this.isValid) {   
  8.         return false;   
  9.     }   
  10.     // 正在使用中并且访问数大于0   
  11.     if (ACTIVITY_CHECK && accessCount.get() > 0) {   
  12.         return true;   
  13.     }   
  14.   
  15.     if (maxInactiveInterval >= 0) {   
  16.         // 判断Session是否过期   
  17.         long timeNow = System.currentTimeMillis();   
  18.         int timeIdle = (int) ((timeNow - thisAccessedTime) / 1000L);   
  19.         if (timeIdle >= maxInactiveInterval) {   
  20.             // 设定Session过期   
  21.             expire(true);   
  22.         }   
  23.     }   
  24.   
  25.     return (this.isValid);   
  26. }  
阅读(885) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~