How to configure the maximum number of active sessions allowed in JBoss/Tomcat?
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 4.x
- 5.x
- 6.x
- 7.x
- Tomcat
Issue
- Is there any settings where we can specify the maximum number of active sesssions allowed? If not, is there any upper limit on number of active sessions?
Resolution
JBoss EAP 7.x:
- non-
<distributable/>application:
You can specify configure the default value ofmax-sessions (defaults to -1 which means no limit) in undertow subsystem. For example:
/subsystem=undertow/servlet-container=default:write-attribute(name=max-sessions,value=1000)
the above CLI will result in the following configuration in standalone.xml:
<subsystem xmlns="urn:jboss:domain:undertow:3.1">
...
<servlet-container name="default" max-sessions="1000">
<jsp-config/>
<websockets/>
</servlet-container>
...
</subsystem>
The above setting means each web application can have 1000 active sessions. When exceeding this limit, a new request will fail with the following IllegalStateException:
Caused by: java.lang.IllegalStateException: UT000121: Session was rejected as the maximum number of sessions (1000) has been hit
at io.undertow.server.session.InMemorySessionManager.createSession(InMemorySessionManager.java:154)
at io.undertow.servlet.spec.ServletContextImpl.getSession(ServletContextImpl.java:758)
at io.undertow.servlet.spec.HttpServletRequestImpl.getSession(HttpServletRequestImpl.java:370)
at io.undertow.servlet.spec.HttpServletRequestImpl.getSession(HttpServletRequestImpl.java:375)
...(snip)...
If you want to tune the max active sessions for each web application, you can set <max-active-sessions> in WEB-INF/jboss-web.xml. This would override any undertow subsystem max-sessions value:
<jboss-web>
<max-active-sessions>2000</max-active-sessions>
</jboss-web>
<distributable/>application:
You can use the same configuration for <distributable/> is enabled. However, note that the behavior is different from non-<distributable/> case.
A new request is NOT rejected even if the number of active sessions exceeds <max-active-sessions> . An old session is passivated to disk using a Least Recently Used (LRU) algorithm. Passivated session data is written to an appname.dat file the JBoss data/infinispan/web/ directory by default. See also <https://access.redhat.com/documentation/en-us/red_hat_jboss_enterprise_application_platform/7.0/html/development_guide/clustering_in_web_applications#http_session_passivation_and_activation> for details.
JBoss EAP 6.x:
<distributable/>application:
To configure the maximum number of active sesssions allowed for your web application, you can set <max-active-sessions> to your web application's WEB-INF/jboss-web.xml in the same way as EAP 4.x/5.x:
<jboss-web>
<max-active-sessions>1000</max-active-sessions>
</jboss-web>
- non-
<distributable/>application:
You can configure the default value of "maxActiveSessions" for the container by specifying the system property.:
-Dorg.apache.catalina.session.StandardManager.MAX_ACTIVE_SESSIONS=1000
(If you do not configure the setting explicitly, the default value is -1 which means no limit.)
But there is no configurable parameter to set the maximum number of active sessions allowed for each web application.
As workaround, you can modify maxActiveSessions attribute of JMX MBean jboss.web:type=Manager,host=default-host,path=/myapp after start-up with setting the following system property:
-Dorg.apache.tomcat.util.ENABLE_MODELER=true
Or you can try to implements LifecycleListener like the following:
package com.redhat.jboss.support;
import org.apache.catalina.Lifecycle;
import org.apache.catalina.LifecycleEvent;
import org.apache.catalina.LifecycleListener;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.session.StandardManager;
import org.jboss.logging.Logger;
public class SetMaxActiveSessionsListener implements LifecycleListener {
private static Logger log = Logger.getLogger(SetMaxActiveSessionsListener.class);
protected int maxActiveSessions = -1;
public void lifecycleEvent(LifecycleEvent event) {
String type = event.getType();
if (Lifecycle.AFTER_START_EVENT.equals(type)) {
StandardContext context = (StandardContext) event.getSource();
StandardManager manager = (StandardManager) context.getManager();
log.debug("manager.setMaxActiveSessions(" + maxActiveSessions + ")");
manager.setMaxActiveSessions(maxActiveSessions);
}
}
public int getMaxActiveSessions() {
log.debug("SetMaxActiveSessionsListener#getMaxActiveSessions() is called. maxActiveSessions = " + maxActiveSessions);
return maxActiveSessions;
}
public void setMaxActiveSessions(String value) {
log.debug("SetMaxActiveSessionsListener#setMaxActiveSessions() is called. " + value + " is set.");
this.maxActiveSessions = Integer.parseInt(value);
}
}
then set it in <listener> in your web application's 'WEB-INF/jboss-web.xml like the following:
<jboss-web>
<listener>
<class-name>com.redhat.jboss.support.SetMaxActiveSessionsListener</class-name>
<param>
<param-name>maxActiveSessions</param-name>
<param-value>1000</param-value>
</param>
</listener>
</jboss-web>
Please see the attached example SetMaxActiveSessionsListener.tar.gz for details.
JBoss EAP 4.x/5.x
- Clustered environment = Using
allorproductionbased profile and enabling session replication by setting<distributable/>inWEB-INF/web.xml:
To configure the maximum number of active sesssions allowed for your web application, you can set max-active-sessions to your web application's WEB-INF/jboss-web.xml like the following:
<jboss-web>
<max-active-sessions>1000</max-active-sessions>
</jboss-web>
- Non-Clustered environment = Using
defaultprofile or disabling session replication:
To configure the maximum number of active sesssions allowed for your web application, you can set maxActiveSessions attribute of <Manager> to your web application's WEB-INF/context.xml in the same way as Tomcat:
<Context>
<Manager maxActiveSessions="1000" />
</Context>
If you would like to configure global setting, you can edit $JBOSS_HOME/server/$PROFILE/deploy/jboss-web.deployer/context.xml and modify from:
<Manager pathname=""/>
to:
<Manager pathname="" maxActiveSessions="1000"/>
If you do not configure any of the above setting explicitly, the default value is -1 which means no limit.
That sets the maximum number of active sessions that can exist per web application, if you have more than two WARs then they can each have that many sessions.
If you have monitoring tools that watch JMX MBeans, you can check "activeSessions" attribute on the Manager MBeans like "jboss.web:host=localhost,path=/jmx-console,type=Manager" to see how many active sessions there are at the current time. The 'path' part of the name will be different for each application.
Tomcat
To configure the maximum number of active sesssions allowed for your web application, you can set maxActiveSessions attribute of <Manager> to context.xml like the following:
<Context path="/foobar">
<Manager className="org.apache.catalina.session.StandardManager" maxActiveSessions="1000" />
</Context>
If you do not configure the above setting explicitly, maxActiveSessions is -1 by default which means no limit. Please also see Content from tomcat.apache.org is not included.Content from tomcat.apache.org is not included.http://tomcat.apache.org/tomcat-6.0-doc/config/manager.html.
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.