![]() Version: 9.3.28.v20191105 |
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 for sponsored feature development
Jetty can support session clustering by persisting sessions to Infinispan.
Each Jetty instance locally caches sessions for which it has received requests, writing any changes to the session through to Infinispan as the request exits the server.
Sessions must obey the Serialization contract, and servlets must call the Session.setAttribute()
method to ensure that changes are persisted.
The persistent session mechanism works in conjunction with a load balancer that supports stickiness. Stickiness can be based on various data items, such as source IP address or characteristics of the session ID or a load-balancer specific mechanism. For those load balancers that examine the session ID, the Jetty persistent session mechanism appends a node ID to the session ID, which can be used for routing.
There are two components to session management in Jetty: a session ID manager and a session manager.
These managers also cooperate and collaborate with the org.eclipse.jetty.server.session.SessionHandler
to enable cross-context dispatch.
When using the jetty distribution, to enable Infinispan session persistence, you will first need to enable the Infinispan module for your base using the --add-to-start
or --add-to-startd
argument to the start.jar.
As part of the module installation, the necessary Infinispan jars will be dynamically downloaded and installed to your ${jetty.base}/lib/infinispan
directory.
If you need to up or downgrade the version of the Infinispan jars, then you can delete the jars that were automatically installed and replace them.
Once you’ve done that, you will need to prevent Jetty’s startup checks from detecting the missing jars.
To do that, you can use --skip-file-validation=infinispan
argument to start.jar on the command line, or place that line inside ${jetty.base}/start.ini
to ensure it is used for every start.
You will also find the following properties, either in your base’s start.d/infinispan.ini
file or appended to your start.ini
, depending on how you enabled the module:
## Unique identifier for this node in the cluster jetty.infinispanSession.workerName=node1
These properties are applied to the InfinispanSessionIdManager
described below.
The Infinispan module will have installed file called $\{jetty.home}/etc/jetty-infinispan.xml
.
This file configures an instance of the InfinispanSessionIdManager
that will be shared across all webapps deployed on that server.
It looks like this:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd">
<Configure id="Server" class="org.eclipse.jetty.server.Server">
<!-- ===================================================================== -->
<!-- Get a reference to the default local cache. -->
<!-- ===================================================================== -->
<New id="local" class="org.infinispan.manager.DefaultCacheManager">
<Get id="cache" name="cache"></Get>
</New>
<!-- ===================================================================== -->
<!-- Get a reference to a hotrod client for a remote cache. -->
<!-- Change the name of the cache to match your setup. -->
<!-- ===================================================================== -->
<!--
<New id="hotrodMgr" class="org.infinispan.client.hotrod.RemoteCacheManager">
<Call id="cache" name="getCache">
<Arg>sessions</Arg>
</Call>
</New>
-->
<!-- ===================================================================== -->
<!-- Configure a SessionIdManager with the cache selected above. -->
<!-- ===================================================================== -->
<Set name="sessionIdManager">
<New id="idMgr" class="org.eclipse.jetty.session.infinispan.InfinispanSessionIdManager">
<Arg>
<Ref refid="Server"/>
</Arg>
<Set name="workerName"><Property name="jetty.infinispanSession.workerName" default="node1"/></Set>
<Set name="cache"><Ref refid="cache"/></Set>
</New>
</Set>
</Configure>
As you can see, you configure the Infinispan Cache instance that the InfinispanSessionIdManager
should use in this file.
By default, the Infinispan Default cache instance is used (e.g. on the local node).
You can instead use a custom Cache setup - the jetty-infinispan.xml
file shows you how to configure a remote Cache (using the hotrod java client).
The InfinispanSessionIdManager
can be configured by calling setters:
As mentioned elsewhere, there should be one InfinispanSessionManager
per context (e.g. webapp).
It will need to reference the single InfinispanSessionIdManager
configured previously for the Server.
The way you configure a InfinispanSessionManager
depends on whether you’re configuring from a context xml file, a jetty-web.xml
file or code.
The basic difference is how you get a reference to the Jetty org.eclipse.jetty.server.Server
instance.
From a context xml file, you reference the Server instance as a Ref:
<!-- Expose the jetty infinispan classes for session serialization -->
<Call name="prependServerClass">
<Arg>-org.eclipse.jetty.session.infinispan.</Arg>
</Call>
<!-- Get a reference to the InfinispanSessionIdManager -->
<Ref id="Server">
<Call id="idMgr" name="getSessionIdManager"/>
</Ref>
<!-- Get a referencee to the Cache from the InfinispanSessionIdManager -->
<Ref id="idMgr">
<Get id="cache" name="cache"/>
</Ref>
<!-- Use the InfinispanSessionIdManager and Cache to setup up the InfinispanSessionManager -->
<Set name="sessionHandler">
<New class="org.eclipse.jetty.server.session.SessionHandler">
<Arg>
<New id="mgr" class="org.eclipse.jetty.session.infinispan.InfinispanSessionManager">
<Set name="sessionIdManager">
<Ref id="idMgr"/>
</Set>
<Set name="cache">
<Ref id="cache">
</Ref>
</Set>
<Set name="scavengeInterval">60</Set>
</New>
</Arg>
</New>
</Set>
From a WEB-INF/jetty-web.xml
file, you can reference the Server instance directly:
<!-- Expose the jetty infinispan classes for session serialization -->
<Call name="prependServerClass">
<Arg>-org.eclipse.jetty.session.infinispan.</Arg>
</Call>
<!-- Reference the server directly -->
<Get name="server">
<Get id="idMgr" name="sessionIdManager"/>
</Get>
<!-- Get a reference to the Cache via the InfinispanSessionIdManager -->
<Ref id="idMgr">
<Get id="cache" name="cache"/>
</Ref>
<!-- Apply the SessionIdManager and Cache to the InfinispanSessionManager -->
<Set name="sessionHandler">
<New class="org.eclipse.jetty.server.session.SessionHandler">
<Arg>
<New id="mgr" class="org.eclipse.jetty.session.infinispan.InfinispanSessionManager">
<Set name="sessionIdManager">
<Ref id="idMgr"/>
</Set>
<Set name="cache">
<Ref id="cache">
</Ref>
</Set>
<Set name="scavengeInterval">600</Set>
</New>
</Arg>
</New>
</Set>
The InfinispanSessionManager can be provided by calling setters:
If you’re using the hotrod client - where serialization will be required - you will need to ensure that the hotrod marshalling software works with Jetty classloading.
To do this, firstly ensure that you have included the lines containing the prependServerClass
to your context xml file as shown above.
Then, create the file ${jetty.base}/resources/hotrod-client.properties
.
Add the following line to this file:
infinispan.client.hotrod.marshaller=org.eclipse.jetty.session.infinispan.WebAppMarshaller