blob: 102d268deab98b1a4f374e56cae73cbd9b00d56e [file] [log] [blame]
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Session Clustering with Infinispan</title><link rel="stylesheet" type="text/css" href="css/docbook.css"><meta name="generator" content="DocBook XSL Stylesheets V1.79.1"><meta name="keywords" content="jetty, servlet, servlet-api, cometd, http, websocket, eclipse, maven, java, server, software"><link rel="home" href="index.html" title="Jetty"><link rel="up" href="session-management.html" title="Chapter&nbsp;10.&nbsp;Session Management"><link rel="prev" href="session-clustering-mongodb.html" title="Session Clustering with MongoDB"><link rel="next" href="configuring-sessions-hazelcast.html" title="Clustered Session Management: Hazelcast"><link xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" rel="shortcut icon" href="images/favicon.ico"><link rel="stylesheet" href="css/highlighter/foundation.css"><script src="js/highlight.pack.js"></script><script>
hljs.initHighlightingOnLoad();
</script><link type="text/css" rel="stylesheet" href="css/font-awesome/font-awesome.min.css"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><table xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><tr><td style="width: 25%"><a href="http://www.eclipse.org/jetty"><img src="images/jetty-header-logo.png" alt="Jetty Logo"></a><br><span style="font-size: small">
Version: 9.3.28.v20191105</span></td><td style="width: 50%"></td></tr></table><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">Session Clustering with Infinispan</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="session-clustering-mongodb.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a>&nbsp;</td><th width="60%" align="center">Chapter&nbsp;10.&nbsp;Session Management<br><a accesskey="p" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></th><td width="20%" align="right">&nbsp;<a accesskey="n" href="configuring-sessions-hazelcast.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr></table><hr></div><div xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times" class="jetty-callout"><h5 class="callout"><a href="http://www.webtide.com/">Contact the core Jetty developers at
<span class="website">www.webtide.com</span></a></h5><p>
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
</p></div><div class="section"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="session-clustering-infinispan"></a>Session Clustering with Infinispan</h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="section"><a href="session-clustering-infinispan.html#_configuration_3">Configuration</a></span></dt><dt><span class="section"><a href="session-clustering-infinispan.html#_the_infinispan_module">The Infinispan Module</a></span></dt></dl></div><p>Jetty can support session clustering by persisting sessions to <a class="link" href="http://www.infinispan.org" target="_top">Infinispan</a>.
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 <code class="literal">Session.setAttribute()</code> method to ensure that changes are persisted.</p><p>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.</p><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_configuration_3"></a>Configuration</h3></div></div></div><p>There are two components to session management in Jetty: a session ID manager and a session manager.</p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem">The session ID manager ensures that session IDs are unique across all webapps hosted on a Jetty instance, and thus there can only be one session ID manager per Jetty instance.</li><li class="listitem">The session manager handles the session lifecycle (create/update/invalidate/expire) on behalf of a web application, so there is one session manager per web application instance.</li></ul></div><p>These managers also cooperate and collaborate with the <code class="literal">org.eclipse.jetty.server.session.SessionHandler</code> to enable cross-context dispatch.</p></div><div class="section"><div class="titlepage"><div><div><h3 class="title"><a name="_the_infinispan_module"></a>The Infinispan Module</h3></div></div></div><p>When using the jetty distribution, to enable Infinispan session persistence, you will first need to enable the Infinispan <a class="link" href="startup-modules.html" title="Managing Startup Modules">module</a> for your <a class="link" href="quickstart-running-jetty.html#creating-jetty-base" title="Creating a new Jetty Base">base</a> using the <code class="literal">--add-to-start</code> or <code class="literal">--add-to-startd</code> argument to the <a class="link" href="startup.html#startup-overview" title="Startup Overview">start.jar</a>.</p><p>As part of the module installation, the necessary Infinispan jars will be dynamically downloaded and installed to your <code class="literal">${jetty.base}/lib/infinispan</code> 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&#8217;ve done that, you will need to prevent Jetty&#8217;s startup checks from detecting the missing jars.
To do that, you can use <code class="literal">--skip-file-validation=infinispan</code> argument to start.jar on the command line, or place that line inside <code class="literal">${jetty.base}/start.ini</code> to ensure it is used for every start.</p><p>You will also find the following properties, either in your base&#8217;s <code class="literal">start.d/infinispan.ini</code> file or appended to your <code class="literal">start.ini</code>, depending on how you enabled the module:</p><pre class="literallayout">## Unique identifier for this node in the cluster
jetty.infinispanSession.workerName=node1</pre><div class="variablelist"><dl class="variablelist"><dt><span class="term">jetty.infinispanSession.workerName</span></dt><dd>The name that uniquely identifies this node in the cluster.
This value will also be used by the sticky load balancer to identify the node.
Don&#8217;t forget to change the value of this property on <span class="strong"><strong>each</strong></span> node on which you enable Infinispan session clustering.</dd></dl></div><p>These properties are applied to the <code class="literal">InfinispanSessionIdManager</code> described below.</p><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="_configuring_the_infinispansessionidmanager"></a>Configuring the InfinispanSessionIdManager</h4></div></div></div><p>The Infinispan module will have installed file called <code class="literal">$\{jetty.home}/etc/jetty-infinispan.xml</code>.
This file configures an instance of the <code class="literal">InfinispanSessionIdManager</code> that will be shared across all webapps deployed on that server.
It looks like this:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code>&lt;?xml version="1.0"?&gt;
&lt;!DOCTYPE Configure PUBLIC "-//Jetty//Configure//EN" "http://www.eclipse.org/jetty/configure_9_3.dtd"&gt;
&lt;Configure id="Server" class="org.eclipse.jetty.server.Server"&gt;
&lt;!-- ===================================================================== --&gt;
&lt;!-- Get a reference to the default local cache. --&gt;
&lt;!-- ===================================================================== --&gt;
&lt;New id="local" class="org.infinispan.manager.DefaultCacheManager"&gt;
&lt;Get id="cache" name="cache"&gt;&lt;/Get&gt;
&lt;/New&gt;
&lt;!-- ===================================================================== --&gt;
&lt;!-- Get a reference to a hotrod client for a remote cache. --&gt;
&lt;!-- Change the name of the cache to match your setup. --&gt;
&lt;!-- ===================================================================== --&gt;
&lt;!--
&lt;New id="hotrodMgr" class="org.infinispan.client.hotrod.RemoteCacheManager"&gt;
&lt;Call id="cache" name="getCache"&gt;
&lt;Arg&gt;sessions&lt;/Arg&gt;
&lt;/Call&gt;
&lt;/New&gt;
--&gt;
&lt;!-- ===================================================================== --&gt;
&lt;!-- Configure a SessionIdManager with the cache selected above. --&gt;
&lt;!-- ===================================================================== --&gt;
&lt;Set name="sessionIdManager"&gt;
&lt;New id="idMgr" class="org.eclipse.jetty.session.infinispan.InfinispanSessionIdManager"&gt;
&lt;Arg&gt;
&lt;Ref refid="Server"/&gt;
&lt;/Arg&gt;
&lt;Set name="workerName"&gt;&lt;Property name="jetty.infinispanSession.workerName" default="node1"/&gt;&lt;/Set&gt;
&lt;Set name="cache"&gt;&lt;Ref refid="cache"/&gt;&lt;/Set&gt;
&lt;/New&gt;
&lt;/Set&gt;
&lt;/Configure&gt;</code></pre><p>As you can see, you configure the Infinispan <a class="link" href="http://infinispan.org/docs/7.1.x/user_guide/user_guide.html#_the_cache_apis" target="_top">Cache</a> instance that the <code class="literal">InfinispanSessionIdManager</code> should use in this file.
By default, the Infinispan <a class="link" href="http://infinispan.org/docs/7.1.x/getting_started/getting_started.html#_running_infinispan_on_a_single_node" target="_top">Default cache</a> instance is used (e.g. on the local node).
You can instead use a custom Cache setup - the <code class="literal">jetty-infinispan.xml</code> file shows you how to configure a remote Cache (using the <a class="link" href="http://infinispan.org/docs/7.1.x/user_guide/user_guide.html#_using_hot_rod_server" target="_top">hotrod java client</a>).</p><p>The <code class="literal">InfinispanSessionIdManager</code> can be configured by calling setters:</p><div class="variablelist"><dl class="variablelist"><dt><span class="term">idleExpiryMultiple</span></dt><dd>Sessions that are not immortal, e.g. they have an expiry time, have their ids stored into Infinispan with an <a class="link" href="http://infinispan.org/docs/7.1.x/user_guide/user_guide.html#_expiration" target="_top">idle expiry timeout</a> equivalent to double the session&#8217;s timeout.
This should be sufficient to ensure that a session id that is in-use by a session is never accidentally removed.
However, should you wish to, you can configure this to any integral value to effectively increase the <a class="link" href="http://infinispan.org/docs/7.1.x/user_guide/user_guide.html#_expiration" target="_top">idle expiry</a> timeout.</dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="_configuring_the_infinispansessionmanager"></a>Configuring the InfinispanSessionManager</h4></div></div></div><p>As mentioned elsewhere, there should be one <code class="literal">InfinispanSessionManager</code> per context (e.g. webapp).
It will need to reference the single <code class="literal">InfinispanSessionIdManager</code> configured previously for the Server.</p><p>The way you configure a <code class="literal">InfinispanSessionManager</code> depends on whether you&#8217;re configuring from a context xml file, a <code class="literal">jetty-web.xml</code> file or code.
The basic difference is how you get a reference to the Jetty <code class="literal">org.eclipse.jetty.server.Server</code> instance.</p><p>From a context xml file, you reference the Server instance as a Ref:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code> &lt;!-- Expose the jetty infinispan classes for session serialization --&gt;
&lt;Call name="prependServerClass"&gt;
&lt;Arg&gt;-org.eclipse.jetty.session.infinispan.&lt;/Arg&gt;
&lt;/Call&gt;
&lt;!-- Get a reference to the InfinispanSessionIdManager --&gt;
&lt;Ref id="Server"&gt;
&lt;Call id="idMgr" name="getSessionIdManager"/&gt;
&lt;/Ref&gt;
&lt;!-- Get a referencee to the Cache from the InfinispanSessionIdManager --&gt;
&lt;Ref id="idMgr"&gt;
&lt;Get id="cache" name="cache"/&gt;
&lt;/Ref&gt;
&lt;!-- Use the InfinispanSessionIdManager and Cache to setup up the InfinispanSessionManager --&gt;
&lt;Set name="sessionHandler"&gt;
&lt;New class="org.eclipse.jetty.server.session.SessionHandler"&gt;
&lt;Arg&gt;
&lt;New id="mgr" class="org.eclipse.jetty.session.infinispan.InfinispanSessionManager"&gt;
&lt;Set name="sessionIdManager"&gt;
&lt;Ref id="idMgr"/&gt;
&lt;/Set&gt;
&lt;Set name="cache"&gt;
&lt;Ref id="cache"&gt;
&lt;/Ref&gt;
&lt;/Set&gt;
&lt;Set name="scavengeInterval"&gt;60&lt;/Set&gt;
&lt;/New&gt;
&lt;/Arg&gt;
&lt;/New&gt;
&lt;/Set&gt;</code></pre><p>From a <code class="literal">WEB-INF/jetty-web.xml</code> file, you can reference the Server instance directly:</p><pre xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><code> &lt;!-- Expose the jetty infinispan classes for session serialization --&gt;
&lt;Call name="prependServerClass"&gt;
&lt;Arg&gt;-org.eclipse.jetty.session.infinispan.&lt;/Arg&gt;
&lt;/Call&gt;
&lt;!-- Reference the server directly --&gt;
&lt;Get name="server"&gt;
&lt;Get id="idMgr" name="sessionIdManager"/&gt;
&lt;/Get&gt;
&lt;!-- Get a reference to the Cache via the InfinispanSessionIdManager --&gt;
&lt;Ref id="idMgr"&gt;
&lt;Get id="cache" name="cache"/&gt;
&lt;/Ref&gt;
&lt;!-- Apply the SessionIdManager and Cache to the InfinispanSessionManager --&gt;
&lt;Set name="sessionHandler"&gt;
&lt;New class="org.eclipse.jetty.server.session.SessionHandler"&gt;
&lt;Arg&gt;
&lt;New id="mgr" class="org.eclipse.jetty.session.infinispan.InfinispanSessionManager"&gt;
&lt;Set name="sessionIdManager"&gt;
&lt;Ref id="idMgr"/&gt;
&lt;/Set&gt;
&lt;Set name="cache"&gt;
&lt;Ref id="cache"&gt;
&lt;/Ref&gt;
&lt;/Set&gt;
&lt;Set name="scavengeInterval"&gt;600&lt;/Set&gt;
&lt;/New&gt;
&lt;/Arg&gt;
&lt;/New&gt;
&lt;/Set&gt;</code></pre><p>The InfinispanSessionManager can be provided by calling setters:</p><div class="variablelist"><dl class="variablelist"><dt><span class="term">scavengeInterval</span></dt><dd>Time in seconds between runs of a scavenger task that looks for expired old sessions to delete.
The default is 10 minutes.</dd><dt><span class="term">staleIntervalSec</span></dt><dd>The length of time a session can be in memory without being checked against the cluster.
A value of 0 indicates that the session is never checked against the cluster - the current node is considered to be the master for the session.</dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="_using_hotrod"></a>Using HotRod</h4></div></div></div><p>If you&#8217;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 <code class="literal">prependServerClass</code> to your context xml file as shown above.</p><p>Then, create the file <code class="literal">${jetty.base}/resources/hotrod-client.properties</code>.
Add the following line to this file:</p><pre class="literallayout">infinispan.client.hotrod.marshaller=org.eclipse.jetty.session.infinispan.WebAppMarshaller</pre></div></div></div><script type="text/javascript">
SyntaxHighlighter.all()
</script><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="session-clustering-mongodb.html"><i class="fa fa-chevron-left" aria-hidden="true"></i> Previous</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="session-management.html"><i class="fa fa-chevron-up" aria-hidden="true"></i> Top</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="configuring-sessions-hazelcast.html">Next <i class="fa fa-chevron-right" aria-hidden="true"></i></a></td></tr><tr><td width="40%" align="left" valign="top">Session Clustering with MongoDB&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html"><i class="fa fa-home" aria-hidden="true"></i> Home</a></td><td width="40%" align="right" valign="top">&nbsp;Clustered Session Management: Hazelcast</td></tr></table></div><p xmlns:jfetch="java:org.eclipse.jetty.xslt.tools.JavaSourceFetchExtension" xmlns:fetch="java:org.eclipse.jetty.xslt.tools.SourceFetchExtension" xmlns:d="http://docbook.org/ns/docbook" xmlns:l="http://docbook.sourceforge.net/xmlns/l10n/1.0" xmlns:xslthl="http://xslthl.sf.net" xmlns:gcse="http://www.google.com" xmlns:date="http://exslt.org/dates-and-times"><div class="jetty-callout">
See an error or something missing?
<span class="callout"><a href="http://github.com/eclipse/jetty.project">Contribute to this documentation at
<span class="website"><i class="fa fa-github" aria-hidden="true"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2019-11-05)</i></span></div></p></body></html>