Getting Active Session counts with JMX (Java Management Extensions) API

36,736

Solution 1

You can accomplish this by using something like JConsole or JVisualVM once you configure your app server to expose a JMX port. You don't mention which app server you're using but for Tomcat, it's described here: http://tomcat.apache.org/tomcat-5.5-doc/monitoring.html. Once you connect with JConsole, Tomcat exposes an MBean which has session information but again it depends which container you use.

Solution 2

ObjectName name = new ObjectName("Catalina:type=Manager,path=/NAME_OF_APP,host=localhost"); 
ManagementFactory.getPlatformMBeanServer().getAttribute(name, "activeSessions");

Solution 3

To track the sessions you could use an HttpSessionListener . If you want to expose the active sessions via JMX, you could register a mbean and call it from other applications(see JMX documentation).

Solution 4

JBoss already exposes the active session count via JMX, but only across the whole server, not per webapp. If you only have one webapp being used, then that should be OK for you.

Go the JMX console on port 8080, and look for the entry called host=localhost,path=/,type=Manager. Inside that you'll find a entry for active session count.

Share:
36,736
codingbear
Author by

codingbear

I design, I code, I test

Updated on December 18, 2020

Comments

  • codingbear
    codingbear over 3 years

    I'm trying to use JMX API to get active session counts for a web application.

    1. Is it possible to use JMX API to get this kind of information?
    2. If yes, how reliable would it be?
    3. Any example code on how to get this done?

    I've been reading JMX tutorial and documentation, but they are giving me the overview of what the technology is. I just can't pinpoint to what I need, yet.

  • skaffman
    skaffman almost 15 years
    The question is tagged "Jboss", so it is Tomcat, but the jboss flavour of it. It probably won't have the standard Tomcat JMX layer.
  • knalli
    knalli over 10 years
    Hm, on Tomcat 7 that does not work. But Catalina:type=Manager,context=/NAME_OF_APP,host=localhost
  • Shelly
    Shelly over 5 years
    what should be the value for attribute for /NAME_OF_APP