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.
Difference between revisions of "Jetty/Howto/Persisting Sessions"
m |
(Fixed the classes and syntax to match the new jetty-maven-plugin packages and syntax.) |
||
Line 51: | Line 51: | ||
</source> | </source> | ||
− | ===Enabling Persistence for the | + | ===Enabling Persistence for the Jetty Maven Plugin=== |
− | To enable session persistence for the | + | 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. | + | <groupId>org.mortbay.jetty</groupId> |
− | <artifactId> | + | <artifactId>jetty-maven-plugin</artifactId> |
− | <version> | + | <version>7.4.3.v20110701</version> |
<configuration> | <configuration> | ||
− | < | + | <!-- ... --> |
− | <webAppConfig implementation="org. | + | <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"> | |
− | <sessionHandler implementation="org.eclipse.jetty. | + | <storeDirectory>${basedir}/target/jetty-sessions</storeDirectory> |
− | <sessionManager implementation="org.eclipse.jetty. | + | <idleSavePeriod>1</idleSavePeriod> |
− | <storeDirectory>${basedir}/target/ | + | |
</sessionManager> | </sessionManager> | ||
</sessionHandler> | </sessionHandler> | ||
− | |||
− | |||
− | |||
</webAppConfig> | </webAppConfig> | ||
+ | <!-- ... --> | ||
</configuration> | </configuration> | ||
</plugin> | </plugin> |
Revision as of 10:18, 9 February 2012
Contents
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.servlet.SessionHandler"> <Arg> <New class="org.eclipse.jetty.servlet.HashSessionManager"> <Set name="storeDirectory">your/chosen/directory/goes/here</Set> </New> </Arg> </New> </Set> . . . </Configure>
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
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.