Jump to: navigation, search

Jetty/Howto/Persisting Sessions

< Jetty‎ | Howto
Revision as of 15:34, 22 August 2011 by Boulay.intalio.com (Talk | contribs)



Introduction

It is sometimes useful to preserve existing Sessions across restarts of Jetty. The HashSessionManager supports this feature. If you enable persistence, the HashSessionManager saves all existing, valid Sessions to disk before shutdown completes. On restart, Jetty restores the saved Sessions are.

Enabling Persistence

A SessionManager does just what its name suggests–it manages the lifecycle and state of Sessions on behalf of a webapp. Each webapp must have its own unique SessionManager instance. Enabling persistence is as simple as configuring the HashSessionManager as the SessionManager for a webapp and telling it where on disk to store the sessions:

 
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
  .
  .
  .
  <Set name="sessionHandler">
    <New class="org.eclipse.jetty.servlet.SessionHandler">
      <Arg>
        <New class="org.eclipse.jetty.servlet.HashSessionManager">
          <Set name="storeDirectory">your/chosen/directory/goes/here</Set>
        </New>
      </Arg>
    </New>
  </Set>
  .
  .
  .
</Configure>
Idea.png
Reminder
If you want to persist the sessions from multiple webapps:
  1. Configure a separate HashSessionManager for each.
  2. Assign to each a different value for storeDirectory.


The above example uses a configuration file suitable for the [[1]], thus you might want to check out the Context Deployer feature guide.

Delaying Session Load

Sometimes you might need to ensure that the sessions are loaded AFTER the servlet environment starts up (by default, Jetty eagerly loads sessions as part of the container startup, but before it initializes the servlet environment). For example, the Wicket web framework requires the servlet environment to be available when sessions are activated.

Using SessionManager.setLazyLoad(true), sessions will be loaded lazily either when the first request for a session is received, or the session scavenger runs for the first time, whichever happens first. Here's how the configuration looks in xml:

 
<Set name="sessionHandler">
  <New class="org.mortbay.jetty.servlet.SessionHandler">
    <Arg>
      <New class="org.mortbay.jetty.servlet.HashSessionManager">
        <Set name="lazyLoad">true</Set>
      </New>
    </Arg>
  </New>
</Set>

Enabling Persistence for the Maven Jetty Plugin

To enable session persistence for the maven jetty plugin, set up the HashSessionManager in the <configuration> section like so:

 
<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>maven-jetty-plugin</artifactId>
  <version>6.1.6</version>
  <configuration>
    <scanIntervalSeconds>1</scanIntervalSeconds>
    <webAppConfig implementation="org.mortbay.jetty.plugin.Jetty6PluginWebAppContext">
      <contextPath>/foo</contextPath>
      .
      .
      .
      <sessionHandler implementation="org.mortbay.jetty.servlet.SessionHandler">
        <sessionManager implementation="org.mortbay.jetty.servlet.HashSessionManager">
          <storeDirectory>${basedir}/target/your/sessions/go/here</storeDirectory>
        </sessionManager>
      </sessionHandler>
      .
      .
      .
    </webAppConfig>
  </configuration>
</plugin>






Additional Resources

For more information, please see the Session Clustering tutorial.