Содержащиеся вНайти другие документыРесурсы поддержки | Загрузить это руководство в формате PDF (336 КБ)
Chapter 2 Dynamic MBeansA dynamic MBean implements its management interface programmatically, instead of through static method names. To do this, it relies on descriptor classes which represent the attributes and operations exposed for management. Management applications then call generic getters and setters whose implementation must resolve the attribute or operation name to its intended behavior. One advantage of this instrumentation is that you can use it to quickly make an existing resource manageable. The implementation of the DynamicMBean interface can provide an instrumentation wrapper for an existing resource. Another advantage is that the descriptor classes for the management interface can provide human-readable descriptions of the attributes, operations and MBean itself. This information could be displayed to a user on a management console to describe how to interact with this particular resource. The code samples in this topic are taken from the files in the DynamicMBean example directory located in the main examplesDir (see "Directories and Classpath" in the preface). Contents:
Exposing the Management InterfaceIn the standard MBean, attributes and operations are exposed statically in the names of methods in the MBean interface. Dynamic MBeans all share the same interface which defines generic methods to access attributes and operations. Since the management interface is no longer visible through introspection, dynamic MBeans must also provide a description of their attributes and operations explicitly. The DynamicMBean InterfaceThe DynamicMBean class is a Java interface defined by the Java Management extensions. It specifies the methods that a resource implemented as a dynamic MBean must provide to expose its management interface. Here is an uncommented version of the code: Example 2-1 The DynamicMBean Interface
The getMBeanInfo method is the one which provides a description of the MBean's management interface. This method returns an MBeanInfo object which is the descriptor type that contains information about attributes and operations. The attribute getters and setters are generic, since they take the name of the attribute which needs to be read or written. For convenience, dynamic MBeans must also define bulk getters and setters to operate on any number of attributes at once. These methods use the Attribute and AttributeList classes to represent attribute name-value pairs and lists of name-value pairs, respectively. Since the names of the attributes are not revealed until runtime, the getters and setters are necessarily generic. In the same way, the invoke method takes the name of an operation and its signature, in order to invoke any method which might be exposed. As a consequence of implementing generic getters, setters, and invokers, the code for a dynamic MBean is more complex than for a standard MBean. For example, instead of having a specific getter called by name, the generic getter must verify the attribute name and then encode the functionality to read each of the possible attributes. The MBean Descriptor ClassesA dynamic MBean has the burden of building the description of its own management interface. The JMX specification defines the Java objects used to completely describe the management interface of an MBean. Dynamic MBeans use these objects to provide a complete self description as returned by the getMBeanInfo method. Agents also use these classes to describe a standard MBean after it has been introspected. As a group, they are referred to as the MBean descriptor classes because they provide information about the MBean. This information includes the attributes and operations of the management interface but also the list of constructors for the MBean class and the notifications that the MBean may send. Notifications are messages in the event mechanism that is defined by the JMX architecture; they are covered in the notification example in examplesDir/Notification. Each element is described by its descriptor object containing its name, a description string, and its characteristics. For example, an attribute has a type and is readable and/or writeable. The following table lists all MBean descriptor classes:
Implementing a Dynamic MBeanA dynamic MBean consists of a class that implements the DynamicMBean interface coherently. By this, we mean a class which exposes a management interface whose description matches the attributes and operations which are accessible through the generic getters, setters and invokers. Note - MBeans are not allowed to be both standard and dynamic. When a class is instantiated as an MBean, the agent checks the interfaces that it implements. If the class implements or inherits an implementation of both the corresponding MBean interface and the DynamicMBean interface, then an exception is raised and the MBean cannot be created. Beyond this restriction, a dynamic MBean must also follow the same two rules as a standard MBean, namely:
Thereafter, a dynamic MBean class is free to declare any number of public or private methods and variables. None of these are visible to management applications, only the methods implementing the DynamicMBean interface are exposed for management. A dynamic MBean is also free to rely on other classes which may be a part of the manageable resource. Dynamic Programming IssuesAn MBean is a manageable resource that exposes a specific management interface. The name dynamic MBean refers to the fact that the interface is revealed at runtime, as opposed to through the introspection of static class names. The term dynamic is not meant to imply that the MBean can dynamically change its management interface. The management architecture defined by JMX and implemented in the Java Dynamic Management Kit does not support MBeans whose management interface is modified during runtime. This is not an issue with standard MBeans which would need to be recompiled in order to change their interface. However, dynamic MBeans could be programmed so that their interface description and their generic getters, setters and the invoker have a different behavior at different times. In practice, this type of MBean could be created but it couldn't be managed after any change of interface. As a rule, the value returned by the MBeanInfo method of a dynamic MBean, and the corresponding behavior of getters, setters and the invoker, must never change over the lifetime of a given instance of the MBean. However, it is permissible to have the same dynamic MBean class expose different management interfaces depending upon the instantiation conditions. This would be a valid MBean, since the agent architecture manages object instances, not class types. It would also be a very advanced MBean for a complex management solution, beyond the scope of this tutorial. The getMBeanInfo MethodSince the MBean description should never change, it is usually created once at instantiation, and the getMBeanInfo method just returns its reference at every call. The MBean constructor should therefore build the MBeanInfo object from the MBean descriptor classes such that it accurately describes the management interface. And since most dynamic MBeans will always be instantiated with the same management interface, building the MBeanInfo object is fairly straightforward. The following code shows how the SimpleDynamic MBean defines its management interface, as built at instantiation and returned by its getMBeanInfo method: Example 2-2 Implemention of the getMBeanInfo Method
Generic Attribute Getters and SettersGeneric getters and setters take a parameter that indicates the name of the attribute to read or write. There are two issues to keep in mind when implementing these methods:
The getAttribute method is the simplest, since only the attribute name must be verified: Example 2-3 Implementation of the getAttribute Method
The setAttribute method is more complicated, since you must also insure that the given type can be assigned to the attribute and handle the special case for a null value: Example 2-4 Implementation of the setAttribute Method
Notice that the generic getter and setter methods usually hard-code information about the attributes. If a change in your management solution requires you to change your management interface, it will be harder to do with a dynamic MBean. In a standard MBean, each attribute and operation is a separate method, so unchanged attributes are unaffected. In a dynamic MBean, you must modify these generic methods that encode all attributes. Bulk Getters and SettersThe DynamicMBean interface includes bulk getter and setter methods for reading or writing more than one attribute at once. These methods rely on the following classes:
The AttributeList class extends the java.util.ArrayList class which is specific to Java 2. For this class and others that rely on similar sets and collection, the Java Dynamic Management Kit provides the collections.jar file for complete compatibility using any JDK version 1.1.x. See Directories and Classpath in the preface for more information. The bulk getter and setter methods usually rely on the generic getter and setter, respectively. This makes them independent of the management interface, which can simplify certain modifications. In this case, their implementation consists mostly of error checking on the list of attributes. However, all bulk getters and setters must implement the following behavior: an error on any one attribute does not interrupt or invalidate the bulk operation on the other attributes. If an attribute cannot be read, then its name-value pair does not figure in the list of results. If an attribute cannot be written, it will not be copied to the returned list of successful set operations. As a result, if there are any errors, the lists returned by bulk operators will not have the same length as the array or list passed to them. In any case, the bulk operators do not guarantee that their returned lists have the same ordering of attributes as the input array or list. The SimpleDynamic MBean shows one way of implementing the bulk getter and setter methods: Example 2-5 Implementation of the Bulk Getter and Setter
Generic Operation InvokerFinally, a dynamic MBean must implement the invoke method so that operations in the management interface can be called. This method requires the same considerations as the generic getter and setter:
The implementation in the SimpleDynamic MBean is relatively simple due to the one operation with no parameters: Example 2-6 Implementation of the invoke Method
As it is written, the SimpleDynamic MBean correctly provides a description of its management interface and implements its attributes and operations. However, this example demonstrates the need for a strict coherence between what is exposed by the getMBeanInfo method and what can be accessed through the generic getters, setters, and invoker. A dynamic MBean whose getMBeanInfo method describes an attribute or operation which cannot be accessed is not compliant with the Java Management extensions and is technically not a manageable resource. Similarly, a class could make attributes or operations accessible without describing them in the returned MBeanInfo object. Since MBeans should raise an exception when an undefined attribute or operation is accessed, this would, again, technically not be a compliant resource. Running the Dynamic MBean ExampleThe examplesDir/DynamicMBean directory contains the SimpleDynamic.java file which makes up the MBean. The DynamicMBean interface is defined in the javax.management package provided in the run-time jar file (jdmkrt.jar) of the Java Dynamic Management Kit. This directory also contains a simple agent application which instantiates this MBean, calls its getMBeanInfo method to get its management interface and manipulates its attributes and operations. Compile all files in this directory with the javac command. For example, on the Solaris platform, you would type:
To run the example, launch the agent class which will interact with the SimpleDynamic MBean:
Type return when the application pauses to step through the example. The agent application handles all input and output in this example and gives us 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. Comparison with the SimpleStandard ExampleNow that we have implemented both types of MBeans we can compare how they are managed. We purposely 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 may vary):
If the two agent classes had the same name, we see that the only programmatic difference would be the following:
We can see that there is only one difference between the two example agents handling different types of MBeans: 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. This is the power of the JMX architecture: managers interact with the attributes and operations of a manageable resource, and the specification of the agent hides any implementation differences between MBeans. Since 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:
Dynamic MBean Execution TimeIn the introduction to this topic 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 the Chapter 1, Standard MBeans topic. When seeking improved performance, there are two situations which must be considered: MBean introspection, and management operations. Since the dynamic MBean provides its own description, the agent doesn't 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: calling the getters, setters and invoker. As we shall see in the next lesson ("The MBean Server in a Minimal Agent"), 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. Since 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. |
||||||||||||||||||||||||||||||||