Jump to: navigation, search

Difference between revisions of "Jetty/Howto/Persisting Sessions"

< Jetty‎ | Howto
(New page: {{Jetty Howto | introduction = It can sometimes be useful to be able to preserve existing Sessions across restarts of Jetty. The [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jett...)
 
Line 3: Line 3:
 
It can sometimes be useful to be able to preserve existing Sessions across restarts of Jetty. The [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashSessionManager.java?root=RT_JETTY&view=markup org.eclipse.jetty.server.session.HashSessionManager] 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.
 
It can sometimes be useful to be able to preserve existing Sessions across restarts of Jetty. The [http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-server/src/main/java/org/eclipse/jetty/server/session/HashSessionManager.java?root=RT_JETTY&view=markup org.eclipse.jetty.server.session.HashSessionManager] 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.
  
| steps = (optional)
+
| steps =
 
==Enabling Persistence==
 
==Enabling Persistence==
  
Line 30: Line 30:
 
{{tip|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'''.}}
 
{{tip|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 [[Jetty/Feature/ContextDeployer|Context Deployer]] - you might want to check out the [[Jetty/Feature/ContextDeployer|feature guide]] for it.
+
The above example uses a configuration file suitable for the [[http://dev.eclipse.org/viewcvs/index.cgi/jetty/trunk/jetty-deploy/src/main/java/org/eclipse/jetty/deploy/ContextDeployer.java?root=RT_JETTY&view=markup|org.eclipse.jetty.deploy.ContextDeployer]], thus you might want to check out the [[Jetty/Feature/ContextDeployer|Context Deployer]] feature guide.
  
 
==Delaying Session Load==
 
==Delaying Session Load==
Line 38: Line 38:
 
Using <tt>SessionManager.setLazyLoad(true)</tt>, 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:
 
Using <tt>SessionManager.setLazyLoad(true)</tt>, 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:
  
<div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
<source lang="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>
 +
</source>
  
+
==Enabling Persistence for the Maven Jetty Plugin==
<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>
+
 
+
</div></div>
+
 
+
=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:
 
To enable session persistence for the maven jetty plugin, set up the HashSessionManager in the <configuration> section like so:
  
<div class="code panel" style="border-width: 1px"><div class="codeContent panelContent">
+
<source lang="xml">  
 
+
<plugin>
+
  <groupId>org.mortbay.jetty</groupId>
<plugin>
+
  <artifactId>maven-jetty-plugin</artifactId>
        <groupId>org.mortbay.jetty</groupId>
+
  <version>6.1.6</version>
        <artifactId>maven-jetty-plugin</artifactId>
+
  <configuration>
        <version>6.1.6</version>
+
    <scanIntervalSeconds>1</scanIntervalSeconds>
        <configuration>
+
    <webAppConfig implementation="org.mortbay.jetty.plugin.Jetty6PluginWebAppContext">
          <scanIntervalSeconds>1</scanIntervalSeconds>
+
      <contextPath>/foo</contextPath>
          <webAppConfig implementation="org.mortbay.jetty.plugin.Jetty6PluginWebAppContext">
+
      .
            <contextPath>/foo</contextPath>
+
      .
            .
+
      .
            .
+
      <sessionHandler implementation="org.mortbay.jetty.servlet.SessionHandler">
            .
+
        <sessionManager implementation="org.mortbay.jetty.servlet.HashSessionManager">
            <sessionHandler implementation="org.mortbay.jetty.servlet.SessionHandler">
+
          <storeDirectory>${basedir}/target/your/sessions/go/here</storeDirectory>
              <sessionManager implementation="org.mortbay.jetty.servlet.HashSessionManager">
+
        </sessionManager>
                <storeDirectory>${basedir}/target/your/sessions/go/here</storeDirectory>
+
      </sessionHandler>
              </sessionManager>
+
      .
            </sessionHandler>
+
      .
            .
+
      .
            .
+
    </webAppConfig>
            .
+
  </configuration>
          </webAppConfig>
+
</plugin>
        </configuration>
+
</plugin>
+
 
</source>
 
</source>
 
}}
 
}}

Revision as of 12:33, 21 June 2010



Introduction

It can sometimes be useful to be able to preserve existing Sessions across restarts of Jetty. The org.eclipse.jetty.server.session.HashSessionManager 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.


Steps

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:

 
<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>
Idea.png
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 [[1]], thus you might want to check out the Context Deployer feature guide.

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:

 
<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>