FeaturesPluginsDocs & SupportCommunityPartners

Developing a Java Management Extensions (JMX) Manager and Connecting to a Remote JMX Agent

By and , JMX Engineering Team.

Expected duration: 30 minutes

The NetBeans JMX Wizard Module integrates JMX technology right into your workflow in the NetBeans IDE. This module allows you to quickly develop management applications, add management to existing applications, develop manager applications, and monitor the state of the Virtual Machine. This tutorial has been updated for the JMX Wizard module version 3.0, available for NetBeans IDE version 6.0 and higher.

This tutorial requires the JDK 5 or higher version of the Java Platform. The steps detailed below have been performed with JDK 6.

This tutorial shows you how to perform the following actions:

  1. Start a JMX agent in order to make it accessible from a remote JMX manager.
  2. Create a JMX manager.
  3. Run the manager.
  4. Update the manager to add your own management logic.

Tutorial Exercises

Content on this page applies to NetBeans IDE 6.0 and 6.1

Prerequisites

This tutorial assumes you have some basic knowledge of, or programming experience with, the following technologies.

You will also benefit from having some knowledge on Monitoring and Management for the Java Platform

Software Needed for the Tutorial

For this tutorial you need to have the following software installed on your computer:

Software or Resource Version Required
NetBeans IDE version 6.1 or
version 6.0
Java Developer Kit (JDK) version 6 or
version 5
JMX plugin Available from NetBeans Update Center

To install the JMX plugin, choose Tools > Plugins and download the module from the NetBeans Update Center.

IMPORTANT NOTE: If you are using NetBeans 6.1 or higher, you also need to download and install the JConsole module.

This tutorial requires the JDK 5 or higher version of the Java Platform. The steps detailed below have been performed with JDK 6.

Resources

  • Netbeans help contents (Help > Help Contents > JMX). This help is also available from the Wizards.

Exercise 1: Creating the Management Sample Anagram Project

The goal of this exercise is to create a JMX agent to which the manager application can connect. The JMX module comes with a JMX agent sample Java project. Instead of writing a JMX agent from scratch, we will simply create this sample project.

Steps to follow

The JMX module includes a sample application with JMX monitoring built into it.  

  1. Choose File > New Project.
  2. In Samples, select the JMX category.
  3. Select the Anagram Game Managed with JMX project.
    Anagram Game Managed with JMX
  4. Click Next, no need to change the already filled in default project name and location values, keep the Set as Main Project checkbox checked and click Finish

Exercise 2: Running the Agent

The goal of this exercise is to start a Java application with the JMX remote management enabled.  Remote management allows client applications (JMX managers)  to connect to the application (JMX agent) and visualize the management and monitoring information.

Note: Any running application based on JDK 1.5 is a JMX agent for which you can enable remote management.

Steps to Follow

  1. Be sure to have the JMXAnagramGame project selected and to have set it as your main project.

  2. Right-click on the JMXAnagramGame project and select Properties. In the left part of the dialog box which is displayed, select "Monitoring and Management".
    Configure Agent
  3. Uncheck the "Attach JConsole to Project" box and check the "Enable RMI Remote Access" box. You can provide a port on which the agent waits for incoming JMX requests. In this tutorial, we just keep the default port (1099). If you provided another port number, you would have to make the appropriate changes in every part of this tutorial. Also, in this tutorial, we do not specify a properties file. (For your own applications, we provide a wizard to help you create a management properties file.)
  4. Click OK.
  5. Now, run the anagram game by clicking on the "Run Main Project with Monitoring and Management" button in the toolbar. It's the button with an icon like this: Run with Monitoring and Management

    This will launch the Angram game, with the JVM listening for RMI access on local host port 1099. The Anagram Game window will pop up:

    Run Anagram
  6. You can reduce the Anagrams window, but keep it running. Your JMX agent is running, waiting for the manger to send management requests.

Exercise 3: Creating the Manager Project

In this exercise, we will create a Java Application project named JMXAnagramManager.

Steps to Follow

  1. Choose File > New Project (Ctrl-Shift-N).
  2. Choose the project type by selecting category "Java" and project "Java Application".
    Create Manager 1
  3. Click Next.
  4. Name the new project JMXAnagramManager. Set it as your main project. Deselect the Create Main Class checkbox. In the next exercise, the JMX Manager wizard will be used to generate the main runnable class.
    Create Manager 2
  5. Click Finish. The new project is added to the Projects tree. Notice that a JMX manager project is just like any other Java Application project.

Exercise 4: Creating a Runnable Manager Class

In this exercise, you will learn how to use the JMX Manager wizard to generate a runnable manager class.

Steps to Follow

  1. Be sure to have selected the JMXAnagramManager project and to have it set as your main project.
  2. Choose File > New File (Ctrl-N). Then select the JMX Manager template from the Management category.
    Create Manager 4
  3. Click Next.
  4. Type AnagramsManager as the Class Name. Enter com.toys.anagrams.manager as the package name. Leave the checkboxes as they are. The wizard will generate a main method and some MBean discovery code. It will also set this class as the project's runnable class.
    Create Manager 5
  5. Click Next to select the JMX Agent URL to which to connect.
  6. Click the Edit button to enter the JMX agent URL to which you want to connect. A dialog box is displayed to help you enter a valid JMX URL. A JMX URL is composed of a Protocol, a Host, a Port and an URL path.
    Create Manager 6
  7. In the protocol drop-down list, a single element is provided. The list is writable and you can enter your own protocol. The default protocol RMI JVM Agent is the RMI protocol used to connect to a JDK JMX agent. The Agent we previously started is of this nature. Accept the protocol default selection.
  8. The Agent is listening on localhost:1099 so keep the host and port default values as they are. The URL path is read-only.  It is automaticaly updated with the host and port values. This is the way the URL path is constructed for a RMI JVM Agent.
  9. Click OK. The JMX Agent URL textfield is updated with the full URL you entered.
    Create Manager 7
  10. The connection to the Agent is not authenticated because we did not provide any authentication configuration when the agent was launched. Accept the default selection. Some sample code to enable an authenticated connection will be generated.
  11. Click Finish. The manager class is opened in the Source Editor.

Exercise 5: Running the Manager

In this exercise, you will learn how to run the manager and discover the MBeans.

Steps to Follow

  1. In the AnagramsManager.java file, uncomment the MBean discovery code located in the main method, so that it should read:
        public static void main(String[] args) throws Exception {
    
            //Manager instantiation and connection to the remote agent
            AnagramsManager manager = AnagramsManager.getDefault();
    
            // SAMPLE MBEAN NAME DISCOVERY. Uncomment following code:
              Set resultSet =
              manager.getMBeanServerConnection().queryNames(null, null);
              for(Iterator i = resultSet.iterator(); i.hasNext();) {
              System.out.println("MBean name: " + i.next());
              }
    
            // Close connection
            manager.close();
            System.out.println("Connection closed.");
        }
          

    You may also need to add import clauses for the java.util.Set and java.util.Iterator classes.

  2. Choose Run > Run Main Project to run the manager which will connect to the remote agent, display the discovered MBean names in the Output window and then close the connection:

    The project is compiled and the manager is started. The discovered ObejctNames are displayed in the Output window. You can notice the AnagramsStats MBean name as well as the Java VM MBeans. All Java VM standard MBeans are located under the java.lang JMX domain.

    Here is what you should see in the NetBeans Output Window of the JMXAnagramManager run:

    init:
    deps-jar:
    compile:
    run:
    MBean name: java.lang:type=MemoryManager,name=CodeCacheManager
    MBean name: java.lang:type=Compilation
    MBean name: java.lang:type=MemoryPool,name=PS Perm Gen
    MBean name: com.sun.management:type=HotSpotDiagnostic
    MBean name: java.lang:type=Runtime
    MBean name: com.toy.anagrams.mbeans:type=AnagramsStats
    MBean name: java.lang:type=ClassLoading
    MBean name: java.lang:type=Threading
    MBean name: java.lang:type=MemoryPool,name=PS Survivor Space
    MBean name: java.util.logging:type=Logging
    MBean name: java.lang:type=OperatingSystem
    MBean name: java.lang:type=Memory
    MBean name: java.lang:type=MemoryPool,name=Code Cache
    MBean name: java.lang:type=GarbageCollector,name=PS Scavenge
    MBean name: java.lang:type=MemoryPool,name=PS Eden Space
    MBean name: JMImplementation:type=MBeanServerDelegate
    MBean name: java.lang:type=GarbageCollector,name=PS MarkSweep
    MBean name: java.lang:type=MemoryPool,name=PS Old Gen
    Connection closed.
    BUILD SUCCESSFUL (total time: 1 second)
    

Exercise 6: Updating the Manager Class with Management Logic

In this exercise you will learn how to make a simple JMX request to access an MBean attribute. We will update the manager class main method in order to access the NumResolvedAnagrams attribute. This attribute represents the number of anagrams solved.

Steps to Follow

  1. In the main method, before closing the connection we are going to perform the following actions:
    • Access the MBeanServerConnection.  The MBeanServerConnection class allows you to make requests to the remote server.
    • Create the Anagrams statistics MBean ObjectName. The name is copied/pasted from the output. An object name is needed when a request is sent to a remote agent in order to specify the MBean from which to get the atribute.
    • Get the attribute NumResolvedAttribute.
    • Print the attribute value to the output.
    To do so, copy and paste the four lines of code below at the end of the AnagramsManager.java main method, right before the close conection lines:
            MBeanServerConnection connection = manager.getMBeanServerConnection();
            ObjectName name = new ObjectName("com.toy.anagrams.mbeans:type=AnagramsStats");
            Integer num = (Integer) connection.getAttribute(name, "NumSolvedAnagrams");
            System.out.println("NumSolvedAnagrams : " + num);
        
    You may need to add an import clause for javax.management.ObjectName
  2. Once you have updated the source code, you can play with the Anagrams game then run the project again in order to see the updated values. In the NetBeans Output Window of the JMXAnagramManager run you should now see at the end, before the connection closed, the added line:
    NumSolvedAnagrams : 0
    

Yes, you are done! Great job!
We hope that this tutorial helped you understand how to develop manager applications in order to access information exported thanks to JMX.


See Also

For more information, see the following:

Bookmark this page

del.icio.us furl simpy slashdot technorati digg
Companion
Projects:
MySQL Database Server   Open JDK: an Open SourceJDK   GlassFish Community: an Open Source Application Server    Mobile & Embedded Community    Open Solaris   java.net - The Source for Java Technology Collaboration   Open ESB - The Open Enterprise Service Bus Powered by