Following code illustrates how to configure distributed JDBC sessions in Dropwizard V1.2.2.
Application.java file,
Application.java file,
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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); | |
} | |
}); | |
} |
Comments
Post a Comment