Introduction to JMX

Source: Internet
Author: User

From: http://blog.csdn.net/zhangxiaogen/archive/2004/07/26/52352.aspx


JMX is becoming increasingly popular in various technical journals, white papers, and reports, although it is no longer a buzzword. Not just sun ?, Many vendors have recently announced that they are or are preparing to support this technology. Not only are large vendors such as IBM, Bea, HP, and marcomedia (JRun), but also many small software companies and open-source projects, including adventnet, JBoss (jboss3.0's micorkernel structure is built on JMX ).
Why is JMX so popular, or are there any advantages that require attention? In this article and a series of subsequent articles, we will learn and understand JMX step by step. [/I]

Introduction
Before learning the first instance, let's have a preliminary understanding of JMX. What is the so-called "all-in-one, all-win" J.
Let's take a look at the terminology in JMX:


  • Mbean: Short for managed bean. In JMX, mbean represents a managed resource instance. Through the methods and attributes exposed in mbean, the outside world can obtain the status of managed resources and manipulate mbean behaviors. In fact, mbean is a Java object. Like the JavaBean model, the outside world uses self-waking and reflection to obtain the object Value and call the object method, but mbean is more complex and advanced.
  • Mbeanserver: mbean is stored in an mbeanserver. Mbeanserver manages these mbeans and acts as a proxy for external access to them. In addition, mbeanserver provides a registration mechanism that allows external users to obtain the corresponding mbean instance by name.
  • JMX agent: The agent is only a Java Process, which includes the mbeanserver and a series of additional mbeanservices. Of course, these services are also published in the form of mbean.
  • Protocol adapters and connectors
    JMX agent communicates with the outside (outside JVM) through a variety of adapters and ctor. Similarly, external (outside JVM) must also send management or control requests to the JMX agent through an adapter and connector.
    The difference between the adapter and connector is that the adapter uses an Internet Protocol to get in touch with the JMX agent, and the agent has an object (adapter) to process details about the protocol. For example, SNMP adapter and HTTP adapter. Connector uses RPC-like methods to access the agent. Both the agent and client must have such an object to process the corresponding request and response. For example, RMI connector.
    JMX agent can contain any number of adapters, so it can be accessed in multiple ways.


Basic architecture of JMX:
JMX is divided into three layers, responsible for processing different transactions. They are:

  • Instrumentation Layer
    The instrumentation layer mainly includes a series of interface definitions and describes how to develop mbean specifications. Generally, resources managed by JMX are composed of one or more mbeans. Therefore, this resource can be any component developed by Java or developed by other languages packaged by javawrapper.
  • Agent Layer
    The agent is used to manage resources and provides interfaces for remote users to access. The agent layer is built on the intrumentation layer and uses and manages the components described in the instrumentation layer. Generally, an agent consists of an mbeanserver and multiple system services. In addition, the agent also provides one or more adapters or ctor for external access.
    The JMX agent does not care about the resources it manages.
  • Distributed Layer
    The distributed layer cares about how the agent is accessed by remote users. It defines a series of interfaces and components used to access the agent, including the description of the adapter and ctor.


Hello JMX! Step by step
Generally, adding a JMX management framework to our project requires access to all the three layers of JMX mentioned above. Next we will implement a simple hellojmx example to introduce the three-layer framework step by step.

Instrumentation Layer
The instrumentation layer defines a series of interfaces and a set of mbean specifications. Every mbean implemented using the JMX management framework must comply with this set of interfaces and specifications. There are two types of mbeans in JMX: static (standard) mbean and dynamic (dynamic) mbean. Standard mbean is easy to implement and only needs to comply with a set of inheritance specifications. It is especially suitable for developing projects. Dynamic mbean needs to inherit a dynamicmbean interface, which is complicated in development, but it can be dynamically modified at runtime, so it is flexible and powerful.

We will first implement a standardmbean here.
To implement standardmbean, a set of inheritance specifications must be followed. Each mbean must define an interface, and the interface name must be the name of the object class of the managed resource followed by "mbean ". For example, our object is Kert. jmxnotes. hellojmx. To construct a standardmbean, we must define the interface name as Kert. jmxnotes. hellojmxmbean. The agent depends on the standardmbean interface to access the managed resources. Therefore, you need to define the corresponding methods in hellojmxmbean.

public interface HelloJMXMBean {        void sayHello();              void hello(String msg);            String getMessage(); }

This mbean defines three methods: sayhello (), hello (string), and getmessage ().

The following is a real resource object. Due to naming restrictions, the object name must be hellojmx.

public class HelloJMX            implements HelloJMXMBean {            private String msg;            public void sayHello() {            System.out.println("Hello JMX " + (msg == null?"":msg));        }            public void hello(String msg) {           this.msg = msg;         }            public String getMessage() {            return msg;        } }

 


In this way, resources that can be managed by JMX are created.

Agent Layer
Generally, JMX agent is built in our program, that is, it is not an external service. We must construct a JMX agent shown in our application to manage our resources.
 
25FinalMbeanserver =
26 mbeanserverfactory. creatembeanserver (domain );
27FinalHellojmx hellombean =NewHellojmx ();
28FinalObjectname helloon =NewObjectname (domain + ": Name = hellojmx ");
29 mbeanserver. registermbean (hellombean, helloon );

(Line25 ~ 26) generally, we need to create an mbeanserver to manage our mbean. We usually use mbeanserver to obtain mbean information and indirectly call mbean methods.
(Line27) then generate an object for our resources. Resources managed by the JMX agent are no different from common Java objects. Therefore, you can use the common object creation method.
(Line28 ~ 29) then, we will display the registration object to mbeanserver. JMX uses the objectname in the SNMP specification as the way to identify and find mbeans.
Then, our hello mbean has been successfully registered in the mbeanserver. Like other component/Container systems, the container will proxy all component actions it manages.

Distributed Layer
We need to manage our mbean remotely. In the JMX structure introduction, we mentioned that the agent can have one or more adapters or ctor for remote user access. Like mbeanserver, we also need to show that our agent supports those adaptors and connector.

30FinalHtmladaptorserver htmladaptor =NewHtmladaptorserver ();
31FinalObjectname htmladaptoron =NewObjectname (domain + ": Name = htmladaptor ");
32 mbeanserver. registermbean (htmladaptor, htmladaptoron );
33 htmladaptor. setport (9999 );
34
37 log.info ("starting the htmladaptor ....");
38 htmladaptor. Start ();

A htmladaptor is provided in Sun's JMX reference implementation. Supports HTTP access protocol and has a good HTML interface. Our hellojmx uses this interface for remote management. Note that we need to display the port number for setting htmladaptor and call its start () method. In fact, htmladaptor is a simple httpserver that converts HTTP requests to JMX agent requests.

Run this program. Open the browser and enter http: // localhost: 9999. Then you can use browser as your management interface to manage your applications.

Notification
JMX also provides a notification mechanism. This kind of notification is determined by the user. When an application encounters a certain situation, you can use the notification to remind the management personnel.
It is also very easy to add notifications for our hellojmx.
First, declare that our application (hellojmx) supports JMX notifications. Just modify our hellojmx.

9Public classHellojmxExtendsNotificationbroadcastersupport
10ImplementsHellojmxmbean {

Similarly, hellojmx can support the notification mechanism by implementing the icationicationbroadcaster interface.
Then modify the hello (string): void method.

18Public voidHello (string MSG ){
19This. MSG = MSG;
20FinalNotification =NewNotification ("Kert. JMX. Hello ",This,-1,
21 system. currenttimemillis (), "message is changed ");
22 sendnotification (notification );
23}

We want to send a notification when the hello method is called.

Like the event model of Java2, a notification must have a receiver. In JMX, the role of this receiver is completed by an object that implements the icationicationlistener interface.
In our example, let helloagent assume this role.

12Public classHelloagentImplementsIcationicationlistener {
.......
43Public voidHandlenotification (Notification, object O ){
44 system. Out. println (This. Getclass (). getname () +
45 "Notification listener --" + notification. getmessage ());
46}


Download sun JMX = https://opendmk.dev.java.net/download/index.html here

To run the complete example, refer:

Http://oss.org.cn/ossdocs/java/jmx/jmx.html

Http://oss.org.cn/ossdocs/java/jmx/jmx2.html

Http://oss.org.cn/ossdocs/java/jmx/jmx3.html

  • Jmxremote_optional.jar (512.6 KB)
  • Jdmktk. Jar (663.2 KB)
  • Jdmkrt. Jar (2.4 MB)

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.