![]() |
|||
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
![]() ![]() |
![]() |
| |||||
Chapter 1Standard MBeansA standard MBean is the simplest and fastest way to instrument a resource from scratch: Attributes and operations are simply methods which follow certain design patterns. A standard MBean is composed of the MBean interface which lists the methods for all exposed attributes and operations, and the class which implements this interface and provides the functionality of the resource. The code samples in this chapter are from the files in the StandardMBean example directory located in the main examplesDir/current (see "Directories and Classpath" in the Preface). This chapter covers the following topics:
1.1 Exposing the MBean InterfaceTo expose the MBean interface, first determine the management interface of your resource, that is the information needed to manage it. This information is expressed as attributes and operations. An attribute is a value of any type that a manager can get or set remotely. An operation is a method with any signature and any return type that the manager can invoke remotely. Note - Attributes and operations are conceptually equivalent to properties and actions on JavaBeans objects. However, their translation into Java code is entirely different to accommodate the management functionality. As specified by the Java Management Extensions (JMX) instrumentation, all attributes and operations are explicitly listed in an MBean interface. This is a Java interface that defines the full management interface of an MBean. This interface must have the same name as the class that implements it, followed by the MBean suffix. Because the interface and its implementation are usually in different files, two files make up a standard MBean. For example, the class SimpleStandard (in the file SimpleStandard.java) has its management interface defined in the interface SimpleStandardMBean (in the file SimpleStandardMBean.java). Example 1-1 SimpleStandardMBean Interface
Only public methods in the MBean interface are taken into consideration for the management interface. When present, non-public methods should be grouped separately, to make the code clearer for human readers. 1.1.1 MBean AttributesAttributes are conceptual variables that are exposed for management through getter and setter methods in the MBean interface:
Attribute types can be arrays of objects, but individual array elements cannot be accessed individually through the getters and setters. Use operations to access the array elements, as described in the next section. The following code example demonstrates an attribute with an array type:
The name of the attribute is the literal part of the method name following get, is, or set. This name is case sensitive in all Java Dynamic Management Kit (Java DMK) objects that manipulate attribute names. Using these patterns, we can determine the attributes exposed in Example 1-1:
The specification of the design patterns for attributes implies the following rules:
1.1.2 MBean OperationsOperations are methods that management applications can call remotely on a resource. They can be defined with any number of parameters of any type and can return any type. The design patterns for operations are simple. Any public method defined in the MBean interface that is not an attribute getter or setter is an operation. For this reason, getters and setters are usually declared first in the Java code, so that all operations are grouped afterwards. The name of an operation is the name of the corresponding method. The SimpleStandardMBean in Example 1-1 defines one operation, reset, which takes no parameters and returns nothing. While the following methods define valid operations (and not attributes), to avoid confusion with attributes, these types of names should not be used:
For performance reasons, you might want to define operations for accessing individual elements of an array type attribute. In this case, use unambiguous operation names:
Note - The Java DMK imposes no restrictions on attribute types, operation attribute types, and operation return types. However, the developer must ensure that the corresponding classes are available to all applications manipulating these objects, and that they are compatible with the type of communication used. For example, attribute and operation types must be serializable to be manipulated remotely using the remote method invocation (RMI) or JMX messaging protocol (JMXMP) protocols. 1.2 Implementing an MBeanThe second part of an MBean is the class that implements the MBean interface. This class encodes the expected behavior of the manageable resource in its implementation of the attribute and operation methods. The resource does not need to reside entirely in this class. The MBean implementation can rely on other objects. If the MBean must be instantiated remotely, it must be a concrete class and must expose at least one public constructor so that any other class can create an instance. Otherwise, the developer is free to implement the management interface in any way, provided that the object has the expected behavior. Here is the sample code that implements our MBean interface. Example 1-2 SimpleStandard Class
| |||||
| |||||
![]() |