Spring Rich Clients with JMX and Java VisualVM
Creating a VisualVM Plugin for the Simple Spring Demo
In this section, we'll create a plugin that modifies VisualVM in such a way that it will recognize our Spring demo application.
The way in which VisualVM recognizes an application is via its main class.
When VisualVM is running and an application starts up with the main class specified by our plugin, VisualVM will provide an icon and a display name specifically for our application, together with subnodes for each of the attributes we've made available via our Spring-driven JMX bean.
We'll start all of this via a template that is part of the VisualVM Sample Collection 1.0. So go to the sample collection's page, download it, and install it into NetBeans IDE 6.1.
- Open the New Project wizard in the IDE and choose the "Explorer Subnodes" sample, which we'll use as our template:
- Once you've completed the wizard, you'll have a project with error messages because you haven't set the plugin's platform. The platform provides all the modules that provide the APIs used by the plugin. Now that VisualVM is part of the JDK, it's simple to get the necessary platform:
- Now that you've registered your platform, set VisualVM as the platform for the plugin you're working on:
- Now we're going to perform some small tweaks that will customize the template to our needs. After all, instead of the Anagram Game application, for which it was made, it should now be tailored to work with the Spring demo application. Optionally, you could refactor all the class names so that "Anagram" isn't referred to anymore. But let's do the essentials only:
- Open org.visualvm.demoapplicationtype.applicationtype.AnagramApplicationTypeFactory. In this class the "createApplicationTypeFor" method specifies which application will be handled by our plugin. That, in turn, is determined by the main class of the application, which in our case is "app.SimpleApp" (assuming you're working with the Spring demo that comes with the Spring RCP Tooling plugin from the NetBeans Plugin Portal). So change that method and ensure that the application's main class is referred to in line 3 below:
@Override
public ApplicationType createApplicationTypeFor(Application app, Jvm jvm, String mainClass) {
if ("app.SimpleApp".equals(mainClass)) {
return new AnagramApplicationType(app.getPid());
}
return null;
} - Open org.visualvm.demoapplicationtype.application.AnagramApplicationProvider. Here is where JMX-related information is set. In "DiscoveryTask.onSchedule", make sure the same object name as the object name you defined earlier in the "Contact" domain object is used, for connecting to the JMX server:
ObjectName obj = new ObjectName("app.SimpleApp:type=SpringDemo"); - Open org.visualvm.demoapplicationtype.applicationtype.AnagramApplicationType. In the "getName()" method, change the display name to "Spring Simple Demo" (or whatever you'd like to have displayed in the explorer view). That's also where you can change the icon, using the "getIcon" method.
- Open org.visualvm.demoapplicationtype.applicationtype.AnagramApplicationTypeFactory. In this class the "createApplicationTypeFor" method specifies which application will be handled by our plugin. That, in turn, is determined by the main class of the application, which in our case is "app.SimpleApp" (assuming you're working with the Spring demo that comes with the Spring RCP Tooling plugin from the NetBeans Plugin Portal). So change that method and ensure that the application's main class is referred to in line 3 below:
- Install the plugin and run the Spring demo application. The application should now be recognized as being a distinct application, because the display name and icon should change to the values you specified above. If you install the MBeans plugin into VisualVM, you'll be able to view the JMX info. Whether you do so or not, you're able to expand the "Attributes" node in the explorer view and thereby see the attributes exposed by the application via JMX:
That's it. Without having needed to understand much about the source code, we've been able to adapt it in such a way that we now have a plugin that provides unique functionality for the Spring demo application.
Further reading:
- Chapter 20: JMX
- 5 Minute Guide to Spring and JMX
- Enterprise Management with Spring and JMX (PDF)
- Getting Started Extending VisualVM
- VisualVM's JMX API entrypoints available in VisualVM-Tools module
| Attachment | Size |
|---|---|
| figure-1.png | 75.51 KB |
| figure-3.png | 79.4 KB |
| figure-4.png | 51.23 KB |
| figure-5.png | 64.41 KB |
| figure-6.png | 98.37 KB |
| figure-2.png | 65.65 KB |
| figure-7.png | 105.27 KB |
| figure-8.png | 68.62 KB |
| figure-9.png | 87.33 KB |
- Login or register to post comments
- 7234 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
Andrew McVeigh replied on Mon, 2008/07/21 - 4:13am
these articles have been very, very interesting for me. it's the first time i've seen the spring rcp described in a clear manner.
i think the overhead of describing the widget structures via XML seems high. however the flexibility is impressive. it's a shame they are re-jigging the whole framework just as it seems to have reached a level of maturity. i lost interest in it about 2 years ago when no activity seemed to be occurring.
thanks for the series geertjan.
cheers,
Andrew
Geertjan Wielenga replied on Mon, 2008/07/21 - 2:48pm
vbplayr2000 replied on Wed, 2008/09/17 - 5:48pm
Hi,
I'm new to Java developent and am drinking from the firehose to get up to speed. I discoverd the VisualVM tool and wished to use it for performance monitoring of our applications. However, one key feature that I do not see is the ability to capture the data and export it to .csv, html, or xml. It appears I can only save the data in the proprietary file type.
Do you have any suggestions for how to customize the API's to allow export of data?
Thanks for any help you might offer.
Regards,
Jeremiah