Code Monkey home page Code Monkey logo

jvisualvmtomcat's Introduction

JVisualVM Tomcat EC2

The objective is to connect a Java profiler like JVisualVm or JConsole to a remote tomcat running on AWS EC2 Instance (Amazon Linux AMI).

Environment Information

  • Tomcat: tomcat 8.0.x (through yum)
  • Instance type: EC2 t2.micro
  • OS: Red Hat 4.8.3-9

Software already installed

The machine already has the following software installed from my previous tutorials:

  • Tomcat 8.x: This video explains how to install tomcat in an AWS EC2 instance.

Configuration Steps

1. Find the environment file for Tomcat

  • If you installed tomcat8 through yum command this path '/etc/tomcat8/tomcat8.conf' is correct.
whereis tomcat8
vim /etc/tomcat8/tomcat8.conf
  • If you installed the tomcat by yourself you should create the setenv.sh file if you have not set the environment file (setenv.sh) yet. In your tomcat "folder/bin" create a sentenv.sh file and add the execution rights.
cd /var/local/apache-tomcat-8.0.x/bin
touch setenv.sh
chmod +x setenv.sh

2. Set the JMX Configuration

In the tomcat8.conf or setenv.sh file you need to add the following configuration to the CATALINA_OPTS variable. In my configuration I have chosen randomly the 7091 for my JMX port.

  • For the tomcat8.conf file:
CATALINA_OPTS="-Dcom.sun.management.jmxremote.port=7091 -Dcom.sun.management.jmxremote.rmi.port=7091 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1 "
  • For the setenv.sh file:
CATALINA_OPTS="-Dcom.sun.management.jmxremote.port=7091 -Dcom.sun.management.jmxremote.rmi.port=7091 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -Djava.rmi.server.hostname=127.0.0.1 $CATALINA_OPTS"

Note: Use CATALINA_OPTS instead of JAVA_OPTS. If you use JAVA_OPTS you might get a java.net.BindException: "Address already in use". This is because with this JAVA_OPTS option will try to start a JMX server when you start and shutdown tomcat. Nevertheless, if you use CATALINA_OPTS will only run when you start tomcat.

3. Restart Tomcat

service tomcat8 restart

4. Find the RMI and JMX ports

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 JVM in the server, and later on you will need to redirect it via SSH tunnel. As you can see in the image the JMX port is 7091.

sudo netstat -lp | grep java

jmx

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

By creating a ssh tunnel you won’t need to add any security group to the ec2 instance so you will be able to skip the firewall. In your local machine create a ssh tunnel to the RMI and JMX ports using the following command:

  • Mac
ssh -N -v -L 7091:127.0.0.1:7091 -L 7091:127.0.0.1:7091 [email protected] -i <your aws key.pem>
  • Windows

Note: If you are in Windows you can set up the tunnel using Putty with the following configuration:

putty

6. Launch the Profiler

  • JVisualVM

Open a console and type the following:

jvisualvm

Add a JMX connection with the following connection information:

localhost:7091

You can change the display name to whatever name and check the "Do not require SSL connection" option.

jvisualvm

  • JConsole

Open a console and type the following:

jconsole

Add a remote process connection with the following information:

localhost:7091

jconsole

Thanks

  • How to connect a Java profiler like VisualVm or JConsole to a remote tomcat running on Amazon EC2 java profiler.
  • Profiling a jvm in ec2 with visualvm profiling jvm.

Any improvement or comment about this info is always welcome! As well as others shared their code publicly I want to share mine! Thanks!

jvisualvmtomcat's People

Contributors

carloscharz avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

jvisualvmtomcat's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.