Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of active sessions in Tomcat using Java

Tags:

java

tomcat

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 using the application.

like image 940
rampatel Avatar asked Nov 01 '10 13:11

rampatel


People also ask

How can I see active connections in Tomcat?

I use Apache Geronimo and it displays in the web console the number of current busy threads (not sessions) so it is programmable. For your solution, you should have netstat -np tcp | find "12.34. 56.78:80" | find "ESTABLISHED" so you don't include non-established states like FIN_WAIT, SYN_SENT, etc.

How do I know how many active user sessions?

Press Windows + R button. Type perfmon and hit the Enter button. Now, you have to add the relevant counters for seeing the number of active user sessions. To do it, click on the green “+” button on the toolbar.

Where are Tomcat sessions stored?

Tomcat's sessions are stored according to chosen session manager. If we pick the standard manager (StandardManager class saw previously), all session data will be saved into Java heap.

What are Tomcat sessions?

A Session is the Catalina-internal facade for an HttpSession that is used to maintain state information between requests for a particular user of a web application.


7 Answers

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.

like image 75
Janning Avatar answered Oct 26 '22 08:10

Janning


There isn't any way to get the session count directly from tomcat. But you can create and register a session listener and up the count when its created. Here is an example:

http://tomcat-configure.blogspot.com/2009/01/tomcat-session-listener-example.html

public class SessionCounter implements HttpSessionListener {

  private static int activeSessions = 0;

  public void sessionCreated(HttpSessionEvent se) {
    activeSessions++;
  }

  public void sessionDestroyed(HttpSessionEvent se) {
    if(activeSessions > 0)
      activeSessions--;
    }

  public static int getActiveSessions() {
     return activeSessions;
  }
}
like image 45
Amir Raminfar Avatar answered Oct 26 '22 07:10

Amir Raminfar


Here is the Java 7 style JMX code snippet (what basZero asked for and exactly does the job what Janning described):

JMXServiceURL url = new JMXServiceURL("service:jmx:rmi:///jndi/rmi://localhost:9999/jmxrmi");
try(JMXConnector jmxc = JMXConnectorFactory.connect(url)) {
  MBeanServerConnection mbsc = jmxc.getMBeanServerConnection();
  ObjectName mbeanName = new ObjectName("Catalina:type=Manager,context=/,host=localhost");
  Object value = mbsc.getAttribute(mbeanName, "activeSessions");
}

Of course you need to replace root context (/) in ObjectName with your app context string if it is not deployed in the root context. See my detailed explanation on the Catalina JMX issue here: Accessing built-in MBeans in Tomcat programatically

like image 42
Miklos Krivan Avatar answered Oct 26 '22 09:10

Miklos Krivan


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 :

  1. Eclipse Browser ->

Eclipse

  1. Firefox Browser ->

Firefox

  1. Internet Explorer Browser ->

IE

  1. Console Output ->

Console

For more: http://www.hubberspot.com/2013/09/how-to-determine-active-users-sessions.html

like image 21
Yash P Shah Avatar answered Oct 26 '22 07:10

Yash P Shah


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);
}
like image 40
AdeelMufti Avatar answered Oct 26 '22 08:10

AdeelMufti


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(' ');
like image 43
Brimstedt Avatar answered Oct 26 '22 08:10

Brimstedt


"PSI Probe" may do the trick for you: http://code.google.com/p/psi-probe/

like image 40
FelixD Avatar answered Oct 26 '22 07:10

FelixD