0

I need a way to share the session across different dropwizard web services. In Jetty there is a way to do it by using JDBCSessionIdManager and JDBCSessionManager (http://wiki.eclipse.org/Jetty/Tutorial/Session_Clustering).

The problem is that dropwizard (0.7.1) does not expose a reference of the org.eclipse.jetty.server.Server that is needed so there is no obvious way to do change the SessionManager and the SessionIdManager. I've seen that the Server is created in io.dropwizard.cli.ServerCommand#run through a io.dropwizard.server.ServerFactory but the reference in io.dropwizard.cli.ServerCommand#run is local so I can't even use reflection to get the reference that I want.

What should I do in dropwizard to change SessionManager and the SessionIdManager?

Thanks,
Alex

Alex
  • 83
  • 1
  • 11

2 Answers2

1

I was able to get it working by hooking into the life cycle.

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;
        }
    });
}
slow
  • 810
  • 9
  • 14
0

I used Hazelcast (http://hazelcast.org) instead. Whether this is overkill or not depends on what exactly you want to achieve and if you can afford to have another dependency in your code. But in terms of flexibility and ease of implementation, I'd definitely recommend it.

I used it as a distributed map in place of storing the data in the session itself.

You can look at some examples here:http://hazelcast.org/use-cases/clustering/ and http://hazelcast.org/use-cases/caching/

Daniele
  • 1,053
  • 10
  • 17