Basic Example of JMX Technology--转载

原文地址: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.

  • The server:
  • Creates an MBean server
  • Registers a  and a  MBean in the local MBean server
  • Performs local operations on the MBeans
  • Creates an RMI connector server
  • The client:
  • Creates an RMI connector
  • Registers a  and a  MBean on the remote MBean server
  • Performs remote operations on both MBeans
  • .

    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:

  • The ,which in this case is the .
  • An ,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:

  • The connector will use the default RMI transport,denoted by .
  • The RMI registry in which the RMI connector stub will be stored will be running on port  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 methodpressed,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 .

  • To define a state.
  • To update this state.
  • To count the number of times the state is updated
  • To reset the values of the state and the number of changes to their original value of zero
  • To send a notification whenever the reset operation is invoked
  • ,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 .

    public class Client { public static void main(String[] args) { try { // Create an RMI connector client // JMXServiceURL url = new JMXServiceURL( "service:jmx:rmi:///jndi/rmi://localhost:9999/server"); JMXConnector jmxc = JMXConnectorFactory.connect(url,null); ClientListener listener = new ClientListener(); MBeanServerConnection mbsc = jmxc.getMBeanServerConnection(); waitForEnterPressed(); // Get domains from MBeanServer // String domains[] = mbsc.getDomains(); for (int i = 0; i < domains.length; i++) { System.out.println("Domain[" + i + "] = " + domains[i]); } waitForEnterPressed(); String domain = mbsc.getDefaultDomain(); // Create SimpleStandard MBean // ObjectName mbeanName = new ObjectName(domain +":type=SimpleStandard,index=2"); mbsc.createMBean("SimpleStandard",stdMBeanName,null); waitForEnterPressed(); // Create SimpleDynamic MBean // ObjectName dynMBeanName = new ObjectName(domain +":type=SimpleDynamic,index=2"); echo("\nCreate SimpleDynamic MBean..."); mbsc.createMBean("SimpleDynamic",dynMBeanName,null); waitForEnterPressed(); // Get MBean count // echo("\nMBean count = " + mbsc.getMBeanCount()); // Query MBean names // echo("\nQuery MBeanServer MBeans:"); Set names = mbsc.queryNames(null,null); for (Iterator i = names.iterator(); i.hasNext(); ) { echo( "ObjectName = " + (ObjectName) i.next()); } waitForEnterpressed(); mbsc.setAttribute(stdMBeanName, new Attribute("State","changed state")); SimpleStandardMBean proxy = (SimpleStandardMBean) MBeanServerInvocationHandler.newProxyInstance( mbsc, stdMBeanName, SimpleStandardMBean.class, false); echo("\nState = " + proxy.getState()); ClientListener listener = new ClientListener(); mbsc.addNotificationListener(stdMBeanName,listener,null); mbsc.invoke(stdMBeanName,null); mbsc.removeNotificationListener(stdMBeanName,listener); mbsc.unregisterMBean(stdMBeanName); [...] jmxc.close(); } catch (Exception e) { e.printStackTrace(); } } } [...]

     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.

    sspath . 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.

    相关文章

    HashMap是Java中最常用的集合类框架,也是Java语言中非常典型...
    在EffectiveJava中的第 36条中建议 用 EnumSet 替代位字段,...
    介绍 注解是JDK1.5版本开始引入的一个特性,用于对代码进行说...
    介绍 LinkedList同时实现了List接口和Deque接口,也就是说它...
    介绍 TreeSet和TreeMap在Java里有着相同的实现,前者仅仅是对...
    HashMap为什么线程不安全 put的不安全 由于多线程对HashMap进...