Mimicking Java Flight Recorder Triggers Outside Java Mission Control
By Jim Connors - 28 July 2017

As highlighted in this previous article, Java Flight Recorder triggers enable you to selectively dump detailed runtime information about your Java application when user-defined conditions are met.  In order to take advantage of this powerful feature, you must create and enable trigger rules inside the Java Mission Control client.  For one or a very small number of applications, using Java Mission Control might be acceptable, however if you need to manage a large number of applications, the notion of keeping a Mission Control client open for each application instance might not be very appealing or realistic.

Unfortunately, use of Flight Recorder triggers currently only works within the Mission Control client.  So the question becomes, is it possible to mimic trigger-like behavior outside of Mission Control?  This article aims to show how you can, with a simple JMX client program and some scripting.  The following README.txt file is part of the included framework and provides a reasonable description of its individual components.  It also instructs the user as to how to run the demonstration.  The framework is bundled as a NetNeans project and can be downloaded here.

To cut to the chase, here's a simple example of how the framework might be used.  For this example, we'll use a sample Java application called Latencies. This program has a deliberate flaw in that as the user increases the number of threads, the latency dramatically increases too.  What we'd like to do is mimic a trigger which will result in a dump of the Java Flight Recorder information when our thread count crosses a certain threshold.  Here are the steps:

1. Unzip the jmxclient.zip file. For this example, we unzip the jmxclient.zip file in the d:\tmp directory, resulting in the creation of the d:\tmp\JMXClient directory.

2. Start NetBeans and open up the JMXClient project that was just unzipped in the d:\tmp\JMXClient directory

3. Within NetBeans "Clean and Build" the JMXClient project.



4. The previous step creates a D:\tmp\JMXClient\dist directory.  Change to the dist/ directory and run the Latencies.bat script from a CMD shell:



Let's take a look at the command-line options associated with running the Latencies program:

5. With the Latencies program running, open up another CMD shell and start the JMXClientThreadCount.bat script.



Again, let's examine the command-line arguments to this program:

6. With both programs running, return back to the Latencies window and hit <Enter> 4 or 5 times to add additional threads to the program.



7. Now take a look at the JMXClientThreadCount.bat window.  It should have caused the thread count to exceed the specified threshold value of 20, resulting in a "trigger" that dumps the Flight Recorder contents to a file called Latencies.jfr.  With that file, you can now examine the inner workings of the Latencies program with the Java Mission Control utility.



Voila!  For further information please look at the aforementioned  JMXClient NetBeans Project and README.txt file.