To 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.
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).
public interface SimpleStandardMBean { public String getState() ; public void setState(String s) ; public Integer getNbChanges() ; public void reset() ; } |
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.
Attributes are conceptual variables that are exposed for management through getter and setter methods in the MBean interface:
A getter is any public method whose name begins with get and which does not return void; it enables a manager to read the value of the attribute, whose type is that of the returned object.
A public method whose name begins with is and which returns a boolean or Boolean is also a getter, though a boolean attribute can have only one getter (it must be one form or the other).
A setter is any public method whose name begins with set and which takes a single parameter; it enables a manager to write a new value in the attribute, whose type is that of the parameter.
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:
public String[] getMessages(); public void setMessages(String[] msgArray);
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:
State is a readable and writable attribute of type String
NbChanges is a read-only attribute of type Integer
The specification of the design patterns for attributes implies the following rules:
Attributes can be read-only, write-only, or readable and writable.
Attribute names cannot be overloaded. For any given attribute name there can be at most one setter and one getter, and if both are defined, they must use the same type.
Operations 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:
public void getFoo(); public Integer getBar(Float p); public void setFoo(Integer one, Integer two); public String isReady();
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:
public String singleGetMessage(int index); public void singleSetMessage(int index, String msg);
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.