Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

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

< Jetty‎ | Howto
(6 intermediate revisions by 4 users not shown)
Line 1: Line 1:
 
{{Jetty Howto
 
{{Jetty Howto
 
| introduction =  
 
| 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/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 is sometimes useful to preserve existing Sessions across restarts of Jetty. The [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/server/session/HashSessionManager.html 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.
  
| steps =
+
==Enabling Persistence==
===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:
+
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:
  
 
<source lang="xml">  
 
<source lang="xml">  
<Configure class="org.mortbay.jetty.webapp.WebAppContext">
+
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
 
   .
 
   .
 
   .
 
   .
 
   .
 
   .
 
   <Set name="sessionHandler">
 
   <Set name="sessionHandler">
     <New class="org.mortbay.jetty.servlet.SessionHandler">
+
     <New class="org.eclipse.jetty.server.session.SessionHandler">
 
       <Arg>
 
       <Arg>
         <New class="org.mortbay.jetty.servlet.HashSessionManager">
+
         <New class="org.eclipse.jetty.server.session.HashSessionManager">
 
           <Set name="storeDirectory">your/chosen/directory/goes/here</Set>
 
           <Set name="storeDirectory">your/chosen/directory/goes/here</Set>
 
         </New>
 
         </New>
Line 28: Line 27:
 
</source>
 
</source>
  
{{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|If you want to persist the sessions from multiple webapps:
 +
# Configure a separate HashSessionManager for each.
 +
# Assign to each a different value for ''storeDirectory''.}}
  
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.
+
The above example uses a configuration file suitable for the [http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/deploy/providers/ContextProvider.html ContextProvider], thus you might want to check out the [[Jetty/Feature/ContextDeployer|Context Provider]] feature guide.
  
===Delaying Session Load===
+
==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.
+
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 <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>, Jetty loads sessions lazily either when it receives the first request for a session, or the session scavenger runs for the first time, whichever happens first. Here's how the configuration looks in XML:
  
 
<source lang="xml">  
 
<source lang="xml">  
 
<Set name="sessionHandler">
 
<Set name="sessionHandler">
   <New class="org.mortbay.jetty.servlet.SessionHandler">
+
   <New class="org.eclipse.jetty.servlet.SessionHandler">
 
     <Arg>
 
     <Arg>
       <New class="org.mortbay.jetty.servlet.HashSessionManager">
+
       <New class="org.eclipse.jetty.servlet.HashSessionManager">
 
         <Set name="lazyLoad">true</Set>
 
         <Set name="lazyLoad">true</Set>
 
       </New>
 
       </New>
Line 50: Line 51:
 
</source>
 
</source>
  
===Enabling Persistence for the Maven Jetty Plugin===
+
===Enabling Persistence for the Jetty Maven 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 Jetty Maven plugin, set up the HashSessionManager in the <configuration> section like so:
  
 
<source lang="xml">  
 
<source lang="xml">  
 
<plugin>
 
<plugin>
 
   <groupId>org.mortbay.jetty</groupId>
 
   <groupId>org.mortbay.jetty</groupId>
   <artifactId>maven-jetty-plugin</artifactId>
+
   <artifactId>jetty-maven-plugin</artifactId>
   <version>6.1.6</version>
+
   <version>7.4.3.v20110701</version>
 
   <configuration>
 
   <configuration>
     <scanIntervalSeconds>1</scanIntervalSeconds>
+
     <!-- ... -->
     <webAppConfig implementation="org.mortbay.jetty.plugin.Jetty6PluginWebAppContext">
+
     <webAppConfig implementation="org.mortbay.jetty.plugin.JettyWebAppContext">
       <contextPath>/foo</contextPath>
+
       <defaultsDescriptor>${project.build.outputDirectory}/META-INF/webdefault.xml</defaultsDescriptor>
       .
+
       <contextPath>${jetty.contextRoot}</contextPath>
      .
+
       <sessionHandler implementation="org.eclipse.jetty.server.session.SessionHandler">
      .
+
         <sessionManager implementation="org.eclipse.jetty.server.session.HashSessionManager">
       <sessionHandler implementation="org.mortbay.jetty.servlet.SessionHandler">
+
           <storeDirectory>${basedir}/target/jetty-sessions</storeDirectory>
         <sessionManager implementation="org.mortbay.jetty.servlet.HashSessionManager">
+
          <idleSavePeriod>1</idleSavePeriod>
           <storeDirectory>${basedir}/target/your/sessions/go/here</storeDirectory>
+
 
         </sessionManager>
 
         </sessionManager>
 
       </sessionHandler>
 
       </sessionHandler>
      .
 
      .
 
      .
 
 
     </webAppConfig>
 
     </webAppConfig>
 +
    <!-- ... -->
 
   </configuration>
 
   </configuration>
 
</plugin>
 
</plugin>
 
</source>
 
</source>
 +
| more =
 +
For more information, see the [[Jetty/Tutorial/Session Clustering|Session Clustering]] tutorial.
 
}}
 
}}

Revision as of 09:10, 3 May 2012



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.

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.server.session.SessionHandler">
      <Arg>
        <New class="org.eclipse.jetty.server.session.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 ContextProvider, thus you might want to check out the Context Provider feature guide.

Delaying Session Load

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), Jetty loads sessions lazily either when it receives the first request for a session, 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.eclipse.jetty.servlet.SessionHandler">
    <Arg>
      <New class="org.eclipse.jetty.servlet.HashSessionManager">
        <Set name="lazyLoad">true</Set>
      </New>
    </Arg>
  </New>
</Set>

Enabling Persistence for the Jetty Maven Plugin

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

 
<plugin>
  <groupId>org.mortbay.jetty</groupId>
  <artifactId>jetty-maven-plugin</artifactId>
  <version>7.4.3.v20110701</version>
  <configuration>
    <!-- ... -->
    <webAppConfig implementation="org.mortbay.jetty.plugin.JettyWebAppContext">
      <defaultsDescriptor>${project.build.outputDirectory}/META-INF/webdefault.xml</defaultsDescriptor>
      <contextPath>${jetty.contextRoot}</contextPath>
      <sessionHandler implementation="org.eclipse.jetty.server.session.SessionHandler">
        <sessionManager implementation="org.eclipse.jetty.server.session.HashSessionManager">
          <storeDirectory>${basedir}/target/jetty-sessions</storeDirectory>
          <idleSavePeriod>1</idleSavePeriod>
        </sessionManager>
      </sessionHandler>
    </webAppConfig>
    <!-- ... -->
  </configuration>
</plugin>






Additional Resources

For more information, see the Session Clustering tutorial.

Back to the top