Tomcat如何监控并删除超时Session详解

发布时间:2022-04-24 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Tomcat如何监控并删除超时Session详解脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。

前言

偶然发现Tomcat会话时间的小时,并不是说会话创建后,只有半小时的有效使用时间,而是说会话空闲半小时后,会被删除。索性就翻了一下码。做了一番整理。

注:空闲时间,指的是同一个会话两次请求之间的间隔时间

Session相关类图


  • HttpSession就是大家Servlet层可以直接使用的Session.
  • Session是Tomcat内部使用的接口,可以做一些内部调用
  • Standardsession是标准的HttpSession实现,同时它也实现了Session接口,用于Tomcat内部管理
  • StandardSessionFaCADe,类名已经指明它就是一个“门面类”,它内部会引用一个StandardSession的对象,但对外只提供HttpSession规定的方法。

Manager相关类图


StandardManager与PErsITentManager都是Manager的实现,但是它们在存储Session对象的方式上有所不同。

StandarManager

1.Tomcat运行时,把Session存储在内存中

2.Tomcat关闭时(注意是正常的关闭操作,而非突然崩溃),会把Session写入到磁盘中,等到Tomcat重启后再把Session加载进来

PersistentManager

1.总是把Session存储在磁盘中。

Manager与Context的关系

在Tomcat中,一个Context就是部署到Tomcat中的一个应用(Webapp)。每一个Context都有一个单独的Manager对象来管理这个应用的会话信息。

Manager如何存储Session

Manager对象会使用一个Map来存储Session对象

  • Key  => SessionId
  • Value  => Session Object
 /**
  * The set of currently active Sessions for this Manager, keyed by
  * session identifier.
  */
 PRotected Map<String, Session> sessions = new ConcurrentHashMap<>();

当一个请求到达Context的时候,如果它带有JSESSIONID的Cookie,Manager就能依此找到关联的Session对象,放入到Request对象中。

Manager的定期检查

Manager接口有一个backgroundProcess()方法,顾名思义就是后台处理。

/**
  * This method will be invoked by the context/container on a periodic
  * basis and allows the manager to implement
  * a method that executes periodic tasks, such as expiring sessions etc.
  */
 public void backgroundProcess();

注:Container接口也有这个方法,这个方法一般在容器启动(start)的时候,开启一个额外的线程来执行这个backgroundProcess方法。其中Context的这个方法启动后,会执行Loader和Manager的backgroundProcess方法。

我们来看看这个方法都做了些什么?

/**
 * {@inheritDoc}
 * <p>
 * Direct call to {@link #processExpires()}
 */
@override
public void backgroundProcess() {
 count = (count + 1) % processExpiresFrequency;
 if (count == 0) //如果达到检查频率则开始检查
  processExpires();
}

/**
 * Invalidate all sessions that have expired.
 */
public void processExpires() {

 long timeNow = System.currentTimeMillis();
 Session sessions[] = findSessions(); //获取所有session对象
 int expireHere = 0 ; //过期session的数量,不要被这个变量名骗了

 if(LOG.isDebugEnabled())
  log.debug("Start expire sessions " + getName() + " at " + timeNow + " sessioncount " + sessions.length);
 for (int i = 0; i < sessions.length; i++) {
  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 );

}

很多人看到这里,可能会有跟我一样的疑惑,即这里面根本就没有使Session过期失效的操作,好像只做了状态检查。不过后来看到了Session的isValid方法的实现就都明白了。

/**
 * Return the <code>isValid</code> flag for this session.
 */
@Override
public boolean isValid() {

 if (!this.isValid) {
  return false;
 }

 if (this.expiring) {
  return true;
 }

 if (ACTIVITY_CHECK && accessCount.get() > 0) {
  return true;
 }

 //关键所在
 //如果有设置最大空闲时间
 //就获取此Session的空闲时间进行判断
 //如果已超时,则执行expire操作
 if (maxInactiveinterval > 0) { 
  int timeidle = (int) (getIdleTimeInternal() / 1000L);
  if (timeIdle >= maxInactiveInterval) {
   expire(true);
  }
 }

 return this.isValid;
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对脚本宝典的支持。

脚本宝典总结

以上是脚本宝典为你收集整理的Tomcat如何监控并删除超时Session详解全部内容,希望文章能够帮你解决Tomcat如何监控并删除超时Session详解所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签:sessiontomcat