| <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 10. 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> </td><th width="60%" align="center">Chapter 10. 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"> <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’ve done that, you will need to prevent Jetty’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’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’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><?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></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’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’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> <!-- 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></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> <!-- 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></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’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> </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"> <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 </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"> 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> |