This section presents an example of a straightforward, standard MBean.
A standard MBean is defined by writing a Java interface called
SomethingMBeanand a Java class calledSomethingthat implements that interface. Every method in the interface defines either an attribute or an operation in the MBean. By default, every method defines an operation. Attributes and operations are methods that follow certain design patterns. A standard MBean is composed of an MBean interface and a class. The MBean interface lists the methods for all exposed attributes and operations. The class implements this interface and provides the functionality of the instrumented resource.The following sections examine an example of a standard MBean and a simple JMX technology-enabled agent (JMX agent) that manages the MBean.
MBean Interface
An example of a basic MBean interface,
HelloMBean, follows:package com.example; public interface HelloMBean { public void sayHello(); public int add(int x, int y); public String getName(); public int getCacheSize(); public void setCacheSize(int size); }By convention, an MBean interface takes the name of the Java class that implements it, with the suffix
MBeanadded. In this case, the interface is calledHelloMBean. TheHelloclass that implements this interface is described in the next section.According to the JMX specification, an MBean interface consists of named and typed attributes that are readable and possibly writable, in addition to the named and typed operations that can be invoked by the applications that are managed by the MBean. The
HelloMBeaninterface declares two operations: the Java methodsadd()andsayHello().
HelloMBeandeclares two attributes:Nameis a read-only string, andCacheSizeis an integer that can be both read and written. Getter and setter methods are declared to allow the managed application to access and possibly change the attribute values. As defined by the JMX specification, a getter is any public method that does not return void and whose name begins withget. A getter enables a manager to read the value of the attribute, whose type is that of the returned object. A setter is any public method that takes a single parameter and whose name begins withset. A setter enables a manager to write a new value in the attribute, whose type is the same as that of the parameter.The implementation of these operations and attributes is shown in the following section.
MBean Implementation
The
HelloJava class that follows implements theHelloMBeanMBean interface:package com.example; public class Hello [...] implements HelloMBean { public void sayHello() { System.out.println("hello, world"); } public int add(int x, int y) { return x + y; } public String getName() { return this.name; } public int getCacheSize() { return this.cacheSize; } public synchronized void setCacheSize(int size) { [...] this.cacheSize = size; System.out.println("Cache size now " + this.cacheSize); } [...] private final String name = "Reginald"; private int cacheSize = DEFAULT_CACHE_SIZE; private static final int DEFAULT_CACHE_SIZE = 200; }The straightforward
Helloclass provides the definitions of the operations and attributes that are declared byHelloMBean. ThesayHello()andadd()operations are extremely simple, but real-life operations can be as simple or as sophisticated as needed.The methods to get the
Nameattribute and to get and set theCacheSizeattribute are also defined. In this example, theNameattribute value never changes. However, in a real scenario this attribute might change as the managed resource runs. For example, the attribute might represent statistics such as uptime or memory usage. Here, the attribute is merely the nameReginald.Calling the
setCacheSizemethod enables you to alter theCacheSizeattribute from its declared default value of 200. In a real scenario, changing theCacheSizeattribute could require other operations to be performed, such as discarding entries or allocating new entries. This example merely prints a message to confirm that the cache size has changed. However, more sophisticated operations could be defined instead of the simple call toprintln().With the
HelloMBean and its interface thus defined, they can now be used to manage the resource they represent, as shown in the following section.Creating a JMX Agent to Manage a Resource
Once a resource has been instrumented by MBeans, the management of that resource is performed by a JMX agent.
The core component of a JMX agent is the MBean server. An MBean server is a managed object server in which MBeans are registered. A JMX agent also includes a set of services to manage MBeans. See the API documentation for the
MBeanServerinterface for details of the MBean server implementation.The
Mainclass that follows represents a basic JMX agent:package com.example; import java.lang.management.*; import javax.management.*; public class Main { public static void main(String[] args) throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("com.example:type=Hello"); Hello mbean = new Hello(); mbs.registerMBean(mbean, name); [...] System.out.println("Waiting forever..."); Thread.sleep(Long.MAX_VALUE); } }The JMX agent
Mainbegins by obtaining an MBean server that has been created and initialized by the platform, by calling thegetPlatformMBeanServer()method of thejava.lang.management.ManagementFactoryclass. If no MBean server has been created by the platform already, thengetPlatformMBeanServer()creates an MBean server automatically by calling the JMX methodMBeanServerFactory.createMBeanServer(). TheMBeanServerinstance obtained byMainis namedmbs.Next,
Maindefines an object name for the MBean instance that it will create. Every JMX MBean must have an object name. The object name is an instance of the JMX classObjectNameand must conform to the syntax defined by the JMX specification. Namely, the object name must contain a domain and a list of key-properties. In the object name defined byMain, the domain iscom.example(the package in which the example MBean is contained). In addition, the key-property declares that this object is of the typeHello.An instance of a
Helloobject, namedmbean, is created. TheHelloobject namedmbeanis then registered as an MBean in the MBean servermbswith the object namename, by passing the object and the object name into a call to the JMX methodMBeanServer.registerMBean().With the
HelloMBean registered in the MBean server,Mainsimply waits for management operations to be performed onHello. In this example, these management operations are invokingsayHello()andadd(), and getting and setting the attribute values.Running the Standard MBean Example
Having examined the example classes, you can now run the example. In this example, JConsole is used to interact with the MBean.
To run the example, follow these steps:
- Save the bundle of JMX API sample classes,
jmx_examples.zip, to your working directory,work_dir.
- Unzip the bundle of sample classes by using the following command in a terminal window.
unzip jmx_examples.zip- Compile the example Java classes from within the
work_dirdirectory.
javac com/example/*.java- Start the
Mainapplication.
java com.example.MainA confirmation that
Mainis waiting for something to happen is displayed.- Start JConsole in a different terminal window on the same machine.
jconsoleThe New Connection dialog box is displayed, presenting a list of running JMX agents that you can connect to.
- In the New Connection dialog box, select
com.example.Mainfrom the list and click Connect.A summary of your platform's current activity is displayed.
- Click the MBeans tab.
This panel shows all the MBeans that are currently registered in the MBean server.
- In the left frame, expand the
com.examplenode in the MBean tree.You see the example MBean
Hellothat was created and registered byMain. If you clickHello, you see its associated Attributes and Operations nodes in the MBean tree.- Expand the Attributes node of the
HelloMBean in the MBean tree.The MBean attributes that were defined by the
Helloclass are displayed.- Change the value of the
CacheSizeattribute to 150.In the terminal window in which you started
Main, a confirmation of this attribute change is generated.- Expand the Operations node of the
HelloMBean in the MBean tree.The two operations declared by the
HelloMBean,sayHello()andadd(), are visible.- Invoke the
sayHello()operation by clicking thesayHellobutton.A JConsole dialog box informs you that the method was invoked successfully. The message "hello, world" is generated in the terminal window in which
Mainis running.- Provide two integers for the
add()operation to add and click theaddbutton.The answer is displayed in a JConsole dialog box.
- To close JConsole, select Connection -> Exit.