HashedSession无法强制转换为JDBCSessionManager $ Session

时间:2016-03-27 20:00:36

标签: java jdbc jetty session-replication

我正在使用本文session-clustering-jetty

通过与jetty的会话群集编写示例Web应用程序

我在运行的两个jetty实例前面有一个apache httpd mod_proxy_balancer负载均衡器。我的jetty-jdbc-sessions.xml文件看起来像这样

<Set name="sessionIdManager">
    <New id="idMgr" class="org.eclipse.jetty.server.session.JDBCSessionIdManager">
      <Arg>
        <Ref refid="Server"/>
      </Arg>
      <Set name="workerName"><Property name="jetty.jdbcSession.workerName" default="node1"/></Set>    
      <Set name="workerName"><Property name="jetty.jdbcSession.workerName" default="node2"/></Set>
      <Set name="scavengeInterval"><Property name="jetty.jdbcSession.scavenge" default="1800"/></Set>

      <!-- ===================================================================== -->
      <!-- Uncomment either the datasource or driver setup and configure         -->
      <!-- ===================================================================== -->

      <!--
          <Set name="DatasourceName"><Property name="jetty.jdbcSession.datasource" default="javax.sql.DataSource/default"/></Set>
      -->

        <Call name="setDriverInfo">
          <Arg><Property name="jetty.jdbcSession.driverClass"/></Arg>
          <Arg><Property name="jetty.jdbcSession.connectionURL"/></Arg>
        </Call>

    </New>
  </Set>

我的deployable.xml文件如下所示

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN"
   "http://www.eclipse.org/jetty/configure_9_0.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
<Ref id="Server">
  <Call id="idMgr" name="getSessionIdManager"/>
</Ref>
<Set name="sessionHandler">
  <New class="org.eclipse.jetty.server.session.SessionHandler">
    <Arg>
      <New id="jdbcmgr" class="org.eclipse.jetty.server.session.JDBCSessionManager">
        <Set name="sessionIdManager">
          <Ref id="idMgr"/>
        </Set>
      </New>
    </Arg>
  </New>
</Set>
</Configure>

当我运行我的应用程序并尝试从HttpServletRequest读取会话ID时,如下所示

String sessionId = request.getSession().getId();

我一直收到异常

java.lang.ClassCastException: org.eclipse.jetty.server.session.HashedSession cannot be cast to org.eclipse.jetty.server.session.JDBCSessionManager$Session
    at org.eclipse.jetty.server.session.JDBCSessionIdManager.addSession(JDBCSessionIdManager.java:813)
    at org.eclipse.jetty.server.session.AbstractSessionManager.addSession(AbstractSessionManager.java:678)
    at org.eclipse.jetty.server.session.AbstractSessionManager.newHttpSession(AbstractSessionManager.java:566)
    at org.eclipse.jetty.server.Request.getSession(Request.java:1405)
    at org.eclipse.jetty.server.Request.getSession(Request.java:1378)
    at com.javacodegeeksexample.SessionServlet.doPost(SessionServlet.java:42)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:707)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:812)
    at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:587)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:143)
    at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:553)
    at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:223)
    at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1127)
    at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:515)
    at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:185)
    at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1061)
    at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:141)
    at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:215)
    at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:110)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.rewrite.handler.RewriteHandler.handle(RewriteHandler.java:332)
    at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:97)
    at org.eclipse.jetty.server.Server.handle(Server.java:499)
    at org.eclipse.jetty.server.HttpChannel.handle(HttpChannel.java:311)
    at org.eclipse.jetty.server.HttpConnection.onFillable(HttpConnection.java:257)
    at org.eclipse.jetty.io.AbstractConnection$2.run(AbstractConnection.java:544)
    at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:635)
    at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:555)
    at java.lang.Thread.run(Unknown Source)

不确定我在这里做错了什么? JDBCSessionIdManager.addSession使用HttpSession,这是HttpServletRequest中的内容。

1 个答案:

答案 0 :(得分:1)

这可能是因为您尚未为Web应用程序上下文配置JDBCSessionManager。仅配置JDBCSessionIdManager是不够的。见the documentation