JMX Notes ---- Part I (Hellojmx) Kert Original (Participation: 558, Expert): 2002-9-5 6:00 PM Update: 2002-9-5 6:11 PM: 1.0 Read : 3617 times
[i] JMX is increasingly appearing in a variety of technical magazines, white paper and reports, although it is no longer a buzzword. Not only Sun ?, Many manufacturers have announced that they have been or ready to support this technology. Not only IBM, BEA, HP, MarcoMedia (JRUN) these large vendors, but also many small software companies and open source projects have also joined this ranks, including AdventNet, JBoss, etc. (JBoss3.0's Micorkernel structure is built. On the basis of JMX). Why is JMX being welcomed, or JMX has those advantages that only have to pay attention? In this series of articles, we will learn and understand JMX in a series of articles. [/ i] Introduction Before learning the first instance, let's take some initial understanding of JMX. It is the so-called "knowing the roots, the hundred battles" is "J. Let's take a look at the terms in JMX:
MBean: is a referusion of Managed Bean. In the JMX, MBean represents a managed resource instance, through the methods and properties exposed in the MBean, the outside world can get the status of the managed resource and manipulate the behavior of MBean. In fact, MBean is a Java Object. Like the JavaBean model, the outside world uses a wake-up and reflection to obtain the value of Object and the method of calling Object, just MBean is more complex and advanced. MBeanServer: MBean is survive in an MBeanserver. MBeanServer manages these MBeans and proxy outside the access to them. And MBeanServer provides a registration mechanism that the outside world can be obtained by the name to get the corresponding MBEAN instance. JMX Agent: Agent is just a Java process, which includes this MBeanServer and a series of additional MBeanService. Of course, these services are also released by MBean. Protocol Adapters and Connectorsjmx Agent communicates with the outside (JVM) through a wide variety of Adapter and Connector. The same external (Outside JVM) must also send management or control requests to JMX Agent through an Adapter and Connector. The difference between Adapter and Connector is that Adapter is to contact the JMX Agent with a JMX Agent, and an agent is an object to handle details of the protocol. For example, SNMP Adapter and HTTP Adapter. The Connector is to access the Agent using similar RPCs, and there must be such an object to handle the corresponding request and response in the Agent side and the client. For example, RMI Connector. JMX Agent can have any multiple Adapters, so you can use a variety of different ways to access the Agent. JMX Basic Software: JMX is divided into three layers, which is responsible for handling different transactions. they are, respectively:
The Instrumentation layer The Instrumentation layer mainly includes a range of interface definitions and descriptions how to develop MBean specifications. Usually, the resources managed by JMX have one or more MBeans, so this resource can be any component developed by Java language, or a resource developed by a JavaWrapper package. The Agent layer agent is used to manage the corresponding resources and provide access to the remote user. The Agent layer is built on the Intrumentation layer and uses and manages the components described inside the INSTRUMENTATION layer. Usually the Agent consists of a MBeanServer and multiple system services. In addition, Agent also provides one or more Adapter or Connector for access to the outside world. JMX Agent doesn't care what the resources it managed. Distributed Layer Distributed Layer Care for the details of how the Agent is accessible by remote users. It defines a range of interfaces and components to access the Agent, including the Adapter and Connector descriptions. Hello JMX! Step By Step usually, joining the JMX management framework in our project needs to come into contact with the three-layer mentioned JMX mentioned. Next, we will implement a simple hellojmx example to introduce this three-layer framework. The Instrumentation layer The Instrumentation layer defines a range of interfaces and a set of implementations of MBean. Each MBean we implemented using the JMX management framework needs to meet this set of interfaces and specifications. There are two types of MBean: Static MBean and dynamic MBeans in JMX. Standard MBean is simple, just in line with a set of inheritance specification, especially for projects being developed. Dynamic MBean needs to inherit a Dynamicmbean interface, which is more complicated, but can be dynamically modified during runtime and powerful. We are here to implement a StandardMbean. In order to achieve STANDARDMBEAN, a set of inheritance specifications must be followed. Each MBean must define an interface, and the name of this interface must be the name of the object class of its managed resource, add "MBean". For example: our object is kert.jmxnotes.Hellojmx, in order to construct a StandardMbean, the name of the interface we must define is kert.jmxnotes.Hellojmxmbean. The agent relies on the STANDARDMBean interface to access the managed resource, so you need to define the corresponding method in the HelloJMXMBean. 6 public interface hellojmbembean {7 void syhello (); 8 9 Void Hello (String MSG); 10 11 String getMessage (); 12} Defined three methods in this MBean, SayHello (), Hello (String) and GetMessage (). Next is a real resource object, because the name specification is limited, the object name must be Hellojmx.
9 Public Class Hellojmx 10 Implements Hellojmxmbean {11 12 PRIVATE STRING MSG; 13 14 Public Void Sayhello () {15 System.out.Println ("Hello JMX" (MSG == Null? "": Msg)); 16} 17 18 Public void Hello (String MSG) {19 this.msg = msg; 23} 24 25 Public String getMessage () {26 return; 27} 28} This can be created by JMX management resources. The Agent layer usually JMX Agent is built-in in our program, which means it is not an external service. We must construct a JMX Agent in our app to manage our resources. 25 final MBeanServer mBeanServer = 26 MBeanServerFactory.createMBeanServer (DOMAIN); 27 final HelloJMX helloMBean = new HelloJMX (); 28 final ObjectName helloON = new ObjectName (DOMAIN ": name = HelloJMX"); 29 mBeanServer.registerMBean (helloMBean, helloON) (LINE25 ~ 26) Usually we first need to build a MBeanServer, MBeanServer is used to manage our MBean. We usually get the information of our MBean through MBeanServer, indirect call MBEAN. (line27) Then generate an object of our resources. JMX Agent managed resources and normal Java objects are not different, so use the normal establishment of objects. (LINE28 ~ 29) Then, we want to display this object to the MBeanServer to register. JMX uses ObjectName in the SNMP specification as an identifier and lookup MBean. After that, our Hello MBean has been successfully registered in the MBeanserver. Like the other Component / Container mode, Container will proxy all the Component's behavior it managed. DISTRIBUTED layer We need to manage our MBean at the distal end, in the JMX Structure Introduction We mention Agent to have one or more Adapter or Connector for far-sized user access only. As with MBeanServer, we also need to show that our Agent can support those Adaptor and Connector.
30 final HtmlAdaptorServer htmlAdaptor = new HtmlAdaptorServer (); 31 final ObjectName htmlAdaptorON = new ObjectName (DOMAIN ": name = HtmlAdaptor"); 32 mBeanServer.registerMBean (htmlAdaptor, htmlAdaptorON); 33 htmlAdaptor.setPort (9999); 34 37 LOG. INFO ("Starting The Htmladaptor ...."); 38 htmladaptor.start (); an HTMLADAPTOR is provided in the SUN JMX reference implementation. Support HTTP access protocols and have a nice HTML interface. Our Hellojmx is an interface that uses this as a remote management. Note We need to display the port number of HTMLADAPTOR, and call its start () method. Because in fact, HTMLADAPTOR is a simple HTTPServer, it converts HTTP requests to JMX Agent's request. Run this program, open your browser to enter http: // localhost: 9999 You can use Browser as your management interface to manage your application. NOTIFICATIONJMX also provides a notification mechanism. This notification is determined by the user. When an application occurs some situation, a notification can be used to remind the manager. Join our HelloJMX is also very simple. First, we must declare our application (hellojmx) support JMX notification. Just modify our Hellojmx. 9 Public Class Hellojmx Extends NotificationBroadcasterSupport 10 Implements Hellojmxmbean {The same HelloJMX can also support the notification mechanism by implementing the NotificationBroadCaster interface. Then modify the Hello (String): Void method. 18 public void Hello (String MSG) {19 this.msg = msg; 20 final notification notification = new notification ("kert.jmx.hello", this, -1, 21 system.currenttimemillis (), "message is change") 22 SendNotification (Notification); 23} We want to send a notification when hello is called. Like Java2's event model, the notice must have an acceptor. In JMX, this recipient's role is completed by an object that implements the NotificationListener interface. In our example, we let HelloAgent act as this role.