Getting started with JMX-hellojmx

Source: Internet
Author: User
Tags snmp

1. Introduction to JMX
What is JMX? In an article on the Internet, "JMX (Java Management Extensions) is a framework that embeds management functions for applications. JMX is a set of standard proxies and services. In fact, you can use these proxies and services for management in any Java application.
Let's start with what benefits JMX can provide to us. For example, an application may have some configuration information in a system, such as the IP address and port number of the service. How can I write the code?
1. The first brother of the program is generally written into the program. When the program needs to be changed, the program will be changed and then compiled and released;
2. programmers usually write this information in a configuration file (Java is generally *. properties file), you only need to change the configuration file, but restart the system to read the new values in the configuration file;
3. The program will write a code segment to cache the configuration value. When the system reads the value, check whether the configuration file is changed. If any, repeat it again; otherwise, the value is read from the cache;
4. Programmers know how to get things for me. Use JMX! Set the configuration attributes to a class, write a message called mbean, and configure the configuration. In addition, JMX automatically provides a web page for you to change the configuration information.
// 1 The write is killed in the program. If the write is not completed, it is certainly not running, and there is no parameter change. 2. re-import the attribute file and restart it. 4. You can dynamically adjust and configure the file; it is highly isolated from programs and does not need to be re-compiled or restarted;
2. Preparations
JMX is a specification. Sun provides the JMX interface in JDK (1.3, 1.4, 5.0) based on this specification. There are many implementations based on this interface, such as the JMX Implementation of Weblogic, mx4j, and JBoss. Sun has also implemented a copy of it, but before jdk1.4, this JMX implementation (some jar packages) is optional and you have to download it on its website. Jdk5.0 is embedded, and JMX-based code can be developed after jdk5.0 is installed.
However, jdk5.0 does not contain all Sun's JMX code. Some tool classes are excluded from jdk5.0. Next, let's talk about the preparation of the development environment based on the JDK version used.
1. jdk1.3 and 1.4
Go to the Sun website to download Sun's JMX implementation. There are two zip files. Download URL: http://java.sun.com/products/javamanagement/download.html.
(1javasjmx-1_2_1-ri.zip
The decompressed lib directory includes: jmxri. jar and jmxtools. jar.
(2)jmx_remote-1_0_1_03-ri.zip
The decompressed lib directory includes: jmxremote. jar, jmxremote_optional.jar, and rmissl. jar.
If you use command line development in DOS, add these five jar packages to the classpath system variable. If you use eclipse for development, add the jar package to the libratries (Library) Reference of the Project attribute.
2. jdk5.0
JRE/lib/RT. jar of jdk5.0 already contains the code of jmxri. jar, jmxremote. jar, and rmissl. jar. If you use the jmxtools. jar and jmxremote_optional.jar classes, you need to add these two classes to the classpath or Eclipse project library reference.
3. My development environment: jdk5.0 + eclipse3.2.
Note: The htmladaptorserver class in jmxtools. jar is used, so this package is added to the project library reference. Jmxremote_optional.jar is not used at the moment.

Iii. helloworld instance
1. Hello is a class to be managed (common class)
/**
* @ Author chengang 2005-12-3
*/
Public class Hello implements hellombean {
Private string name;
Public String getname (){
Return name;
}
Public void setname (string name ){
This. Name = Name;
}
Public void printhello (){
System. Out. println ("Hello World," + name );
}
Public void printhello (string whoname ){
System. Out. println ("hello," + whoname );
}
}

2. To manage hello, you must create a corresponding mbean as follows :/**
* @ Author chengang 2005-12-3
*/
Public interface hellombean {
Public String getname ();
Public void setname (string name );
Public void printhello ();
Public void printhello (string whoname );
}
Note: All methods contained in mbean can be managed. Mbean naming is standardized, that is, adding mbean after the original class name.

3. Create an agent class
Import javax. Management. mbeanserver;
Import javax. Management. mbeanserverfactory;
Import javax. Management. objectname;
Import com. Sun. Jdmk. Comm. htmladaptorserver;

Public class helloagent {

Public static void main (string [] ARGs) throws exception {
Mbeanserver Server = mbeanserverfactory. creatembeanserver ();

Objectname helloname = new objectname ("chengang: Name = helloworld ");
Server. registermbean (New Hello (), helloname );

Objectname adaptername = new objectname ("helloagent: Name = htmladapter, Port = 8082 ");
Htmladaptorserver adapter = new htmladaptorserver ();
Server. registermbean (adapter, adaptername );

Adapter. Start ();
System. Out. println ("start .....");

}
}
Note:
* A mbeanserver is created first for mbean containers.
* Inject the Hello class into mbeanserver. Create an objectname class for injection.
* Create an adaptorserver. This class determines the mbean management interface. Here we use the most common HTML interface. Adaptorserver is also an mbean.
* The Name Of chengang: Name = helloworld has certain rules. The format is "Domain Name: Name = mbean name". Both the domain name and mbean name can be obtained at will.

4. Run helloagent, open the webpage: http: // localhost: 8082/, and click "name = helloworld ".

V. Summary
In the actual system, we can change the name to the variable that determines the database connection pool, so that I can monitor and configure (manage) the running parameters of the system ). You can also remotely call some methods (such as printhello ).

Keywords: basic concepts of JMX
I. Application Scope of JMX
JMX can be used to manage resources such as networks, devices, and applications. The current specification is version 1.1.

Ii. Advantages of JMX
1. The program can be easily managed
2. provides a highly scalable architecture
Every JMX agent service can be easily put into the agent. Each JMX implementation provides several core agent services. You can also write services by yourself, which can be easily deployed, cancel deployment.
3. Integrate existing management solutions, such as SNMP
4. It is very easy to use other Java Technologies
5. provide excuses to allow different implementations

The following figure shows the structure of JMX. Through this figure, the levels of JMX can be clearer.

III. Basic Concepts
1: mbean
It is usually a Java class that provides interfaces to make this class have management functions (such as standard mbean, methods defined in the interface make mbean have management functions ).
2: mbean Server
Mbean is a Java class for mbean management. You need to register an mbean with mbean server before mbean can be managed. mbean server also provides the query function and listener registration function, sun only provides interfaces, and mbean Server implementations in different JMX implementations are also different.
3: JMX agent
The agent provides a series of services to manage a series of mbeans, as shown in. Generally, there are mbean relationships, dynamically loading classes, simple monitoring services, and timers. The agent can use protocol adapters (such as HTTP and SNMP) and connectors (RMI and Jini) to allow different clients to access mbean.
4: Protocol adapters and connectors
The adapter and connector allow different protocols and clients to use this agent. One agent can have multiple Protocol adapters and connectors, in this way, mbean management is more convenient (mbean can be operated by multiple types of clients and protocols ). Note that Protocol adapters and connectors are also mbeans.

Iv. mbean type
Jmx1.1 has greatly improved compared with 1.0. In jmx1.1, four types of mbeans are provided, and three or four types of mbeans are special types of dynamic mbeans.
1: Standard mbean
2: Dynamic mbean
3: Open mbean
4: Model mbean

Type description
Standard mbean is the simplest type of mbean. The resources (including attributes, methods, and time) that can be managed by standard mbean must be defined in the interface, and then mbean must implement this interface. Its name must also follow certain specifications. For example, if the previous mbean is hello, the interface must be hellombean.
Dynamic mbean must implement the javax. Management. dynamicmbean interface. All attributes and methods are defined at runtime.
Open mbean the mbean specification is not complete yet and is being improved
Compared with standard and dynamic mbeans, you do not need to write mbean classes. You only need to use javax. Management. modelmbean. requiredmodelmbean. Requiredmodelmbean implements the modelmbean interface, while modelmbean extends the dynamicmbean interface. Therefore, similar to dynamicmbean, model mbean management resources are also defined at runtime. Unlike dynamicmbean, resources managed by dynamicmbean are generally defined in dynamicmbean (which is determined to be managed during runtime), while resources managed by model mbean are not in mbean, instead, it is external (usually a class) and can be added to model mbean through the set method only at runtime. The examples below will be detailed.

Dynamic mbean defines its attributes and methods at runtime, that is, its attributes and methods can be changed dynamically. Dynamic mbeans mainly use helper classes (constructor class mbeanconstructorinfo, attribute class mbeanattributeinfo, and method class mbeanoperationinfo) to complete this function. All dynamic mbeans must implement the dynamicmbean interface. After dynamicmbean is written, it is used in the same way as the common mbean in the first article.
 
A dynamic mbean instance is provided. This instance initially dynamically constructs a name attribute and a print method. After we execute the print method, a print1 method is added to this mbean. The instance code is as follows:
Ii. Instances
Note:
Implemented in the dynamicmbean Interface
Construct a class by using various helper classes. Constructor mbeanconstructorinfo, attribute mbeanattributeinfo, and method mbeanoperationinfo
All public methods are implemented in dynamicmbean. Mainly provides: setattribute setting attributes, getattribute obtaining attributes, setattributes setting a set of attributes, getattributes obtaining a set of attributes, invoke method calling, and getmbeaninfo mbeanserver obtaining key mbean class construction information through this method.
3. Run
 
Run helloagent first. Open the browser and enter the URL: http: // localhost: 8082 /. Click "name = hellodynamic", execute the print method, and return to the previous page. You will find another print1 method.
 
4. Summary
The Code of dynamic mbean is a little complex, but it shows powerful power for some special requirements. It is also the basis of model mbeans. However, in general projects, dynamic mbeans are still rarely used. The so-called powerful weapon is hidden rather than used.

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.