Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ramsrib/1095e7897eb858bd1b70caf19fbfff91 to your computer and use it in GitHub Desktop.
Save ramsrib/1095e7897eb858bd1b70caf19fbfff91 to your computer and use it in GitHub Desktop.
Distributed JDBC session store for Dropwizard 1.2.2
@Override
public void run(TestDropwizardConfiguration configuration, Environment environment)
throws Exception
{
env.lifecycle().addLifeCycleListener(new AbstractLifeCycleListener()
{
@Override
public void lifeCycleStarting(LifeCycle event)
{
if (!(event instanceof Server))
{
return;
}
Server server = (Server) event;
DataSourceFactory dataSourceFactory = configuration.getDataSourceFactory();
DatabaseAdaptor databaseAdaptor = new DatabaseAdaptor();
databaseAdaptor.setDriverInfo(
dataSourceFactory.getDriverClass(),
dataSourceFactory.getUrl()
+ ";user=" + dataSourceFactory.getUser()
+ ";password=" + dataSourceFactory.getPassword());
JDBCSessionDataStore sessionDataStore = new JDBCSessionDataStore();
sessionDataStore.setDatabaseAdaptor(databaseAdaptor);
DefaultSessionIdManager sessionIdManager = new DefaultSessionIdManager(server);
sessionIdManager.setWorkerName(configuration.getNodeName());
SessionHandler sessionHandler = new SessionHandler();
sessionHandler.setSessionIdManager(sessionIdManager);
// Idle HTTP servlet sessions expire after 24 hours
sessionHandler.setMaxInactiveInterval(60 * 60 * 24);
sessionHandler.setSessionCookie("LOGIN_SESSION1");
sessionHandler.setHttpOnly(true);
env.servlets().setSessionHandler(sessionHandler);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment