Basic example of JMX technology — reprint

Original address: 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. The server: Creates an MBean server@H_419_23 @ Registers a and a MBean in the local MBean server Performs local operations on the MBeans@H_419_23 @ Creates an RMI connector server The client:@H_ 419_ 23@ Creates an RMI connector Registers a and a MBean on the remote MBean server@H_419_23 @ Performs remote operations on both MBeans . Open the directory. @ H_ 419_ 23@ SimpleDynamic. java@H_419_23 @ @H_ 419_ 23@ Open each of the files in a text editor.@ H_ 419_ 23@ class is shown in several code excerpts. public class Server { public static void main(String[] args) { try { MBeanServer mbs = MBeanServerFactory.createMBeanServer(); waitForEnterPressed(); String domain = mbs.getDefaultDomain(); waitForEnterPressed(); String mbeanClassName = "SimpleStandard"; String mbeanObjectNameStr = domain + ":type=" + mbeanClassName + ",index=1" ; ObjectName mbeanObjectName = createSimpleMBean(mbs,mbeanClassName,mbeanObjectNameStr); waitForEnterPressed(); printMBeanInfo(mbs,mbeanObjectName,mbeanClassName); waitForEnterPressed(); manageSimpleMBean(mbs,mbeanClassName); waitForEnterPressed(); mbeanClassName = "SimpleDynamic"; mbeanObjectNameStr = domain + ":type=" + mbeanClassName + ",index=1"; mbeanObjectName = createSimpleMBean(mbs,mbeanObjectNameStr); waitForEnterPressed(); printMBeanInfo(mbs,mbeanClassName); waitForEnterPressed(); manageSimpleMBean(mbs,mbeanClassName); waitForEnterPressed(); [...] 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 .@ H_ 419_ 23@ 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 . [...] JMXServiceURL url = new JMXServiceURL("service:jmx: rmi:///jndi/rmi://localhost:9999/server "); JMXConnectorServer cs = JMXConnectorServerFactory.newJMXConnectorServer(url,null,mbs); cs.start(); waitForEnterPressed(); cs.stop(); [...] 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 .@ H_ 419_ 23@ 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 method,that is defined later in the code. [...] private static ObjectName createSimpleMBean(MBeanServer mbs, String mbeanClassName, String mbeanObjectNameStr) { echo("\n>>> Create the " + mbeanClassName + " MBean within the MBeanServer"); echo( "ObjectName = " + mbeanObjectNameStr); try { ObjectName mbeanObjectName = ObjectName.getInstance(mbeanObjectNameStr); mbs.createMBean (mbeanClassName,mbeanObjectName); return mbeanObjectName; } catch (Exception e) { echo( "!!! Could not create the " + mbeanClassName + " MBean !!!"); e.printStackTrace(); echo("\nEXITING...\n"); System. exit(1); } return null; } [...] 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 . private static void printMBeanInfo(MBeanServer mbs, ObjectName mbeanObjectName, String mbeanClassName) { MBeanInfo info = null; try { info = mbs.getMBeanInfo(mbeanObjectName); } catch (Exception e) { echo( "!!! Could not get MBeanInfo object for " + mbeanClassName +" !!!"); e.printStackTrace(); return; } MBeanAttributeInfo[] attrInfo = info. getAttributes(); if (attrInfo.length > 0) { for (int i = 0; i < attrInfo.length; i++) { echo(" ** NAME: " + attrInfo[i].getName()); echo(" DESCR: " + attrInfo[i].getDescription()); echo(" TYPE: " + attrInfo[i].getType() + "READ: "+ attrInfo[i].isReadable() + "WRITE: "+ attrInfo[i].isWritable()); } } else echo(" ** No attributes **"); [...] . 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.@ H_ 419_ 23@

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>