Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

Persisting Sessions

It can sometimes be useful to be able to preserve existing Sessions across restarts of Jetty. The org.mortbay.jetty.servlet.HashSessionManager now supports this feature. If persistence is enabled, the HashSessionManager will save all existing, valid Sessions to disk before shutdown completes. On restart, the saved Sessions are restored.

Enabling Persistence

A SessionManager does just what it's name suggests - it manages the lifecycle and state of Sessions on behalf of a webapp. Each webapp must have it's 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:

Code Block
xml
xml
<Configure class="org.mortbay.jetty.webapp.WebAppContext">

 .
 .
 .
 <Set name="sessionHandler">
   <New class="org.mortbay.jetty.servlet.SessionHandler">
     <Arg>
       <New class="org.mortbay.jetty.servlet.HashSessionManager">
         <Set name="storeDirectory">your/chosen/directory/goes/here</Set>
       </New>
     </Arg>
   </New>
 </Set>
 .
 .
 .
</Configure>
Tip
title"Reminder"

Don't forget that if you want to persist the sessions from multiple webapps, you'll need to configure a separate HashSessionManager for each, and naturally each should have a different value for storeDirectory.

The above example uses a configuration file suitable for the Context Deployer - you might want to check out the documentation for it.

Delaying Session Load

Sometimes you may need to ensure that the sessions are loaded AFTER the servlet environment has been started up (by default, sessions will be eagerly loaded as part of the container startup, but before the servlet environment has been initialized). 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:

Code Block
xml
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:

Code Block
xml
xml
<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>
Contact the core Jetty developers at www.webtide.com
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery