Getting a list of active sessions in Tomcat using Java

后端 未结 9 1441
醉梦人生
醉梦人生 2020-12-08 20:34

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

相关标签:
9条回答
  • 2020-12-08 21:27

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

    0 讨论(0)
  • 2020-12-08 21:28

    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

    0 讨论(0)
  • 2020-12-08 21:33

    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;
      }
    }
    
    0 讨论(0)
提交回复
热议问题