JMX helloworld instance

Source: Internet
Author: User

Original address: http://qun.51.com/bc123/topic.php? PID = 1, 4525

The example can run

 

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?
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;
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;
The program will write a code segment to cache the configuration value. When the system reads the value, it will first check whether the configuration file is changed. If there is any change, repeat it again. Otherwise, the value will be read from the cache.
A programmer knows 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.
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 shown below:
/*** @ 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:
Create an mbeanserver for mbean container
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 and open the webpage: http: // localhost: 8082/, as shown below:

Click "name = helloworld". The following page appears.

Follow the steps in the red lines below to get the following output in the console (I use eclipse as the console view:

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.

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.