This section explains a special type of MBean, called MXBeans.
An MXBean is a type of MBean that references only a predefined set of data types. In this way, you can be sure that your MBean will be usable by any client, including remote clients, without any requirement that the client have access to model-specific classes representing the types of your MBeans. MXBeans provide a convenient way to bundle related values together, without requiring clients to be specially configured to handle the bundles.
In the same way as for standard MBeans, an MXBean is defined by writing a Java interface called
SomethingMXBeanand a Java class that implements that interface. However, unlike standard MBeans, MXBeans do not require the Java class to be calledSomething. Every method in the interface defines either an attribute or an operation in the MXBean. The annotation@MXBeancan be also used to annotate the Java interface, instead of requiring the interface's name to be followed by the MXBean suffix.MXBeans existed in the Java 2 Platform, Standard Edition (J2SE) 5.0 software, in the package
java.lang.management. However, users can now define their own MXBeans, in addition to the standard set that is defined injava.lang.management.The main idea behind MXBeans is that types such as
java.lang.management.MemoryUsagethat are referenced in the MXBean interface,java.lang.management.MemoryMXBeanin this case, are mapped into a standard set of types, the so-called Open Types that are defined in the packagejavax.management.openmbean. The exact mapping rules appear in the MXBean specification. However, the general principle is for simple types such as int or String to remain unchanged, while complex types such asMemoryUsageget mapped to the standard typeCompositeDataSupport.The MXBean example consists of the following files, which are found in
jmx_examples.zip:
QueueSamplerMXBeaninterfaceQueueSamplerclass that implements the MXBean interfaceQueueSampleJava type returned by thegetQueueSample()method in the MXBean interfaceMain, the program that sets up and runs the exampleThe MXBean example uses these classes to perform the following actions:
- Defines a simple MXBean that manages a resource of type
Queue<String>- Declares a getter,
getQueueSample, in the MXBean that takes a snapshot of the queue when invoked and returns a Java classQueueSamplethat bundles the following values together:
- The time the snapshot was taken
- The queue size
- The head of the queue at that given time
- Registers the MXBean in an MBean server
MXBean Interface
The following code shows the example
QueueSamplerMXBeanMXBean interface:package com.example; public interface QueueSamplerMXBean { public QueueSample getQueueSample(); public void clearQueue(); }Note that you declare an MXBean interface in exactly the same way as you declare a standard MBean interface. The
QueueSamplerMXBeaninterface declares a getter,getQueueSampleand an operation,clearQueue.Defining MXBean Operations
The MXBean operations are declared in the
QueueSamplerexample class, as follows:package com.example; import java.util.Date; import java.util.Queue; public class QueueSampler implements QueueSamplerMXBean { private Queuequeue; public QueueSampler(Queue queue) { this.queue = queue; } public QueueSample getQueueSample() { synchronized (queue) { return new QueueSample(new Date(), queue.size(), queue.peek()); } } public void clearQueue() { synchronized (queue) { queue.clear(); } } }
QueueSamplerdefines thegetQueueSample()getter andclearQueue()operation that were declared by the MXBean interface. ThegetQueueSample()operation returns an instance of theQueueSampleJava type which was created with the values returned by thejava.util.Queuemethodspeek()andsize(), and an instance ofjava.util.Date.Defining the Java Type Returned by the MXBean Interface
The
QueueSampleinstance returned byQueueSampleris defined in theQueueSampleclass, as follows:In thepackage com.example; import java.beans.ConstructorProperties; import java.util.Date; public class QueueSample { private final Date date; private final int size; private final String head; @ConstructorProperties({"date", "size", "head"}) public QueueSample(Date date, int size, String head) { this.date = date; this.size = size; this.head = head; } public Date getDate() { return date; } public int getSize() { return size; } public String getHead() { return head; } }QueueSampleclass, the MXBean framework calls all the getters inQueueSampleto convert the given instance into aCompositeDatainstance and uses the@ConstructorPropertiesannotation to reconstruct aQueueSampleinstance from aCompositeDatainstance.Creating and Registering the MXBean in the MBean Server
So far, the following have been defined: an MXBean interface and the class that implements it, as well as the Java type that is returned. Next, the MXBean must be created and registered in an MBean server. These actions are performed by the same
Mainexample JMX agent that was used in the standard MBean example, but the relevant code was not shown in the Standard MBean lesson.package com.example; import java.lang.management.ManagementFactory; import java.util.Queue; import java.util.concurrent.ArrayBlockingQueue; import javax.management.MBeanServer; import javax.management.ObjectName; public class Main { public static void main(String[] args) throws Exception { MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); [...] ObjectName mxbeanName = new ObjectName("com.example:type=QueueSampler"); Queuequeue = new ArrayBlockingQueue (10); queue.add("Request-1"); queue.add("Request-2"); queue.add("Request-3"); QueueSampler mxbean = new QueueSampler(queue); mbs.registerMBean(mxbean, mxbeanName); System.out.println("Waiting..."); Thread.sleep(Long.MAX_VALUE); } } The
Mainclass performs the following actions:
- Gets the platform MBean server.
- Creates an object name for the MXBean
QueueSampler.- Creates a
Queueinstance for theQueueSamplerMXBean to process.- Feeds the
Queueinstance to a newly createdQueueSamplerMXBean.- Registers the MXBean in the MBean server in exactly the same way as a standard MBean.
Running the MXBean Example
The MXBean example uses classes from the
jmx_examples.zipbundle that you used in the Standard MBeans section. This example requires version 6 of the Java SE platform. To run the MXBeans example follow these steps:
- If you have not done so already, save
jmx_examples.zipinto yourwork_dirdirectory.
- 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 generated.- 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
QueueSamplerthat was created and registered byMain. If you clickQueueSampler, you see its associated Attributes and Operations nodes in the MBean tree.- Expand the Attributes node.
You see the
QueueSampleattribute appear in the right pane, with its value ofjavax.management.openmbean.CompositeDataSupport.- Double-click the
CompositeDataSupportvalue.You see the
QueueSamplevaluesdate,head, andsizebecause the MXBean framework has converted theQueueSampleinstance intoCompositeData. If you had definedQueueSampleras a standard MBean rather than as an MXBean, JConsole would not have found theQueueSampleclass because it would not be in its class path. IfQueueSamplerhad been a standard MBean, you would have received aClassNotFoundExceptionmessage when retrieving theQueueSampleattribute value. The fact that JConsole findsQueueSamplerdemonstrates the usefulness of using MXBeans when connecting to JMX agents through generic JMX clients such as JConsole.- Expand the Operations node.
A button to invoke the
clearQueueoperation is displayed.- Click the
clearQueuebutton.A confirmation that the method was invoked successfully is displayed.
- Expand the Attributes node again, and double click on the
CompositeDataSupportvalue.The
headandsizevalues have been reset.- To close JConsole, select Connection -> Exit.