Using Visual VM with JasperReports Server to view call traces

Introduction

Viewing call traces is an important debugging activity, which allows the developer to see exactly what methods the applications is calling, in what order, and how much time it takes to execute. This is useful for looking analyzing bottlenecks, and for verifying that classes that you expected to run actually did.

While there are many tools out in the market that do this, Visual VM stands out because it's part of the JDK itself; no extra expense, and no extra downloads are necessary.

Enable JMX in the Tomcat startup script

To be able to run Visual VM, you'll need to modify the Tomcat startup scripts, and activate JMX. To activate JMX, open up the setenv.bat or setenv.sh file in the [TOMCAT_HOME]/bin directory and add the lines below. I did this on a Windows machine; you'll have to substitute REM for # in a Unix-based system.

REM For enabling JMX (for using with Visual VM)
set CATALINA_OPTS=-Dcom.sun.management.jmxremote
set CATALINA_OPTS=%CATALINA_OPTS% -Dcom.sun.management.jmxremote.port=8999
set CATALINA_OPTS=%CATALINA_OPTS% -Dcom.sun.management.jmxremote.ssl=false
set CATALINA_OPTS=%CATALINA_OPTS% -Dcom.sun.management.jmxremote.authenticate=false -Djava.rmi.server.hostname=localhost

The "8999" could be any open port you want; that value is just an example. When you are done testing with Visual VM, I suggest you add a "REM" (the Windows command for commenting out a line) at the start of each line. That way you can come back later, remove the REM's, and test again.

Use the Visual VM tool in the JDK

Browse to where you've installed the JDK, and in the bin directory, you should the VisualVM executable. It's a graphical desktop application. On Windows environments, it appears at "jvisualvm.exe"; in Unix-based systems, it appears simply as "jvisualvm." Go ahead and double-click on it.

Make sure to add the "VisualVM-Sampler" plugin

This plugin will record the application call trace, so you've got to install it if it's not already there. To check the installed plug-ins, choose "Tools -> Plugins" from the menu, and a new pop-up will appear. Check the VisualVM-Sample choice, and click "Install." This will take through the installation process, eventually asking you to re-start the application. Once you restart, you'll see that the "VisualVM-Sampler" plugin now appears under the "Installed" tab.

Running Visual VM

Now that you have the plugin installed, it's time to connect Visual VM with Tomcat. At this point, you should have already restarted Tomcat with the updated setenv file.

Choose "File -> Add JMX Connection", and fill in Connection details. If you're running this test locally, you'll put something like "localhost:8999", where the "8999" is the port you defined in the setenv script earlier.

After you click "OK", your connection will appear in the "Applications" bar on the left, like below:

Double-click on the entry, and you'll get a new set of options:

Click on the "Sampler" tab, if you haven't done so already, and now you're ready to record the application call traces.

Click on the CPU button, and you should now see call activity, like below:

At this point, go ahead and do the application activity you were interested in, whether it's executing reports, running ad hoc, or some area of the application. This will generate the call trace you were interested in testing, which will result in something like what you see below:

When you're done, hit the "Stop" button. Then click the "Snapshot" button, which will create a new tab, like what you see below:

This will give you a complete call trace, starting at the individual thread level, which you investigate by drilling down into. Or, if you want to check a particular class, you can search for it by doing a Ctrl-F and filling in the name of the class. The result will be the location in the call tree where that class gets executed.

Feedback