原文地址:http://nick-lab.gs.washington.edu/java/jdk1.5b/guide/jmx/tutorial/connectors.html
heading1">2.1 Implementing MBeans
, , and . This example demonstrates standard and dynamic MBeans only.
nes a connector based on RMI. The RMI connector supports the standard RMI transports,Java Remote Method Protocol (JRMP) and the Internet Inter-Object Request broker (ORB) Protocol (IIOP). This connector allows you to connect to an MBean in an MBean server from a remote location,and perform operations on it,exactly as if the operations were being performed locally.
and a MBean in the local MBean server
and a MBean on the remote MBean server
.
hey perform the operations described in the preceding section.
class is shown in several code excerpts.
class creates a new MBean server called by calling the
method of the
class.
method of the interface. The domain is identified by the string
.
is also identified by a variable,in this case the string .
is the name of the Java class for the Java object of which this MBean is an instance. The
object is examined in .
,is defined as the combination of the domain,plus the following key=value pairs:
,which in this case is the .
,to differentiate this MBean from other MBeans of the same type that might be created subsequently. In this case the index number is .
is to give the MBean a human-readable identifier.
,and are then performed on the
MBean. Like
,these methods are defined later in the
code,and are shown in and XXX.
is created and registered in the MBean server in exactly the same way as the MBean. As the name suggests,this MBean is an instance of the
Java object,which is examined in .
,an RMI connector server is created so that operations can be performed on the MBeans remotely. A call to theclass creates a new service URL called
,which serves as an address for the connector server. In this example,the service URL is given in ,rather than in (see the API documentation for the
package for an explanation of JNDI form). This service URL defines the following:
.
on the local host,and the server address will be registered under the name . The port specified in the example is arbitrary; you can use any available port.
is created by calling the constructor ,with the service URL
,a
environment map,and the MBean server
as parameters. The connector server
is launched by calling the
method of
,whereupon
exports the RMI object
to the RMI registry. The connection will remain open until the Enter key is pressed,as instructed by the simple method
pressed
,that is defined later in the code.
shows the definition of the method. In this method,the MBean instance with the object name
is passed to the
method of the
interface to create a new object name for registering the MBean inside the MBean server. The resulting object name instance is named
. A call to the
method
then instantiates an MBean defined by the combination of the Java object identified by
and the MBean instance
and registers this MBean in the MBean server
.
we see the definition of the method . The
method calls the
method
to obtain details of the attributes and operations that are exposed by the MBean
.
defines the following methods,each of which is called in turn to obtain information about the
MBean’s attributes:
,to obtain the attribute’s name.
,to obtain the human readable description of the attribute.
,to obtain the class name of the attribute.
,to determine whether or not the attribute is readable.
,to determine whether or not the attribute is writeable.
MBean’s constructors,operations and notifications:
,to obtain information about the MBean’s Java class.
,to learn what operations the MBean performs,and what parameters it takes.
,to find out what notifications the MBean sends when its operations are performed.
shows a method for managing a simple MBean.
method first of all calls the method that is also defined by
. The
method obtains an MBean attribute called
from the MBean
,as well as another MBean atttribute called
. Both of these attributes are defined in the
class,shown in .
method then defines an attribute called ,which is an instance of the
class. The
attribute associates a value of
with the existing attribute
,defined by
. A call to the
method
then sets the
MBean’s state to the new state defined by
.
method invokes the
MBean’s
operation. The
operation is defined in the
class.
class is shown in .
class is a straightforward JMX specification management interface for the MBean . This interface exposes the four operations defined by
for management through a JMX agent.
heading3">2.1.1.3 SimpleStandard.java
class is shown in .
class defines a straightforward JMX specification standard MBean.
MBean exposes operations and attributes for management by implementing the corresponding interface,shown in .
,which collects information about the number of changes carried out on the attribute before calling reset. The content of the notification sent is defined by the
instance.
class is shown in .
dynamic MBean shows how to expose attributes and operations for management at runtime,by implementing the interface. It starts by defining a method,
,for obtaining information for the MBean dynamically. The
method builds the
for the dynamic MBean.
corresponds to the implementation of the interface. The attributes,operations and notifications exposed are identical to those exposed by the
MBean.
class is shown in .
class implements a straightforward JMX specification notification listener.
method of the interface is called upon reception of a notification,and prints out a message to confirm that a notification has been received.
heading3">2.1.1.6 Client.java
class is shown in .
class creates an RMI connector client that is configured to connect to the RMI connector server created by .
defines the same service URL as that defined by
. This allows the connector client to retrieve the RMI connector server stub named
from the RMI registry running on port
of the local host,and to connect to the RMI connector server.
,is an instance of the interface ,created by the
method of
. The
method is passed the parameters
and a
environment map when it is called.
,to listen for notifications,as shown in .
,named ,is then created by calling the
method of the
instance
.
,and can register MBeans and perform operations on themwith the connection remaining completely transparent to both ends.
MBean and the SimpleDynamic MBean in the MBean server with a call to the method of
,and performs the operations defined by
and
as if they were local JMX specification MBean operations. The code for the different operations performed on
is not shown here,because the operations are the same as those performed on
.
MBean and closes the connection. The final is optional,as listeners registered by a remote client are removed when that client is closed.
heading2">2.1.2 Running the MBean Example
file:
- Compile the Java classes.
- Start an RMI registry on port
of the local host.
to register the RMI connector stub.
- Start the
class.
ssp
ath . Server MBean in the MBean server. You will then be prompted to press the Enter key to obtain information about,and then to perform operations on,the MBean.
have completed,the process will be repeated for the MBean.
.
- Start the
class in another terminal window.
ara
">You will see confirmation of the creation of the RMI connector client and of the connection with the connector server. You will also be informed of the domain name,and the creation and registration of and SimpleDynamic MBeans. The client will then perform operations on and SimpleDynamic MBeans,before unregistering them.