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.
- Dcom.sun.management.jmxremote.port=9000 – you can modify the port number if you want.
- Dcom.sun.management.jmxremote.ssl=false – this means connect without SSL
- 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
This will popup Java Monitoring & Management Console.
- Select “Remote Process”
- Enter the server host and port details
- 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.
You may also perform GC from the console.
- Click on Memory tab, and you will see “Perform GC” button
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.
More great readings on Tomcat
How to Install Tomcat 9 and Where to Host it?Avi on February 20, 2021
How to Implement HTTP2 in Tomcat?Chandan Kumar on January 20, 2020
How to Fix Tomcat Stuck at Startup Issue?Chandan Kumar on January 20, 2020
How to Implement SSL in Apache Tomcat?Chandan Kumar on June 9, 2022
How to Enable Secure HTTP Header in Apache Tomcat 8?Chandan Kumar on January 31, 2022
How to Fix JBoss AS Stuck at Starting in Linux?Chandan Kumar on June 10, 2022
Join Geekflare Newsletter
Every week we share trending articles and tools in our newsletter. More than 10,000 people enjoy reading, and you will love it too.