代码改变世界

超时

2016-04-18 15:00  chen.simon  阅读(441)  评论(0编辑  收藏  举报

1. tomcat的session超时怎么做的

有专门的后台线程每隔10秒(sleep 10l,可以配置)处理一次是否过期

专门的线程

org.apache.catalina.core.ContainerBase.ContainerBackgroundProcessor

触发过期检查的地方:

public void run() {
            while (!threadDone) {
                try {
                    Thread.sleep(backgroundProcessorDelay * 1000L);
                } catch (InterruptedException e) {
                    ;
                }
                if (!threadDone) {
                    Container parent = (Container) getMappingObject();
                    ClassLoader cl = 
                        Thread.currentThread().getContextClassLoader();
                    if (parent.getLoader() != null) {
                        cl = parent.getLoader().getClassLoader();
                    }
                    processChildren(parent, cl);
                }
            }
        }

Thread.sleep(backgroundProcessorDelay * 1000L); backgroundProcessorDelay 秒钟检查一次。backgroundProcessorDelay 在StandardEngine设置的是10 也就是10s钟检查一次。可以理解成session超时默认精度10s

threadDone 这个标志会在stop的时候置成true,以使得能循环能正常结束

处理是否过期

if (maxInactiveInterval >= 0) { 
            long timeNow = System.currentTimeMillis();
            int timeIdle = (int) ((timeNow - thisAccessedTime) / 1000L);
            if (timeIdle >= maxInactiveInterval) {
                expire(true);
            }
        }

处理过期的具体方法:

org.apache.catalina.session.StandardSession.expire(boolean)

--EOF--