2014-09-26 133 views
0

我需要一种方法来通过不同的dropwizard web服务共享会话。在Jetty中,有一种方法可以通过使用JDBCSessionIdManager和JDBCSessionManager(http://wiki.eclipse.org/Jetty/Tutorial/Session_Clustering)来完成。Dropwizard会话群集

问题是dropwizard(0.7.1)没有公开所需的org.eclipse.jetty.server.Server的引用,所以没有明显的方法来更改SessionManager和SessionIdManager。 我见过服务器是在io.dropwizard.cli.ServerCommand中创建的#通过io.dropwizard.server.ServerFactory运行,但io.dropwizard.cli.ServerCommand#run中的引用是本地的,所以我甚至不能使用反射来获得我想要的参考。

我应该怎么做dropwizard来改变SessionManager和SessionIdManager?

感谢,
亚历

回答

1

我能得到它挂接到生命周期的工作。

private void addSessionHandler(final Environment env, final DataSource dataSource) { 
    env.lifecycle().addLifeCycleListener(new AbstractLifeCycleListener() { 
     @Override 
     public void lifeCycleStarting(LifeCycle event) { 
      if (!(event instanceof Server)) { 
       return; 
      } 

      Server server = (Server) event; 
      JDBCSessionIdManager ids = jdbcSessionIdManager(server); 
      server.setSessionIdManager(ids); 
      env.servlets().setSessionHandler(new SessionHandler(jdbcSessionManager(ids))); 
     } 

     private JDBCSessionManager jdbcSessionManager(JDBCSessionIdManager idManager) { 
      JDBCSessionManager m = new JDBCSessionManager(); 
      m.setSessionIdManager(idManager); 
      return m; 
     } 

     private JDBCSessionIdManager jdbcSessionIdManager(Server server) { 
      JDBCSessionIdManager m = new JDBCSessionIdManager(server); 
      m.setWorkerName(""); 
      m.setDatasource(dataSource); 
      return m; 
     } 
    }); 
}