Skip to content

Instantly share code, notes, and snippets.

@asamaraw
Last active April 28, 2018 00:17
Show Gist options
  • Save asamaraw/7cce23eb76cc06eba9ffef4033bf415f to your computer and use it in GitHub Desktop.
Save asamaraw/7cce23eb76cc06eba9ffef4033bf415f 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