| <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-NS Stylesheets V1.76.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 : The Definitive Reference"><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="jndi.html" title="Chapter 11. Configuring JNDI"><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"><script type="text/javascript" src="js/shCore.js"></script><script type="text/javascript" src="js/shBrushJava.js"></script><script type="text/javascript" src="js/shBrushXml.js"></script><script type="text/javascript" src="js/shBrushBash.js"></script><script type="text/javascript" src="js/shBrushJScript.js"></script><script type="text/javascript" src="js/shBrushSql.js"></script><script type="text/javascript" src="js/shBrushProperties.js"></script><script type="text/javascript" src="js/shBrushPlain.js"></script><link type="text/css" rel="stylesheet" href="css/shCore.css"><link type="text/css" rel="stylesheet" href="css/shThemeEclipse.css"><link type="text/css" rel="stylesheet" href="css/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.0.v20150612</span></td><td style="width: 50%"><script type="text/javascript"> (function() { |
| var cx = '016459005284625897022:obd4lsai2ds'; |
| var gcse = document.createElement('script'); |
| gcse.type = 'text/javascript'; |
| gcse.async = true; |
| gcse.src = (document.location.protocol == 'https:' ? 'https:' : 'http:') + |
| '//www.google.com/cse/cse.js?cx=' + cx; |
| var s = document.getElementsByTagName('script')[0]; |
| s.parentNode.insertBefore(gcse, s); |
| })(); |
| </script><gcse:search></gcse:search></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="icon-chevron-left"></i> Previous</a> </td><th width="60%" align="center">Chapter 10. Session Management<br><a accesskey="p" href="index.html"><i class="icon-home"></i> Home</a></th><td width="20%" align="right"> <a accesskey="n" href="jndi.html">Next <i class="icon-chevron-right"></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 from 1 day to full product delivery |
| </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><dt><span class="section"><a href="session-clustering-infinispan.html#d0e9766">Configuration</a></span></dt><dt><span class="section"><a href="session-clustering-infinispan.html#d0e9783">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 Session.setAttribute() 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="d0e9766"></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" type="disc"><li class="listitem"><p>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.</p></li><li class="listitem"><p>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.</p></li></ul></div><p>These managers also cooperate and collaborate with the |
| <code class="code">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="d0e9783"></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 <code class="code">infinispan</code> |
| <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 --add-to-start or |
| --add-to-startd 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="code">${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="code">--skip-file-validation=infinispan</code> argument to start.jar on |
| the command line, or place that line inside |
| <code class="code">${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="code">start.d/infinispan.ini</code> file or appended to your |
| <code class="code">start.ini</code>, depending on how you enabled the module:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: plain;toolbar: false"> |
| <![CDATA[## Unique identifier for this node in the cluster |
| jetty.infinispanSession.workerName=node1 |
| ]]> |
| </script></div><div class="variablelist"><dl><dt><span class="term">jetty.infinispanSession.workerName</span></dt><dd><p>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="bold"><strong>each</strong></span> node on which |
| you enable infinispan session clustering.</p></dd></dl></div><p>These properties are applied to the |
| InfinispanSessionIdManager described below.</p><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e9834"></a>Configuring the InfinispanSessionIdManager</h4></div></div></div><p>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:</p><script type="syntaxhighlighter" class="brush: xml;toolbar: false"> |
| <![CDATA[<?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"> |
| <Get id="cache" name="cache">sessions</Get> |
| </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> |
| ]]> |
| </script><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 InfinispanSessionIdManager 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 (ie 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 <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 InfinispanSessionIdManager can be configured by calling |
| setters:</p><p></p><div class="variablelist"><dl><dt><span class="term">idleExpiryMultiple</span></dt><dd><p> Sessions that are not immortal, ie 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. </p></dd></dl></div></div><div class="section"><div class="titlepage"><div><div><h4 class="title"><a name="d0e9869"></a>Configuring the InfinispanSessionManager</h4></div></div></div><p>As mentioned elsewhere, there should be one |
| InfinispanSessionManager per context (ie webapp). It will need to |
| reference the single InfinispanSessionIdManager configured previously |
| for the Server.</p><p>The way you configure a InfinispanSessionManager depends on |
| whether you're configuring from a context xml file or a |
| <code class="filename">jetty-web.xml</code> file or code. The basic difference is |
| how you get a reference to the Jetty |
| <code class="code">org.eclipse.jetty.server.Server</code> instance.</p><p>From a context xml file, you reference the Server instance as a |
| Ref:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false"> |
| <![CDATA[ |
| <!-- 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> |
| |
| ]]> |
| </script></div><p>From a <code class="filename">WEB-INF/jetty-web.xml</code> file, you can |
| reference the Server instance directly:</p><div class="informalexample"><script type="syntaxhighlighter" class="brush: xml;toolbar: false"> |
| <![CDATA[ |
| <!-- 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> |
| |
| ]]> |
| </script></div><p>The InfinispanSessionManager can be provided by calling |
| setters:</p><div class="variablelist"><dl><dt><span class="term">scavengeInterval</span></dt><dd><p>Time in seconds between runs of a scavenger task that looks |
| for expired old sessions to delete. The default is 10 |
| minutes.</p></dd><dt><span class="term">staleIntervalSec</span></dt><dd><p>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. </p></dd></dl></div></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="icon-chevron-left"></i> Previous</a> </td><td width="20%" align="center"><a accesskey="u" href="session-management.html"><i class="icon-chevron-up"></i> Top</a></td><td width="40%" align="right"> <a accesskey="n" href="jndi.html">Next <i class="icon-chevron-right"></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="icon-home"></i> Home</a></td><td width="40%" align="right" valign="top"> Chapter 11. Configuring JNDI</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/jetty-project/jetty-documentation">Contribute to this documentation at |
| <span class="website"><i class="icon-github"></i> Github!</span></a></span><span style="float: right"><i>(Generated: 2015-06-15T13:18:21-05:00)</i></span></div></p><script 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" type="text/javascript"> |
| var _gaq = _gaq || []; |
| _gaq.push(['_setAccount', 'UA-1149868-7']); |
| _gaq.push(['_trackPageview']); |
| |
| (function() { |
| var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; |
| ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; |
| var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); |
| })(); |
| </script></body></html> |