AD

JMX的Hello World

一、JMX简介

  什么是JMX?在一篇网文中是这样说的:"JMX(Java Management Extensions)是一个为应用程序植入管理功能的框架。JMX是一套标准的代理和服务,实际上,用户可以在任何Java应用程序中使用这些代理和服务实现管理",这句话我现在看着还是不知所云,云里雾里。

  我们还是从JMX能给我们提供什么好处入手来理解吧。举一个应用实例:在一个系统中常常会有一些配置信息,比如服务的IP地址,端口号什么的,那么如何来写这些代码呢?

1. 程序初哥一般是写死在程序里,到要改变时就去改程序,然后再编译发布;
2. 程序熟手则一般把这些信息写在一个配置文件里(JAVA一般都是*.properties文件),到要改变时只要改配置文件,但还是重新启动系统,以便读取配置文件里的新值;
3. 程序好手则会写一个段代码,把配置值缓存起来,系统在读值的时候,先看看配置文件有没有更动。如有更改则重读一遍,否则从缓存里读取值
4. 程序高手则懂得取物为我所用,用JMX!把配置属性集中在一个类,然后写一个叫MBean的东东,再配置一下就轻松搞定了。而且JMX自动提供了一个WEB页面来给你来改变这些配置信息。

二、准备工作

  JMX是一份规范,SUN依据这个规范在JDK(1.3、1.4、5.0)提供了JMX接口。而根据这个接口的实现则有很多种,比如Weblogic的JMX实现、MX4J、JBoss的JMX实现。在SUN自己也实现了一份,不过在JDK1.4之前,这件JMX实现(一些JAR包)是可选的,你得去它的网站上下载。JDK5.0则内嵌了进来,安装JDK5.0就可以开发基于JMX的代码了。
  但JDK5.0并非包含所有SUN的关于JMX的代码,有一些工具类是排除在JDK5.0之外的。下面根据所使用的JDK版本情况,谈一谈开发环境的准备。
1、JDK1.3、1.4
  去SUN网站下载SUN的JMX实现,共两个ZIP文件,下载网址:****oracle**/technetwork/java/javasebusiness/downloads/java-archive-downloads-java-plat-419418.html#7657-jmx-1.2.1-oth-JPR
(1)jmx-1_2_1-ri.zip
   解压后的lib目录包含:jmxri.jar、jmxtools.jar
(2)jmx_remote-1_0_1_03-ri.zip
   解压后的lib目录包含:jmxremote.jar、jmxremote_optional.jar、rmissl.jar
  如果在DOS下用命令行开发,则把这五个JAR包加入到classpath系统变量中。如果你用Eclipse开发,则把JAR包加入到项目属性的Libratries(库)引用中。
2、JDK5.0
  JDK5.0的jre\lib\rt.jar已经包含了jmxri.jar、jmxremote.jar、rmissl.jar三个包的代码。如果你用到jmxtools.jar、jmxremote_optional.jar的类,则需要将这两个类加入到classpath或 Eclipse的项目库引用中。
3、我使用的开发环境:JDK5.0 + Eclipse3.2。
  注:因为用到jmxtools.jar中的HtmlAdaptorServer类,所以将此包加入到项目库引用中。jmxremote_optional.jar暂时不用到,不管它。

三、HelloWorld实例

1、Hello是一个需要被管理的类(普通类)

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、要管理Hello则必须创建一个相应MBean,如下:

public interface HelloMBean {
    public String getName();
    public void setName(String name);
    public void printHello();
    public void printHello(String whoName);
}

说明:包含在MBean中方法都将是可以被管理的。MBean起名是有规范的,就是原类名后加上MBean字样。
 
3、创建一个Agent类

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import com.sun.jdmk**m.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(8082);
        server.registerMBean(adapter, adapterName);

        adapter.start();
        System.out.println("start.....");

    }
}

问题: java.lang.NoClassDefFoundError: com/sun/jdmk/trace/Trace

在classpath下添加jmxri.jar

说明

* 先创建了一个MBeanServer,用来做MBean的容器
* 将Hello这个类注入到MBeanServer中,注入需要创建一个ObjectName类
* 创建一个AdaptorServer,这个类将决定MBean的管理界面,这里用最普通的Html型界面。AdaptorServer其实也是一个MBean。
* chengang:name=HelloWorld的名字是有一定规则的,格式为:“域名:name=MBean名称”,域名和MBean名称都可以任意取。

4、运行HelloAgent,然后打开网页:**localhost:8082/

单击“name=HelloWorld”链接进入

JMX的Hello World
JMX的Hello World

四、总结

  在实际系统中我们可以把name变成决定数库链接池的变量,这样我就可以对系统的运行参数进行实现的监控和配置(管理)。而且也可以对一些方法(如printHello)进行远程调用了。

标签: jmx, Java
分类: cloudify
时间: 2013-06-13

相关文章

  1. jmx, tomcat architecture foundation

    tomcat is very familiar with it? everyday use ~ ~ Few dare to look down on so consider themselves, and We can ...
  2. JBoss5 JMX - See JBoss thread stacks, CPU, memory, information

    JBoss has a JMX management, web management interface called the jmx-console, in the http://localhost:8080/jmx- ...
  3. JMX Monitoring WEBLOGIC7.0

    JMX access WEBLOGIC7.0 Development environment as well as the reference packet JDK version of the options: As ...
  4. JMX Monitoring WEBLOGIC9.2

    JMX access WEBLOGIC9.2 Development environment as well as the reference packet weblogic9.0 running on JDK5.0 o ...
  5. Achieve Log4J JMX runtime level changes

    First to introduce the MBean interface exposed, mainly based filter be logger, set the logger object, dynamic ...
  6. Connected glassfish server through JMX

    HashMap env = new HashMap (1); final String [] credentials = new String [] ("admin", "adminadmi ...
  7. Making a JMX connection with a timeout

    Reprinted from: http://weblogs.java.net/blog/2007/05/23/making-jmx-connection-timeout One question I encounter ...
  8. Remote JMX Exceptions

    Reprinted from: http://marxsoftware.blogspot.com/2008/06/remote-jmx-exceptions.html (required fanqiang) Perhap ...
  9. JMX learning experience

    JMX (Java Management Extensions) Chinese translation of the JAVA Management Extension, jmx in J2SE 5.0 is adde ...
  10. JMX client and server (Spring configuration approach and encoding method)

    1, Spring configuration mode: 1, the client: client.xml <?xml version="1.0" encoding="UTF-8& ...
  11. [Inquiry JMX] 3. To achieve through the RMI server and client communications

    I. Introduction The one presented through the Web form to access and control MBean, this section should be int ...
  12. [Inquiry JMX] 4.Spring2.x integrated MX4J

    I. Introduction Spring's JMX support provides a number of features, so you can transparently integrate Spring ...
  13. tomcat 7 source of -6 server initialization of the JMX (DynamicMBean) continued

    Start with JMX, The JMX technology provides a simple, standard way of managing resources such as applications, ...
  14. JMX in spring configuration

    JConsole in connection: a simple localhost: 1099 or service: jmx: rmi: / / / jndi / rmi: / / localhost: 1099/j ...
  15. Control multiple servers with jmx (tomcat)

    Because of the need to write a background monitoring server program, relating to jmx, also related to the dist ...
  16. Transferred the contents of JMX practice

    JMX, is the SUN companies to structure a management system, provides a standard, design patterns and interface ...
  17. JMX in Action Learning Log

    JMX is the JMX IN ACTION study the book in the book of the API RMI code is obsolete, Write some new code JMXBo ...
  18. Jar download the required package of jmx

    http://java.sun.com/javase/technologies/core/mntr-mgmt/javamanagement/download.jsp JMX 1.2.1 Reference Impleme ...
  19. WAS application package to deploy Jmx the process of conflict resolution

    WAS application package to deploy Jmx the process of conflict resolution Recently, the was6.0 above a JMX-base ...
  20. JMX obtained by the number of full GC?

    Today a colleague asked how to pass the JMX access to a Java process, the number of full GC: Quote hi, ask a q ...