Java Dynamic Management Kit 5.1 Tutorial

2.3 Running the Dynamic MBean Example

The examplesDir/current/DynamicMBean directory contains the file that makes up the MBean. The DynamicMBean interface is defined in the package provided in the runtime JAR file (jmx.jar) of the Java Dynamic Management Kit (Java DMK). This directory also contains a simple agent application that instantiates this MBean, calls its getMBeanInfo method to get its management interface, and manipulates its attributes and operations.

To Run the Dynamic MBean Example
  1. Compile all files in this directory with the javac command.

    For example, on the Solaris platform, type:

    $ cd examplesDir/current/DynamicMBean/
    $ javac -classpath classpath *.java
  2. To run the example, start the agent class that will interact with the SimpleDynamic MBean:

    $ java -classpath classpath DynamicAgent
  3. Press Enter when the application pauses, to step through the example.

    The agent application handles all input and output in this example and gives a view of the MBean at runtime.

This example demonstrates how the management interface encoded in the getMBeanInfo method is made visible in the agent application. We can then see the result of calling the generic getters and setters and the invoke method. Finally, the code for filtering attribute and operation errors is exercised, and we see the exceptions from the code samples as they are raised at runtime.

2.3.1 Comparison With the SimpleStandard Example

Now that we have implemented both the standard and dynamic types of MBeans, we can compare how they are managed. We intentionally created a dynamic MBean and a standard MBean with the same management interface so that we can do exactly the same operations on them. On the Solaris platform, we can compare the relevant code of the two agent applications with the diff utility (your output might vary):

$ cd examplesDir/current
$ diff ./StandardMBean/ ./DynamicMBean/
< public class StandardAgent {
> public class DynamicAgent {
<     public StandardAgent() {
>     public DynamicAgent() {
<       StandardAgent agent = new StandardAgent();
>       DynamicAgent agent = new DynamicAgent();
<       echo("\n>>> END of the SimpleStandard example:\n");
>       echo("\n>>> END of the SimpleDynamic example:\n");
<       String mbeanName = "SimpleStandard";
>       String mbeanName = "SimpleDynamic";

If the two agent classes had the same name, the only programmatic difference would be the following:

<       String mbeanName = "SimpleStandard";
>       String mbeanName = "SimpleDynamic";

We can see that the only difference between the two example agents handling different types of MBeans is the name of the MBean class that is instantiated. In other words, standard and dynamic MBeans are indistinguishable from the agent's point of view. The JMX architecture enables managers to interact with the attributes and operations of a manageable resource, and the specification of the agent hides any implementation differences between MBeans.

Because we know that the two MBeans are being managed identically, we can also compare their runtime behavior. In doing so, we can draw two conclusions:

2.3.2 Dynamic MBean Execution Time

In the introduction to this chapter we presented two structural advantages of dynamic MBeans, namely the ability to wrap existing code to make it manageable and the ability to provide a self-description of the MBean and its features. Another advantage is that using dynamic MBeans can lead to faster overall execution time.

The performance gain depends on the nature of the MBean and how it is managed in the agent. For example, the SimpleDynamic MBean, as it is used, is probably not measurably faster than the SimpleStandard example in Chapter 1, Standard MBeans. When seeking improved performance, there are two situations that must be considered:

Because the dynamic MBean provides its own description, the agent does not need to introspect it as it would a standard MBean. Since introspection is done only once by the agent, this is a one-time performance gain during the lifetime of the MBean. In an environment where there are many MBean creations and where MBeans have a short lifetime, a slight performance increase can be measured.

However, the largest performance gain is in the management operations, when calling the getters, setters and invoker. As we shall see in Part II, the agent makes MBeans manageable through generic getters, setters, and invokers. In the case of standard MBeans, the agent must do the computations for resolving attribute and operation names according to the design patterns. Because dynamic MBeans necessarily expose the same generic methods, these are called directly by the agent. When a dynamic MBean has a simple management interface requiring simple programming logic in its generic methods, its implementation can show a better performance than the same functionality in a standard MBean.