会话超时在Google App Engine上不起作用

时间:2015-05-14 01:23:04

标签: java jsp google-app-engine session google-cloud-datastore

我目前在Google App Engine上部署了一个简单的JSP Servlet Web应用程序。 我有一个SessionListener实现HttpSessionListener来监视会话的创建和销毁。

public class SessionListener implements HttpSessionListener {

  private int sessionCount = 0;

  @Override
  public void sessionCreated(HttpSessionEvent event) {
    // TODO Auto-generated method stub
    synchronized (this) {
        sessionCount++;
    }

    System.out.println("Session Created: " + event.getSession().getId());
    System.out.println("Total Sessions: " + sessionCount);
  }

  @Override
  public void sessionDestroyed(HttpSessionEvent event) {
    // TODO Auto-generated method stub
    synchronized (this) {
        sessionCount--;
    }
    System.out.println("Session Destroyed: " + event.getSession().getId() + ", " + event.getSession().getAttribute("loginName"));
    System.out.println("Total Sessions: " + sessionCount);
  }

}

在web.xml文件中,我设置了以下配置以允许30分钟后自动会话超时:

<session-config>
    <session-timeout>30</session-timeout>
</session-config>

在appengine-web.xml文件中,我设置了以下配置:

<sessions-enabled>true</sessions-enabled>

当部署并在本地运行时,空闲30分钟会在30分钟后被破坏,正如我在控制台中看到的那样

"Session Destroyed: {sessionId} etc"

然而,当我在GAE上部署应用程序时,我无法在GAE管理页面的日志控制台中找到空闲会话的打印输出。当我明确点击调用

的Logout按钮时,会话才会被销毁
HttpSession session=request.getSession(false);
session.invalidate();

所以我认为空闲会话在特定的设定时间后永远不会被破坏。我注意到随着新浏览器访问网络应用程序,会话总数不断增加。

我知道GAE按照自己的方式处理会话:在数据存储区中存储会话并在整个JVM中共享它们。但是有没有办法使会话超时工作并在一段时间内将会话空闲?

1 个答案:

答案 0 :(得分:3)

调整GAE内置的SessionCleanupServlet为我解决了这个问题。更多详情:http://www.radomirml.com/blog/2011/03/26/cleaning-up-expired-sessions-from-app-engine-datastore/

相关问题