simon
simon

Reputation: 12902

How can I find out the amount of open sessions programatically in Tomcat using Java?

Is it possible to find out the number of open sessions reliably in Tomcat (i.e. not only the amount of users who have logged in since [current time]-[session time out], but the number of sessions stored on the server)?

Upvotes: 4

Views: 4052

Answers (3)

laura
laura

Reputation: 7332

If you need this info in your application, you can trace when sessions are created or destroyed by implementing HttpSessionListener and adding it to your server context.

http://java.sun.com/javaee/5/docs/api/javax/servlet/http/HttpSessionListener.html

Upvotes: 2

matt b
matt b

Reputation: 139921

You can find this with the builtin Manager as well at http://server:8080/manager/status

If you don't have an admin login enabled, edit conf/tomcat-users.xml and add a user with role="admin". More info in the Tomcat documentation here.

Upvotes: -1

Brian Agnew
Brian Agnew

Reputation: 272237

You can find this info using JMX. See here for how to enable JMX and what variables to query.

Using an Ant JMX task you can use:

   <!-- get all sessions and split result as delimiter <em>SPACE</em> for easy
             access all session ids directly with ant property sessions.[0..n].
        -->
        <jmx:invoke
            name="Catalina:type=Manager,path=/ClusterTest,host=localhost" 
            operation="listSessionIds"
            resultproperty="sessions"
            echo="false"
            delimiter=" "
        />

but you can use other tools e.g. JConsole.

Upvotes: 4

Related Questions