I am developing a project in Java in which I want the count of all active sessions in Tomcat. Based on that I want to see how much of those users are active and actually usi
A simple tutorial to demonstrate how to determine active users / sessions in a Java Web Application.
package com.hubberspot.javaee.listener;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
@WebListener
public class OnlineUsersCounter implements HttpSessionListener {
private static int numberOfUsersOnline;
public OnlineUsersCounter() {
numberOfUsersOnline = 0;
}
public static int getNumberOfUsersOnline() {
return numberOfUsersOnline;
}
public void sessionCreated(HttpSessionEvent event) {
System.out.println("Session created by Id : " + event.getSession().getId());
synchronized (this) {
numberOfUsersOnline++;
}
}
public void sessionDestroyed(HttpSessionEvent event) {
System.out.println("Session destroyed by Id : " + event.getSession().getId());
synchronized (this) {
numberOfUsersOnline--;
}
}
}
Running the below servlet on three different browsers will provide output as : (see fig below)
package com.hubberspot.javaee;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.hubberspot.javaee.listener.OnlineUsersCounter;
// @WebServlet annotation has a initParams field which takes
// in initialization parameters for a servlet.
// @WebInitParam annotation takes in a name and value for the
// initialization parameters for the current Servlet.
@WebServlet(name = "HelloWorldServlet" , urlPatterns = { "/HelloWorldServlet" }
, initParams = { @WebInitParam(name = "user" , value = "Jonty") })
public class HelloWorldServlet extends HttpServlet {
protected void doGet(
HttpServletRequest request,
HttpServletResponse response
) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
// sessionCreated method gets executed
HttpSession session = request.getSession();
session.setMaxInactiveInterval(60);
try {
out.println("<html>");
out.println("<body>");
out.println("<h2>Number of Users Online : "
+ OnlineUsersCounter.getNumberOfUsersOnline()
+ "</h2>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
}
Output of the program :
For more: http://www.hubberspot.com/2013/09/how-to-determine-active-users-sessions.html
If you dont need the values in the actual web application, a groovy script can help:
import javax.management.remote.*
import javax.management.*
import groovy.jmx.builder.*
// Setup JMX connection.
def connection = new JmxBuilder().client(port: 4934, host: '192.168.10.6')
connection.connect()
// Get the MBeanServer.
def mbeans = connection.MBeanServerConnection
def activeBean = new GroovyMBean(mbeans, 'Catalina:type=Manager,host=localhost,context=/')
println "Active sessions: " + activeBean['activeSessions']
If you want the actual sessions, you have methods to retrieve them, like:
def sessions = activeBean.listSessionIds().tokenize(' ');
Here is how to get the session count locally, if you're getting the stats within an application running on the tomcat server you want the stats for. No need to enable jmx remote this way:
public void init(final ServletConfig config) throws ServletException
{
context = config.getServletContext().getContextPath();
}
//...
private void getSessionStats()
{
MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
ObjectName objectName = new ObjectName("Catalina:type=Manager,context="+context+",host=localhost");
Object activeSessions = mBeanServer.getAttribute(objectName, "activeSessions");
System.out.println(activeSessions);
}
Two more approaches to add, both of which I've been using all the time.
1. VisualVM
To find out the number of active sessions, you can use Tomcat's internal statistics that can be accessed using JMX (Java Management Extension).
Practically, a profiling tool such as VisualVM or Java VisualVM can be used to access the JMX statistics, such as the number of active sessions, on the MBeans tab (See below the screenshot)
2. JavaMelody
You can also use a JavaEE applications monitoring tool, such as JavaMelody, which helps you monitor Java or Java EE applications in QA and production environments.
You can attach a jolokia jvm agent to the running tomcat and query the activeSessions
attribute from the relevant MBeans via curl.
java -jar agent.jar start [TOMCAT-PID]
curl 'http://127.0.0.1:8778/jolokia/read/Catalina:context=*,host=*,type=Manager/activeSessions'
java -jar agent.jar stop [TOMCAT-PID]
This will give you something like
{
"request":{
"mbean":"Catalina:context=*,host=*,type=Manager",
"attribute":"activeSessions",
"type":"read"
},
"value":{
"Catalina:context=\/SampleApp,host=localhost,type=Manager":{
"activeSessions":1
}
},
"timestamp":1553105659,
"status":200
}
You should use JMX (Java Managemnet eXtension) and query the following
jmxObjectName: Catalina:host=localhost,path=/,type=Manager
jmxAttributeName: activeSessions
You can use jconsole to access this data. To get jmx running see http://tomcat.apache.org/tomcat-6.0-doc/monitoring.html
You have lot of advantages using JMX as you get lots of other data, too. You can put it in a munin plugin and let munin monitor it and draw nice graphs to look at.