Additional menu

How to Enable JMX in Tomcat to Monitor & Administer

How to Enable JMX in Tomcat to Monitor & Administer

Geek Flare Blog post is sponsored by Netsparker Web Application Security Scanner.

 JMX (Java Management Extension) is a very powerful technology, which lets you administer, monitor and configure Tomcat MBeans.

If you are a Tomcat administrator, then you should be familiar with how to enable JMX in tomcat to monitor Heap Memory, Threads, CPU Usage, Classes, and configure various MBeans.

In this article, I will talk about how to enable and connect to Tomcat with JConsole.

I assume you have Tomcat installed if not; you can refer installation guide.

  • Go to path where you have Tomcat installed
  • Go to bin folder
  • Create a file as “setenv.sh”
  • Modify file using vi editor and add following
CATALINA_OPTS="-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9000 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false"
  • Save the file with “:w
  • Change the file permission as executable
chmod 755 setenv.sh

Before you start Tomcat to verify JMX, let’s take a look at the above configuration.

  1. Dcom.sun.management.jmxremote.port=9000 – you can modify the port number if you want.
  2. Dcom.sun.management.jmxremote.ssl=false – this means connect without SSL
  3. Dcom.sun.management.jmxremote.authenticate=false – this means no authentication needed when you use JMX client to connect to Tomcat instance.

Let’s start the tomcat by startup.sh script file. Once started, you can verify either using netstat or ps –ef grep command.

To verify using netstat

netstat –anlp | grep 9000 

[[email protected] ~]# netstat -anlp |grep 9000
tcp6       0     0 :::9000                 :::*                   LISTEN     9372/java
[[email protected] ~]#

To verify using ps command

ps –ef |grep jmx

[[email protected] ~]# ps -ef|grep jmx
root       9372     1 0 21:30 pts/0   00:00:04 java -Djava.util.logging.config.file=/home/chandan/Downloads/apache-tomcat-8.0.28/conf/logging.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9000 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Djava.endorsed.dirs=/home/chandan/Downloads/apache-tomcat-8.0.28/endorsed -classpath /home/chandan/Downloads/apache-tomcat-8.0.28/bin/bootstrap.jar:/home/chandan/Downloads/apache-tomcat-8.0.28/bin/tomcat-juli.jar -Dcatalina.base=/home/chandan/Downloads/apache-tomcat-8.0.28 -Dcatalina.home=/home/chandan/Downloads/apache-tomcat-8.0.28 -Djava.io.tmpdir=/home/chandan/Downloads/apache-tomcat-8.0.28/temp org.apache.catalina.startup.Bootstrap start
root       9816   9781 0 21:46 pts/0   00:00:00 grep --color=auto jmx
[[email protected] ~]#

This concludes jmx is enabled in Tomcat and it’s time to connect using JMX client.

You may use any client, however, to show you I will use jconsole, which comes with java.

Connect Tomcat JMX using Jconsole

  • Go to the path where you have jconsole

Tip: you may use the find command to search if you are not sure.

  • Execute jconsole
./jconsole

This will popup Java Monitoring & Management Console.

  • Select “Remote Process”
  • Enter the server host and port details

jconsole-connection

  • Click on “Connect”

This will connect to Tomcat and show you very nice graphs of CPU, Memory & Thread usage. This becomes very handy during troubleshooting.

jconsole-overview

You may also perform GC from the console.

  • Click on Memory tab, and you will see “Perform GC” button

jconsole-memory

As mentioned at the beginning of the article, you may also configure MBeans for the classes.

This is often used if you have to change the logging or specific parameters at runtime.

So go ahead and play around with Jconsole and JMX. It’s a wonderful tool for troubleshooting and monitoring.

If you are interested in learning more about Tomcat, then check out this tutorial.

Reader Interactions

Chandan Kumar
About Chandan
Chandan Kumar is the founder of Geek Flare. Learn more here and connect with him on Twitter.

Comments

  1. Note that your instructions, as you even mention, do not enable ssl or auth. This can lead to a very insecure setup if you leave your tomcat running in this mode and accessible to the internets.

Leave a Reply

Your email address will not be published. Required fields are marked *

30 Shares
Share
Tweet
Stumble
Share