As I mentioned in an earlier post, I noticed recently that the JDK utility VisualVM
is extensible, and it was my goal to create a useful extension. I
didn't intend to create a "getting started" guide, as there is plenty of
information already available, but I keep running into the "out of date
tutorial" issue. Having encapsulated some additional information into
my earlier post in the setup category, I now find myself having to clear
up issues I'm encountering while using NetBeans 7 to actually create an
extension. While some of the differences might be traceable to the new
version of NetBeans, I think some of them may be due to expected API
changes and similar issues.
I'm working through the Creating a "Hello World" Plugin tutorial. I'll basically follow that tutorial here, with changes and observations/tips as needed.
If you followed my earlier post, you already have a new project, as that step was necessary to "seed" the Platform Manager into the clean-install NetBeans 7 Tools menu. If not, just follow Geertjan's steps in his tutorial. My next step was to create the Module Installer. In NetBeans 7, the selection items are slightly different. You still choose "New", then "Other...", then select "Module Development", but the File Type is now called "Installer / Activator" rather than "Module Installer". Further, the description says this installer is frequently not needed. I don't know if that's the case here, so I created one anyway.
The tutorial says to add the "VisualVM-Core" library as a dependency for the project. As you create your "Hello World" application according to the tutorial, you'll quickly learn that you also need to add a dependency on "VisualVM-Application", also, so you may as well do so at the same time.
If you're not fond of "seeing red" in an IDE, you might want to create the Java classes in the reverse order of the tutorial. Also, source listings in the tutorial don't include imports (the downloadable full sample does, of course). If you don't feel like retrieving the full sample, here are the imports for the HelloWorldProvider class:
import com.sun.tools.visualvm.application.Application; import com.sun.tools.visualvm.core.ui.DataSourceView; import com.sun.tools.visualvm.core.ui.DataSourceViewProvider; import com.sun.tools.visualvm.core.ui.DataSourceViewsManager;
and, in the HelloWorldView class:
import javax.swing.ImageIcon; import com.sun.tools.visualvm.application.Application; import com.sun.tools.visualvm.core.ui.DataSourceView; import com.sun.tools.visualvm.core.ui.components.DataViewComponent; import javax.swing.BorderFactory; import javax.swing.JEditorPane; import javax.swing.JPanel; import org.openide.util.Utilities;
From here, right-click on the project and select "Run" and you will get an instance of VisualVM. Selecting an application to monitor will expose your new "Hello World" tab. That was actually quite easy!
Pause for Perspective
What have we just done?
With VisualVM extensions, there are two different paths you can take:
- Creating a new tool that you want to make available to VisualVM, a tool that will be available to analyze any Java application.
- Providing a plugin for a unique type of application and using VisualVM to expose tooling specific to that application type.
What we have done in this "Hello World" is an example of #1 above, provided in the form of a new tab. If you do this, you aren't likely going to be leveraging anything unique about whatever application you are currently thinking of monitoring -- this version of VisualVM will expose the new tab for any and all Java applications to which it attaches.
Which type you choose will probably depend on what project currently interests you, and your general motivations. For example, it seems every project I work on could really use an application-specific VisualVM plugin. I typically use VisualVM on every project I work on, but I have to admit I don't see a lot of other users (unfortunately, because I think it's a great tool). In a production situation, technical support personnel are usually familiar with a small set of tools specific to their environment. For example, a WLS shop has support staff who are familiar with the WLS console; switching to JBoss in such a shop is a lot more complex than simply convincing someone of the technical virtues of one platform over another. There's a training and support issue, too. Additionally, I don't know of any support staff who use VisualVM.
Much as I'd like to provide a unique plugin for the applications I'm currently involved with, it would be a big investment for something that might not get a lot of adoption (and be useless for almost any other application). So, I probably would be more motivated to build a general-purpose module, or build a module that could be highly configurable for any application.
I have an idea in mind for just such a project; I'll share it at a later date. That way, if I get distracted by the guitar or practicing Portuguese and never get around to doing it, there won't be any 404s from my blog. But if I do get around to it, I'll update this entry just as soon as I do! In the interim, I hope that everyone who sees this will be motivated to add some valuable content to VisualVM. More later.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)