blob: 1732da0b5dde216d6f9f7ec10c0e78ac10a979d5 [file] [log] [blame]
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>StatelessContainer Configuration</title>
<meta name="description" content="Apache TomEE">
<meta name="author" content="Apache TomEE">
<meta name="google-translate-customization" content="f36a520c08f4c9-0a04e86a9c075ce9-g265f3196f697cf8f-10">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
<meta http-equiv="Cache-Control" content="no-store, no-cache, must-revalidate, max-age=0">
<!-- Le HTML5 shim, for IE6-8 support of HTML elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<!-- Le styles -->
<link href="./resources/css/bootstrap.css" rel="stylesheet">
<link href="./resources/css/prettify.css" rel="stylesheet">
<!--link href="./resources/css/bootstrap-mods.css" rel="stylesheet"-->
<link href="./resources/css/main.css" rel="stylesheet">
<link href="./resources/font-awesome-4.6.3/css/font-awesome.min.css" rel="stylesheet">
<script type="text/javascript">
var t = encodeURIComponent(document.title.replace(/^\s+|\s+$/g,""));
var u = encodeURIComponent(""+document.URL);
function fbshare () {
window.open(
"http://www.facebook.com/sharer/sharer.php?u="+u,
'Share on Facebook',
'width=640,height=426');
};
function gpshare () {
window.open(
"https://plus.google.com/share?url="+u,
'Share on Google+',
'width=584,height=385');
};
function twshare () {
window.open(
"https://twitter.com/intent/tweet?url="+u+"&text="+t,
'Share on Twitter',
'width=800,height=526');
};
function pinshare () {
window.open("//www.pinterest.com/pin/create/button/?url="+u+"&media=http%3A%2F%2Ftomee.apache.org%2Fresources%2Fimages%2Ffeather-logo.png&description="+t,
'Share on Pinterest',
'width=800,height=526');
};
</script>
<!-- Le fav and touch icons -->
<link rel="shortcut icon" href="./favicon.ico">
<link rel="apple-touch-icon" href="./resources/images/apple-touch-icon.png">
<link rel="apple-touch-icon" sizes="72x72" href="./resources/images/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="114x114" href="./resources/images/apple-touch-icon-114x114.png">
<script src="./resources/js/prettify.js" type="text/javascript"></script>
<script src="./resources/js/jquery-latest.js"></script>
<script src="http://platform.twitter.com/widgets.js" type="text/javascript"></script>
<script src="./resources/js/common.js"></script>
<script src="./resources/js/prettyprint.js"></script>
<!--script src="//assets.pinterest.com/js/pinit.js" type="text/javascript" async></script//-->
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-2717626-1']);
_gaq.push(['_setDomainName', 'apache.org']);
_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>
</head>
<body>
<div class="topbar" data-dropdown="dropdown">
<div class="fill">
<div class="container">
<a class="brand" href="./index.html">Apache TomEE</a>
<ul class="nav">
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
Apache
<b class="caret"></b>
</a>
<ul class="dropdown-menu">
<!-- <li><a href="./misc/whoweare.html">Who we are?</a></li> -->
<!-- <li><a href="./misc/heritage.html">Heritage</a></li> -->
<li><a href="http://www.apache.org">Apache Home</a></li>
<!-- <li><a href="./misc/resources.html">Resources</a></li> -->
<li><a href="./misc/contact.html">Contact</a></li>
<li><a href="./misc/legal.html">Legal</a></li>
<li><a href="http://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li>
<li><a href="http://www.apache.org/foundation/thanks.html">Thanks</a></li>
<li class="divider"/>
<li><a href="http://www.apache.org/security">Security</a></li>
</ul>
</li>
<li><a href="./index.html">Home</a></li>
<li><a href="./downloads.html">Downloads</a></li>
<li><a href="./documentation.html">Documentation</a></li>
<li><a href="./examples-trunk/index.html">Examples</a></li>
<li><a href="./support.html">Support</a></li>
<li><a href="./contribute.html">Contribute</a></li>
<li><a href="./security/index.html">Security</a></li>
</ul>
<!-- Google CSE Search Box Begins -->
<FORM class="pull-right" id="searchbox_010475492895890475512:_t4iqjrgx90" action="http://www.google.com/cse">
<INPUT type="hidden" name="cx" value="010475492895890475512:_t4iqjrgx90">
<INPUT type="hidden" name="cof" value="FORID:0">
<INPUT size="18" width="130" style="width:130px" name="q" type="text" placeholder="Search">
</FORM>
<!--<SCRIPT type="text/javascript" src="http://www.google.com/coop/cse/brand?form=searchbox_010475492895890475512:_t4iqjrgx90"></SCRIPT>-->
<!-- Google CSE Search Box Ends -->
</div>
</div>
</div>
<div class="container">
<div class="page-header">
<small><a href="./index.html">Home</a></small><br>
<h1>StatelessContainer Configuration
<div style="float: right; position: relative; bottom: -10px; ">
<a onclick="javascript:gpshare()" class="gp-share sprite" title="Share on Google+">share [gp]</a>
<a onclick="javascript:fbshare()" class="fb-share sprite" title="Share on Facebook">share [fb]</a>
<a onclick="javascript:twshare()" class="tw-share sprite" title="Share on Twitter">share [tw]</a>
<a onclick="javascript:pinshare()" class="pin-share sprite" title="Share on Pinterest">share [pin]</a>
<a data-toggle="modal" href="#edit" class="edit-page" title="Contribute to this Page">contribute</a>
</div>
</h1>
</div>
<p>A StatelessContainer can be declared via xml in the <code>&lt;tomee-home&gt;/conf/tomee.xml</code> file or in a <code>WEB-INF/resources.xml</code> file using a declaration like the following. All properties in the element body are optional.</p>
<pre><code>&lt;Container id="myStatelessContainer" type="STATELESS"&gt;
accessTimeout = 30 seconds
callbackThreads = 5
closeTimeout = 5 minutes
garbageCollection = false
idleTimeout = 0 minutes
maxAge = 0 hours
maxAgeOffset = -1
maxSize = 10
minSize = 0
replaceAged = true
replaceFlushed = false
strictPooling = true
sweepInterval = 5 minutes
&lt;/Container&gt;
</code></pre>
<p>Alternatively, a StatelessContainer can be declared via properties in the <code>&lt;tomee-home&gt;/conf/system.properties</code> file or via Java VirtualMachine <code>-D</code> properties. The properties can also be used when embedding TomEE via the <code>javax.ejb.embeddable.EJBContainer</code> API or <code>InitialContext</code></p>
<pre><code>myStatelessContainer = new://Container?type=STATELESS
myStatelessContainer.accessTimeout = 30 seconds
myStatelessContainer.callbackThreads = 5
myStatelessContainer.closeTimeout = 5 minutes
myStatelessContainer.garbageCollection = false
myStatelessContainer.idleTimeout = 0 minutes
myStatelessContainer.maxAge = 0 hours
myStatelessContainer.maxAgeOffset = -1
myStatelessContainer.maxSize = 10
myStatelessContainer.minSize = 0
myStatelessContainer.replaceAged = true
myStatelessContainer.replaceFlushed = false
myStatelessContainer.strictPooling = true
myStatelessContainer.sweepInterval = 5 minutes
</code></pre>
<p>Properties and xml can be mixed. Properties will override the xml allowing for easy configuration change without the need for ${} style variable substitution. Properties are not case sensitive. If a property is specified that is not supported by the declared StatelessContainer a warning will be logged. If a StatelessContainer is needed by the application and one is not declared, TomEE will create one dynamically using default settings. Multiple StatelessContainer declarations are allowed.</p>
<h1>Supported Properties</h1>
<table>
<tr>
<th>Property</th>
<th>Type</th>
<th>Default</th>
<th>Description</th>
</tr>
<tr>
<td><a href="#accessTimeout">accessTimeout</a></td>
<td><a href="configuring-durations.html">time</a></td>
<td>30&nbsp;seconds</td>
<td>
Specifies the time an invokation should wait for an instance
of the pool to become available.
</td>
</tr>
<tr>
<td><a href="#callbackThreads">callbackThreads</a></td>
<td>int</td>
<td>5</td>
<td>
The number of threads for constructing and destroying beans.
</td>
</tr>
<tr>
<td><a href="#closeTimeout">closeTimeout</a></td>
<td><a href="configuring-durations.html">time</a></td>
<td>5&nbsp;minutes</td>
<td>
Maximum time to wait for instances to be destroyed when shutting down the pool
</td>
</tr>
<tr>
<td><a href="#garbageCollection">garbageCollection</a></td>
<td>boolean</td>
<td>false</td>
<td>
Allows Garbage Collection to be used as a mechanism for shrinking
the pool.
</td>
</tr>
<tr>
<td><a href="#idleTimeout">idleTimeout</a></td>
<td><a href="configuring-durations.html">time</a></td>
<td>0&nbsp;minutes</td>
<td>
Specifies the maximum time that an instance should be allowed to
sit idly in the pool without use before it should be retired and
removed.
</td>
</tr>
<tr>
<td><a href="#maxAge">maxAge</a></td>
<td><a href="configuring-durations.html">time</a></td>
<td>0&nbsp;hours</td>
<td>
Specifies the maximum time that an instance should live before
it should be retired and removed from use.
</td>
</tr>
<tr>
<td><a href="#maxAgeOffset">maxAgeOffset</a></td>
<td>int</td>
<td>-1</td>
<td>
Applies to MaxAge usage and would rarely be changed, but is a
nice feature to understand.
</td>
</tr>
<tr>
<td><a href="#maxSize">maxSize</a></td>
<td>int</td>
<td>10</td>
<td>
Specifies the size of the instance pool for this stateless
SessionBean container.
</td>
</tr>
<tr>
<td><a href="#minSize">minSize</a></td>
<td>int</td>
<td>0</td>
<td>
Specifies the minimum number of bean instances that should be in
the pool for each bean.
</td>
</tr>
<tr>
<td><a href="#replaceAged">replaceAged</a></td>
<td>boolean</td>
<td>true</td>
<td>
When `ReplaceAged` is enabled, any instances in the pool that
expire due to reaching their `MaxAge` will be replaced immediately
so that the pool will remain at its current size.
</td>
</tr>
<tr>
<td><a href="#replaceFlushed">replaceFlushed</a></td>
<td>boolean</td>
<td>false</td>
<td>
When `ReplaceFlushed` is enabled, any instances in the pool that
are flushed will be replaced immediately so that the pool will
remain at its current size.
</td>
</tr>
<tr>
<td><a href="#strictPooling">strictPooling</a></td>
<td>boolean</td>
<td>true</td>
<td>
StrictPooling tells the container what to do when the pool
reaches it's maximum size and there are incoming requests that
need instances.
</td>
</tr>
<tr>
<td><a href="#sweepInterval">sweepInterval</a></td>
<td><a href="configuring-durations.html">time</a></td>
<td>5&nbsp;minutes</td>
<td>
The frequency in which the container will sweep the pool and
evict expired instances.
</td>
</tr>
</table>
<p><a name="accessTimeout"></a></p>
<h2>accessTimeout</h2>
<p>Specifies the time an invokation should wait for an instance
of the pool to become available.</p>
<p>After the timeout is reached, if an instance in the pool cannot
be obtained, the method invocation will fail.</p>
<p>Usable time units: nanoseconds, microsecons, milliseconds,
seconds, minutes, hours, days. Or any combination such as
"1 hour and 27 minutes and 10 seconds"</p>
<p>Any usage of the <code>javax.ejb.AccessTimeout</code> annotation will
override this setting for the bean or method where the
annotation is used.</p>
<p><a name="callbackThreads"></a></p>
<h2>callbackThreads</h2>
<p>The number of threads for constructing and destroying beans.</p>
<p>When sweeping the pool for expired instances a thread pool is
used to process calling <code>@PreDestroy</code> on expired instances as well
as creating new instances as might be required to fill the pool
to the minimum after a Flush or <code>MaxAge</code> expiration. The
<code>CallbackThreads</code> setting dictates the size of the thread pool and
is shared by all beans deployed in the container.</p>
<p><a name="closeTimeout"></a></p>
<h2>closeTimeout</h2>
<p>Maximum time to wait for instances to be destroyed when shutting down the pool</p>
<p>PostConstruct methods are invoked on all instances in the pool
when the bean is undeployed and its pool is closed. The
<code>CloseTimeout</code> specifies the maximum time to wait for the pool to
close and <code>PostConstruct</code> methods to be invoked.</p>
<p>Usable time units: nanoseconds, microsecons, milliseconds,
seconds, minutes, hours, days. Or any combination such as
<code>1 hour and 27 minutes and 10 seconds</code></p>
<p><a name="garbageCollection"></a></p>
<h2>garbageCollection</h2>
<p>Allows Garbage Collection to be used as a mechanism for shrinking
the pool.</p>
<p>When set to true all instances in the pool, excluding
the minimum, are eligible for garbage collection by the virtual
machine as per the rules of <code>java.lang.ref.SoftReference</code> and can be
claimed by the JVM to free memory. Instances garbage collected
will have their <code>@PreDestroy</code> methods called during finalization.</p>
<p>In the OpenJDK VM the <code>-XX:SoftRefLRUPolicyMSPerMB</code> flag can adjust
how aggressively SoftReferences are collected. The default
OpenJDK setting is 1000, resulting in inactive pooled instances
living one second of lifetime per free megabyte in the heap, which
is very aggressive. The setting should be increased to get the
most out of the <code>GarbageCollection</code> feature of the pool. Much
higher settings are safe. Even a setting as high as 3600000 (1
hour per free MB in the heap) does not affect the ability for the
VM to garbage collect SoftReferences in the event that memory is
needed to avoid an <code>OutOfMemoryException</code>.</p>
<p><a name="idleTimeout"></a></p>
<h2>idleTimeout</h2>
<p>Specifies the maximum time that an instance should be allowed to
sit idly in the pool without use before it should be retired and
removed.</p>
<p>Only instances
in surplus of the pool's <code>MinSize</code> are eligible to expire via <code>IdleTimeout</code>
Instances that expire due to <code>IdleTimeout</code> will have their <code>@PreDestroy</code>
methods invoked before being completely destroyed.</p>
<p>Usable time units: nanoseconds, microsecons, milliseconds,
seconds, minutes, hours, days. Or any combination such as
"1 hour and 27 minutes and 10 seconds"</p>
<p><a name="maxAge"></a></p>
<h2>maxAge</h2>
<p>Specifies the maximum time that an instance should live before
it should be retired and removed from use.</p>
<p>This will happen
gracefully. Useful for situations where bean instances are
designed to hold potentially expensive resources such as memory
or file handles and need to be periodically cleared out.</p>
<p>Usable time units: nanoseconds, microsecons, milliseconds,
seconds, minutes, hours, days. Or any combination such as
<code>1 hour and 27 minutes and 10 seconds</code></p>
<p><a name="maxAgeOffset"></a></p>
<h2>maxAgeOffset</h2>
<p>Applies to MaxAge usage and would rarely be changed, but is a
nice feature to understand.</p>
<p>When the container first starts and the pool is filled to the
minimum size, all those "minimum" instances will have the same
creation time and therefore all expire at the same time dictated
by the <code>MaxAge</code> setting. To protect against this sudden drop
scenario and provide a more gradual expiration from the start
the container will spread out the age of the instances that fill
the pool to the minimum using an offset.</p>
<p>The <code>MaxAgeOffset</code> is not the final value of the offset, but
rather it is used in creating the offset and allows the
spreading to push the initial ages into the future or into the
past. The pool is filled at startup as follows:</p>
<pre><code>for (int i = 0; i &lt; poolMin; i++) {
long ageOffset = (maxAge / poolMin * i * maxAgeOffset) % maxAge;
pool.add(new Bean(), ageOffset));
}
</code></pre>
<p>The default <code>MaxAgeOffset</code> is -1 which causes the initial
instances in the pool to live a bit longer before expiring. As
a concrete example, let's say the MinSize is 4 and the MaxAge is
100 years. The generated offsets for the four instances created
at startup would be 0, -25, -50, -75. So the first instance
would be "born" at age 0, die at 100, living 100 years. The
second instance would be born at -25, die at 100, living a total
of 125 years. The third would live 150 years. The fourth 175
years.</p>
<p>A <code>MaxAgeOffset</code> of 1 would cause instances to be "born" older
and therefore die sooner. Using the same example <code>MinSize</code> of 4
and <code>MaxAge</code> of <code>100 years</code>, the life spans of these initial four
instances would be 100, 75, 50, and 25 years respectively.</p>
<p>A <code>MaxAgeOffset</code> of 0 will cause no "spreading" of the age of the
first instances used to fill the pool to the minimum and these
instances will of course reach their MaxAge at the same time.
It is possible to set to decimal values such as -0.5, 0.5, -1.2,
or 1.2.</p>
<p><a name="maxSize"></a></p>
<h2>maxSize</h2>
<p>Specifies the size of the instance pool for this stateless
SessionBean container.</p>
<p>Each <code>@Stateless</code> bean will get its own instance pool.
If StrictPooling is not used, instances
will still be created beyond this number if there is demand, but
they will not be returned to the pool and instead will be
immediately expire.</p>
<p><a name="minSize"></a></p>
<h2>minSize</h2>
<p>Specifies the minimum number of bean instances that should be in
the pool for each bean.</p>
<p>Pools are prefilled to the minimum on
startup. Note this will create start order dependencies between
other beans that also eagerly start, such as other <code>@Stateless</code>
beans with a minimum or <code>@Singleton</code> beans using <code>@Startup</code>. The
<code>@DependsOn</code> annotation can be used to appropriately influence
start order.</p>
<p>The minimum pool size is rigidly maintained. Instances in the
minimum side of the pool are not eligible for <code>IdleTimeout</code> or
<code>GarbageCollection</code>, but are subject to <code>MaxAge</code> and flushing.</p>
<p>If the pool is flushed it is immediately refilled to the minimum
size with <code>MaxAgeOffset</code> applied. If an instance from the minimum
side of the pool reaches its <code>MaxAge</code>, it is also immediately
replaced. Replacement is done in a background queue using the
number of threads specified by <code>CallbackThreads</code>.</p>
<p><a name="replaceAged"></a></p>
<h2>replaceAged</h2>
<p>When <code>ReplaceAged</code> is enabled, any instances in the pool that
expire due to reaching their <code>MaxAge</code> will be replaced immediately
so that the pool will remain at its current size.</p>
<p>Replacement
is done in a background queue so that incoming threads will not
have to wait for instance creation.</p>
<p>The aim of his option is to prevent user requests from paying
the instance creation cost as <code>MaxAge</code> is enforced, potentially
while under heavy load at peak hours.</p>
<p>Instances from the minimum side of the pool are always replaced
when they reach their <code>MaxAge</code>, this setting dictates the
treatment of non-minimum instances.</p>
<p><a name="replaceFlushed"></a></p>
<h2>replaceFlushed</h2>
<p>When <code>ReplaceFlushed</code> is enabled, any instances in the pool that
are flushed will be replaced immediately so that the pool will
remain at its current size.</p>
<p>Replacement is done in a background
queue so that incoming threads will not have to wait for
instance creation.</p>
<p>The aim of his option is to prevent user requests from paying
the instance creation cost if a flush performed while under
heavy load at peak hours.</p>
<p>Instances from the minimum side of the pool are always replaced
when they are flushed, this setting dictates the treatment of
non-minimum instances.</p>
<p>A bean may flush its pool by casting the <code>SessionContext</code> to
<code>Flushable</code> and calling <code>flush()</code>. See <code>SweepInterval</code> for details on
how flush is performed.</p>
<pre><code>import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import java.io.Flushable;
import java.io.IOException;
public class MyBean {
private SessionContext sessionContext;
public void flush() throws IOException {
((Flushable) sessionContext).flush();
}
}
</code></pre>
<p><a name="strictPooling"></a></p>
<h2>strictPooling</h2>
<p>StrictPooling tells the container what to do when the pool
reaches it's maximum size and there are incoming requests that
need instances.</p>
<p>With strict pooling, requests will have to wait for instances to
become available. The pool size will never grow beyond the the
set <code>MaxSize</code> value. The maximum amount of time a request should
wait is specified via the <code>AccessTimeout</code> setting.</p>
<p>Without strict pooling, the container will create temporary
instances to meet demand. The instances will last for just one
method invocation and then are removed.</p>
<p>Setting <code>StrictPooling</code> to <code>false</code> and <code>MaxSize</code> to <code>0</code> will result in
no pooling. Instead instances will be created on demand and live
for exactly one method call before being removed.</p>
<p><a name="sweepInterval"></a></p>
<h2>sweepInterval</h2>
<p>The frequency in which the container will sweep the pool and
evict expired instances.</p>
<p>Eviction is how the <code>IdleTimeout</code>,
<code>MaxAge</code>, and pool "flush" functionality is enforced. Higher
intervals are better.</p>
<p>Instances in use are excluded from sweeping. Should an instance
expire while in use it will be evicted immediately upon return
to the pool. Effectively <code>MaxAge</code> and flushes will be enforced as
a part of normal activity or sweeping, while IdleTimeout is only
enforcable via sweeping. This makes aggressive sweeping less
important for a pool under moderate load.</p>
<p>Usable time units: nanoseconds, microsecons, milliseconds,
seconds, minutes, hours, days. Or any combination such as
<code>1 hour and 27 minutes and 10 seconds</code></p>
<div id="edit" class="modal hide fade in" style="display: none; ">
<div class="modal-header">
<a class="close" data-dismiss="modal">x</a>
<h3>Thank you for contributing to the documentation!</h3>
</div>
<div class="modal-body">
<h4>Any help with the documentation is greatly appreciated.</h4>
<p>All edits are reviewed before going live, so feel free to do much more than fix typos or links. If you see a page that could benefit from an entire rewrite, we'd be thrilled to review it. Don't be surprised if we like it so much we ask you for help with other pages :)</p>
<small>NOTICE: unless indicated otherwise on the pages in question, all editable content available from apache.org is presumed to be licensed under the Apache License (AL) version 2.0 and hence all submissions to apache.org treated as formal Contributions under the license terms.</small>
<!--[if gt IE 6]>
<h4>Internet Explorer Users</h4>
<p>If you are not an Apache committer, click the Yes link and enter a <i>anonymous</i> for the username and leave the password empty</p>
<![endif]-->
</div>
<div class="modal-footer">
Do you have an Apache ID?
<a href="javascript:void(location.href='https://cms.apache.org/redirect?uri='+escape(location.href))" class="btn">Yes</a>
<a href="javascript:void(location.href='https://anonymous:@cms.apache.org/redirect?uri='+escape(location.href))" class="btn">No</a>
</div>
</div>
<script src="./resources/js/bootstrap-modal.js"></script>
<footer>
<p>Copyright &copy; 1999-2016 The Apache Software Foundation, Licensed under the Apache License, Version 2.0.
Apache TomEE, TomEE, Apache, the Apache feather logo, and the Apache TomEE project logo are trademarks of The Apache Software Foundation.
All other marks mentioned may be trademarks or registered trademarks of their respective owners.</p>
</footer>
</div> <!-- /container -->
<!-- Javascript
================================================== -->
<!-- Placed at the end of the document so the pages load faster -->
<script src="./resources/js/bootstrap-dropdown.js"></script>
</body>
</html>