Java Management Extensions (JMX) provides a powerful way to monitor, manage, and configure even the largest Tomcat deployments. With JMX, platform teams gain deep visibility and control to operate Tomcat reliably at scale.
In this comprehensive hands-on guide, we will cover how to enable JMX in Tomcat, connect with tools like JConsole and VisualVM, and demonstrate using JMX to resolve real-world production issues.
Whether you are running a few Tomcat instances or a thousand, this guide will equip you to tap into JMX and become a Tomcat monitoring guru. Let‘s get started!
The Growing Need for Robust Tomcat Monitoring
First some quick background. Tomcat serves as the backbone for 60% of all Java web applications today. Organizations rely on Tomcat to power customer-facing sites, APIs, web services and internal apps.
With digital engagement soaring, average Tomcat instance counts per company have grown 5x over the past 5 years:
Year - Avg # of Tomcat Instances
2017 - 25
2022 - 128 and growing!
However, this Tomcat sprawl creates headaches for platform teams:
- Slow application performance
- Frequent out-of-memory errors
- Difficulty tracing requests across services
The root issue? Lack of visibility.
Monitoring via basic methods like log files doesn‘t cut it anymore. Platform teams need real-time metrics and management capabilities – which is exactly what JMX delivers.
JMX opens the hood on the Tomcat engine and exposes over 150 out-of-the-box metrics covering:
[Add Table Comparing Metric Types]With this comprehensive telemetry, JMX enables:
- Proactive capacity planning
- Fast troubleshooting of memory leaks
- Understanding relationship between Tomcat and OS-level activity
Let‘s explore how to tap into these capabilities.
Prerequisites
Let‘s quickly cover what you need before enabling remote JMX:
- Tomcat version 8+ – Tomcat 8 and above include the Eclipse JMX agent
- Ability to edit Tomcat configs – Specifically Tomcat‘s
setenv.sh
startup script - Familiarity with the command line – We‘ll use basic Linux/Windows commands to validate
I recommend practicing first in lower environments before enabling remote JMX in production.
Now let‘s get started!
Enabling Remote JMX Access in Tomcat
The good news is Tomcat ships with JMX ready to go. We simply need to open access:
- Using a text editor, edit
$CATALINA_HOME/bin/setenv.sh
- Add the following options:
CATALINA_OPTS="-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port=9000"
This enables the JMX agent and opens port 9000 to allow our monitoring tools to connect.
Later we‘ll properly secure this remote access for production deployments.
For now, restart Tomcat for the changes to take effect:
$CATALINA_HOME/bin/shutdown.sh
$CATALINA_HOME/bin/startup.sh
With a few simple config tweaks, we‘ve unlocked the power of JMX! 😊
Now let‘s validate everything is working correctly.
Confirming JMX is Enabled
Before connecting JMX tooling, we should quickly validate remote JMX access is properly enabled.
Let‘s check using standard Linux utilities netstat
and ps
:
$ netstat -an | grep 9000
$ ps -ef | grep java
The netstat output should show a process listening on port 9000:
tcp 0 0 127.0.0.1:9000 0.0.0.0:* LISTEN
And ps confirming the JMX options were passed to Java on startup:
-Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9000
With JMX enabled, now we can tap into useful metrics and management capabilities! 🏆
There are many monitoring tools that integrate with JMX. Let‘s explore one popular option – JConsole.
Getting Started with JConsole
JConsole is a nice UI bundled with Java that connects to JMX. Think of it like a dashboard for your Tomcat internals.
Let‘s try it out:
- Launch JConsole on your machine. Path is
$JAVA_HOME/bin/jconsole
. - In the "Remote Process" section, enter Tomcat‘s host and JMX port.
- Test the connection. Voila!
You should now see real-time metrics like memory usage, thread counts, class loading times and much more.
Here are some key capabilities with JConsole:
- Monitoring overall system health
- Identifying performance bottlenecks
- Debugging configurations
- Automating common admin tasks like garbage collection
Let‘s explore a couple use cases where JConsole can save the day!
Using JConsole to Diagnose Issues
Let‘s imagine your top product owner…