Skip to content

Instantly share code, notes, and snippets.

@tkareine
Last active January 28, 2019 15:10
Show Gist options
  • Save tkareine/df603217b6756825cf9c99282b8438b1 to your computer and use it in GitHub Desktop.
Save tkareine/df603217b6756825cf9c99282b8438b1 to your computer and use it in GitHub Desktop.

Connecting Visual VM to Tomcat 7

Adapted from Mark Shead's blog.

  1. Login to remote host with SSH:

    local $ ssh remote`
  2. Find out exact Tomcat version, e.g.

    remote $ yum info tomcat7.noarch
    # ...
    Installed Packages
    Name        : tomcat7
    Arch        : noarch
    Version     : 7.0.67
  3. Fetch JxmRemoteLifecycleListener implementation for that specific Tomcat version:

    remote $ curl http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.67/bin/extras/catalina-jmx-remote.jar -o ~/catalina-jmx-remote.jar
    remote $ mkdir -p /tomcat-install-path/lib
    remote $ sudo mv ~/catalina-jmx-remote.jar /tomcat-install-path/lib/
  4. Add listener for JmxRemoteLifecycleListener to Tomcat config:

remote $ sudo cp /tomcat-install-path/conf/server.xml{,.org}
remote $ sudo $EDITOR /tomcat-install-path/config/server.xml

Add the following line inside <Server> element:

<Listener className="org.apache.catalina.mbeans.JmxRemoteLifecycleListener" rmiRegistryPortPlatform="9090" rmiServerPortPlatform="9091" />
  1. Add JMX options to $JAVA_HOME:
remote $ sudo cp /etc/init.d/tomcat-service{,.org}`
remote $ sudo $EDITOR /etc/init.d/tomcat-service`

Add the following line:

JAVA_OPTS="$JAVA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=localhost"
  1. Restart Tomcat:
remote $ sudo service tomcat-service restart
  1. Check that JVM listens to ports 9090 and 9091:
remote $ sudo lsof -iTCP -sTCP:LISTEN -P | grep java
# ...
java      2522 tomcat   52u  IPv6 46072171      0t0  TCP *:9090 (LISTEN)
java      2522 tomcat   53u  IPv6 46072172      0t0  TCP *:9091 (LISTEN)
  1. In local host, setup SSH port forwarding to remote host:
local $ ssh -L 9090:localhost:9090 -L 9091:localhost:9091 remote
  1. Test connection from local to remote:
local $ nc -v -w 10 localhost 9090
# ...
Connection to localhost port 9090 [tcp/websm] succeeded!
  1. Open Visual VM, open remote connection to localhost:9090
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment