How can I find out the number of open sessions programmatically in Tomcat using Java?

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

+4
source share
3 answers

You can find this information using JMX. See here on how to enable JMX and what variables to request.

Using the 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, for example. JConsole

+4
source

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

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

+2
source

You can find this with the built-in Manager, as well as at http: // server: 8080 / manager / status

If you do not have administrator login, edit conf/tomcat-users.xml and add the user with role="admin" . See the Tomcat documentation for more information here .

-one
source

All Articles