Getting a list of active sessions in Tomcat using Java

后端 未结 9 1462
醉梦人生
醉梦人生 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:09

    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(' ');
    

提交回复
热议问题