How to get a list of Tomcat session IDs using JMX and jconsole

Sometimes you want to check if a session is alive or already dead. There’s several ways how you can do it, but I want to tell about the one I found which looks rather simple – it’s by exposing catalina properties and operations via MBeans. It will still need some configuration, though.

First of all, you need to configure your tomcat instance for remote JMX access. If you have a script or a file setting environment variables for your deployment, you can add it to that script. Or, you can do it just by hacking the catalina.sh or startup.sh like this (see below).


export JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9090 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"

This will allow us to connect to servername:9090 with jconsole when the application is up. After that, you navigate to Catalina/Manager path on the MBeans tab, find your app root, expand it and locate the list of operations:

Screen Shot 2016-08-05 at 18.13.33

There’s a listSessionIds() method right there, in a list of operations. If you click this operation you will get all the active session IDs in one string:
Screen Shot 2016-08-05 at 18.27.14

It might not be very pretty, but you can copy-paste it to your fav editor and proceed as needed. This is it!

Advertisements

About Maryna Cherniavska

I have productively spent 10+ years in IT industry, designing, developing, building and deploying desktop and web applications, designing database structures and otherwise proving that females have a place among software developers. And this is a good place.
This entry was posted in java, Programming and tagged , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s