How to connect a Java profiler like VisualVm or JConsole to a remote tomcat running on Amazon EC2

I have been having some issues related to heap space in a tomcat that I have been running on an Amazon EC2 instance.
In order to analyze the problem I wanted to connect a Java profiler to a remote tomcat.

This is my environment:

Application server: apache-tomcat-8.0.24
Instance type: EC2 t2.micro with ubuntu 14.04
java profiler: VisualVm and JConsole

After reading different tutorials and trying different approaches which didn’t work, here is what it worked for me:

1. Set the environment variables for tomcat

If you don’t have a set environment (setenv.sh) file, create one. In your tomcat folder/bin: create a sentenv.sh file and add execution rights.
eg:
[Shell]
cd /var/local/apache-tomcat-8.0.24/bin
touch setenv.sh
chmod +x setenv.sh
[/Shell]

2. Choose a free port for JMX and set the JMX configuration

In the setenv.sh file that you have created in the previous step you will need to add the followind configuration to CATALINA_OPTS:

-Dcom.sun.management.jmxremote
-Dcom.sun.management.jmxremote.port= RANDOM_JMX_PORT
-Dcom.sun.management.jmxremote.ssl=false
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.local.only=false
-Djava.rmi.server.hostname=localhost

In my configuration I have chosen randomly the 10005 as my jmx port, so my setenv.sh file looks like:
[Shell]
CATALINA_OPTS=”$CATALINA_OPTS -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=10005 -Dcom.sun.management.jmxremote.ssl=false -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.local.only=false -Djava.rmi.server.hostname=localhost”
[/Shell]
*Note: Use CATALINA_OPTS instead of JAVA_OPTS. If you use JAVA_OPTS you will get a java.net.BindException: Address already in use.
This is because with this options JAVA_OPTS will try to start a jmx server when you start and when you shutdown tomcat. Nevertheless, if you use CATALINA_OPTS will only run when you start tomcat.

3. Restart tomcat

4. Find the Java server RMI port

When you start tomcat in the server, Java will open a TCP port for RMI calls.
This port by default is 1099 but every time you start a new Java process it will open randomly a different port.

This port is important because it will allow us to connect to the Java Virtual Machine in the server, and later on we will need to redirect it via SSH tunnel.

To obtain the port run the following command:
[Shell]
sudo netstat -lp | grep or sudo netstat -lp | grep java
[/Shell]
rmi

As you can see the jmx port is 10005 and the RMI server port is 45995.

5. Create a SSH-tunnel to the JMX and RMI ports

By creating a ssh tunnel we won’t need to add any security group to the ec2 instance and we will be able to skip the firewall.
In your local machine create a ssh tunnel to the RMI port and to your JMX port using the following command:
[Shell]
ssh -N -v -L 45995:127.0.0.1:45995 -L 10005:127.0.0.1:10005 ubuntu@ec2xxxxx.compute.amazonaws.com -i
[/Shell]
this will work for Linux and Mac users but if you are running under Windows you will need to use putty.

6. Launch the profiler

In Visualvm add a jmx connection using the following uri:
service:jmx:rmi:///jndi/rmi://localhost:10005/jmxrmi

visualvm

or if you would like to use JConsole add the same uri in the remote field:
jconsole

After all, you will be able to run a profiler an analyze your remote application.

You may also like...

6 Responses

  1. Linnea Spargo says:

    Hi,

    I came across ignaciosuay.com and wanted to run something by you.

    My team recently finished a 10-step WordPress Site Optimizer & Blueprint. It’s the exact technical checklist we use to harden security and “trim the fat” from a site to ensure it hits peak performance, regardless of how long it’s been online.

    We are currently selling this blueprint on other platforms for $27. However, I wanted to make it accessible to fellow WordPress users on a “Pay What You Want” basis here.

    You can still pay the full $27 if you want to support our work, but if things are tight or you just want to test the system first, you can grab it for $0.

    You can check out the blueprint here: https://furtherinfo.info/wordpress

    Linnea

  2. Your point of view caught my eye and was very interesting. Thanks. I have a question for you. https://www.binance.info/register?ref=IXBIAFVY

  3. Thank you for your sharing. I am worried that I lack creative ideas. It is your article that makes me full of hope. Thank you. But, I have a question, can you help me? https://accounts.binance.info/en-NG/register?ref=YY80CKRN

  4. Thanks for sharing. I read many of your blog posts, cool, your blog is very good. https://accounts.binance.info/register-person?ref=JW3W4Y3A

  5. 注册 says:

    I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article.

  6. I don’t think the title of your article matches the content lol. Just kidding, mainly because I had some doubts after reading the article. https://www.binance.info/en-ZA/register?ref=B4EPR6J0

Leave a Reply to binance h"anvisningskod Cancel reply

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