Getting Started Extending VisualVM (Part 1)

Update on 28 May 2008: The code and steps in this blog entry have been updated to reflect the VisualVM 1.0 APIs. If you want to download the complete sources of the sample discussed here, get the VisualVM Sample Collection from the Plugin Portal.

VisualVM, i.e., the bundling of the JDK's troubleshooting tools, is perfectly positioned to become the Java developer's troubleshooter of choice—free, open source, active mailing list, with a bunch of great programmers working on it. On top of all that, it is pluggable. Do I need to explain why that makes it even more viable? Probably not. But, imagine if it wasn't—there'd be no way to extend it independently of the rest of the code. Already, that is, without the APIs being 100% figured out, there's an external plugin available, that is, the plugin was not made by one of the core VisualVM developers: Thread Dump Analyzer for VisualVM, by Ingo Rockel. In this blog entry, I will look at how you can get started extending VisualVM yourself.

In theory, you can use any editor or IDE of your choice to extend VisualVM. In reality, since VisualVM is built on the NetBeans Platform, and since NetBeans IDE is currently the only IDE that has tools (i.e., wizards, code generators, etc) for that platform, NetBeans IDE is the IDE of choice when extending VisualVM. (And that fact, to a very small extent, explains my recent interest in IntelliJ. VisualVM should be extendable in Eclipse, IntelliJ, JDeveloper, JBuilder, and any other IDE. The "only" things needed there are tools for generating a project structure, a few wizards, and some interconnections with the layer file.)

Take the following steps to get going extending VisualVM:

  1. Get the Stuff You Will Need. The binary, the sources, and the Javadoc are all open sourced and freely available to you. Since you're creating a plugin, you're by definition an adventurous type of programmer. Hence, the more info you have the better and hence you will want to look at sources and Javadoc and anything else you can get your hands on. All of that's available thanks to the open sourcing of VisualVM.

  2. Register the VisualVM Binary in NetBeans IDE. When you put the binary in the NetBeans Platform Manager, you will be able to compile your plugin against that binary. The VisualVM's APIs are then available to your plugin. Here's me registering the VisualVM binary:

    Also, make very sure that you have JDK 6 registered in the IDE, because VisualVM will not work with any earlier version of the JDK. So, the plugin that you will create must use some version of JDK 6. Also, note that some of the tabs in VisualVM will not be visible for applications that are not running on JDK 6.

  3. Open the VisualVM Sources in the NetBeans IDE Favorites Window. Put the sources in the Favorites window so that... you can explore the "plugins" folder, which you will want to do to learn how the existing three plugins ("glassfish", "jconsole", "mbeans") are created:

    By the way, you could also open them in the Projects window, since they're all in a NetBeans module, as you can see above from the fact that there's a 'nbproject' folder.

  4. Examine the VisualVM Entrypoint Document. When you download the "API Docs" from https://visualvm.dev.java.net/, you will find, apart from the Javadoc, a very handy overview of all the places where your plugin can enrich VisualVM:

    The above screenshot doesn't do the document justice. You really need to see it live, while realizing that there is still some movement in terms of the API itself and hence the document itself may also change.

  5. Say "Hello World" to VisualVM. You now have all your resources and the time has come to make your first trip into the heart of VisualVM. If you're not familiar with how plugins for the NetBeans Platform work, you're in luck, because there are many wonderful things that you can learn about, here, in the "NetBeans Modules and Rich-Client Applications Learning Trail". Assuming that you are at least familiar with the NetBeans Module Quick Start, let's say "Hello World" to VisualVM.

    Now, we'll say "Hello World". We will create a new tab that looks as follows:

    There will be no data in our tab, we'll simply create it and add some placeholders for content. Next time we'll add some interesting data.

    Let's get started! Create a new NetBeans module and make sure to specify that you want to build against the VisualVM binary that you registered in step 2 above:

    Use the "Module Installer" wizard, which will create a class that is called when the plugin is installed into VisualVM. (And, now that I know a little bit about IntelliJ, I can inform you that IntelliJ's "ApplicationComponent" class is comparable to the NetBeans "ModuleInstall" class, which is the class that the Module Installer wizard creates.) Here's where you'll find the wizard in the New File dialog:

    Once you've completed the wizard, implement the ModuleInstall.restored method and the ModuleInstall.uninstalled methods as follows:

    @Override
    public void restored() {
        HelloWorldViewProvider.initialize();
    }
    
    @Override
    public void uninstalled() {
        HelloWorldViewProvider.unregister();
    }

    Now add a dependency (which you should know how to do after reading the NetBeans Module Quick Start) on "VisualVM-Application" and "VisualVM-Core", which provide the applicable APIs into the VisualVM. Create a class called "HelloViewProvider", as follows:

    public class HelloWorldViewProvider extends DataSourceViewProvider<Application> {
    
        private static DataSourceViewProvider instance = new HelloWorldViewProvider();
    
        @Override
        public boolean supportsViewFor(Application application) {
            //Always shown:
            return true;
        }
    
        @Override
        protected DataSourceView createView(Application application) {
            return new HelloWorldView(application);
        }
    
        static void initialize() {
            DataSourceViewsManager.sharedInstance().addViewProvider(instance, Application.class);
        }
    
        static void unregister() {
            DataSourceViewsManager.sharedInstance().removeViewProvider(instance);
        }
        
    }

    By reading the Javadoc that you downloaded earlier, you'll know what the two overridden methods above are all about:

    You should be using the following import statements at this point:

    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;

    Finally, we'll create our HelloView class, which is created by our provider above:

    public class HelloWorldView extends DataSourceView {
    
        private DataViewComponent dvc;
        //Make sure there is an image at this location in your project:
        private static final String IMAGE_PATH = "org/hellovisualvm/coredump.png"; // NOI18N
    
        public HelloWorldView(Application Application) {
            super(Application,"Hello World", new ImageIcon(Utilities.loadImage(IMAGE_PATH, true)).getImage(), 60, false);
        }
    
        protected DataViewComponent createComponent() {
    
            //Data area for master view:
            JEditorPane generalDataArea = new JEditorPane();
            generalDataArea.setBorder(BorderFactory.createEmptyBorder(14, 8, 14, 8));
    
            //Panel, which we'll reuse in all four of our detail views for this sample:
            JPanel panel = new JPanel();
    
            //Master view:
            DataViewComponent.MasterView masterView = new DataViewComponent.MasterView("Hello World Overview", null, generalDataArea);
    
            //Configuration of master view:
            DataViewComponent.MasterViewConfiguration masterConfiguration =
                    new DataViewComponent.MasterViewConfiguration(false);
    
            //Add the master view and configuration view to the component:
            dvc = new DataViewComponent(masterView, masterConfiguration);
    
            //Add detail views to the component:
            dvc.addDetailsView(new DataViewComponent.DetailsView(
                    "Hello World Details 1", null, 10, panel, null), DataViewComponent.TOP_LEFT);
            dvc.addDetailsView(new DataViewComponent.DetailsView(
                    "Hello World Details 2", null, 20, panel, null), DataViewComponent.TOP_RIGHT);
            dvc.addDetailsView(new DataViewComponent.DetailsView(
                    "Hello World Details 3", null, 30, panel, null), DataViewComponent.BOTTOM_RIGHT);
            dvc.addDetailsView(new DataViewComponent.DetailsView(
                    "Hello World Details 4", null, 40, panel, null), DataViewComponent.BOTTOM_RIGHT);
    
            return dvc;
    
        }
    
    }

    Here are the import statements that you should now have:

    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.ImageIcon;
    import javax.swing.JEditorPane;
    import javax.swing.JPanel;
    import org.openide.util.Utilities;

  6. Install the Plugin. Install the plugin and you'll see this new tab in VisualVM:

Hurray, you've said "Hello World" and are now on the road to integrating your own cool tools into VisualVM! Next time we'll add some data.

Update on 28 May 2008: The code and steps in this blog entry have been updated to reflect the VisualVM 1.0 APIs. If you want to download the complete sources of the sample discussed here, get the VisualVM Sample Collection from the Plugin Portal.

Comments:

Hi,

thx for the nice HowTo :)

Just a note: the API Entrypoints-Image could be a bit bigger.

kind regards,

Ingo

Posted by Ingo Rockel on February 26, 2008 at 04:47 PM PST #

Thanks Ingo! You can now click on the image and it will enlarge, so now you can see the texts.

Posted by Geertjan on February 26, 2008 at 09:38 PM PST #

Post a Comment:
  • HTML Syntax: NOT allowed
About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« March 2015
SunMonTueWedThuFriSat
4
14
15
16
22
29
30
31
    
       
Today