JMX 入门例子

来源:互联网 发布:python flask上传图片 编辑:程序博客网 时间:2024/04/29 10:26

     JMX(Java Management Extensions,即Java管理扩展)是一个为应用程序、设备、系统等植入管理功能的框架。JMX可以跨越一系列异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用。

 

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

1、初级程序员一般是写死在程序里,到要改变时就去改程序,然后再编译发布;

2、程序熟手则一般把这些信息写在一个配置文件里(JAVA一般都是*.properties文件),
    到要改变时只要改配置文件,但还是重新启动系统,以便读取配置文件里的新值;

3、程序好手则会写一个段代码,把配置值缓存起来,系统在读值的时候,先看看配置文件有
    没有更动。如有更改则重读一遍,否则从缓存里读取值

4、程序高手则懂得取物为我所用,用JMX!把配置属性集中在一个类,然后写一个叫MBean
    的东东,再配置一下就轻松搞定了。而且JMX自动提供了一个WEB页面来给你来改变这些
    配置信息。

 

参考Java提供的例子(需要 jdk1.6):

http://download.oracle.com/javase/6/docs/technotes/guides/jmx/examples.html

 

1. 需要被管理类的接口  HelloMBean.java  (规范一般是***MBean.java)

 

Java代码  收藏代码
  1. /* HelloMBean.java - MBean interface describing the management 
  2.    operations and attributes for the Hello World MBean.  In this case 
  3.    there are two operations, "sayHello" and "add", and two attributes, 
  4.    "Name" and "CacheSize". */  
  5.   
  6. package com.example.mbeans;  
  7.   
  8. public interface HelloMBean {  
  9.     // operations  
  10.   
  11.     public void sayHello();  
  12.     public int add(int x, int y);  
  13.   
  14.     // attributes  
  15.   
  16.     // a read-only attribute called Name of type String  
  17.     public String getName();  
  18.   
  19.     // a read-write attribute called CacheSize of type int  
  20.     public int getCacheSize();  
  21.     public void setCacheSize(int size);  
  22. }  

 

2. 需要管理的类  Hello.java  (通常是实现**MBean.java)

Java代码  收藏代码
  1. /* Hello.java - MBean implementation for the Hello World MBean. 
  2.    因篇幅所限,把examples带的注释去掉了,自己可以下载看  */  
  3.   
  4. package com.example.mbeans;  
  5.   
  6. public class Hello implements HelloMBean {  
  7.     public void sayHello() {  
  8.     System.out.println("hello, world");  
  9.     }  
  10.   
  11.     public int add(int x, int y) {  
  12.     return x + y;  
  13.     }  
  14.   
  15.     public String getName() {  
  16.     return this.name;  
  17.     }  
  18.   
  19.      public int getCacheSize() {  
  20.     return this.cacheSize;  
  21.     }  
  22.   
  23.     public synchronized void setCacheSize(int size) {  
  24.     this.cacheSize = size;  
  25.   
  26.     System.out.println("Cache size now " + this.cacheSize);  
  27.     }  
  28.   
  29.     private final String name = "Reginald";  
  30.     private int cacheSize = DEFAULT_CACHE_SIZE;  
  31.     private static final int DEFAULT_CACHE_SIZE = 200;  
  32. }  

 

 

3. 代理/注册类 Main.java

Java代码  收藏代码
  1. package com.example.mbeans;  
  2.   
  3. import java.lang.management.*;  
  4. import javax.management.*;  
  5.   
  6. public class Main {  
  7.     /* For simplicity, we declare "throws Exception".  Real programs 
  8.        will usually want finer-grained exception handling.  */  
  9.     public static void main(String[] args) throws Exception {  
  10.     // Get the Platform MBean Server  
  11.     MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();  
  12.   
  13.     // Construct the ObjectName for the MBean we will register  
  14.     ObjectName name = new ObjectName("com.example.mbeans:type=Hello");  
  15.   
  16.     // Create the Hello World MBean  
  17.     Hello mbean = new Hello();  
  18.   
  19.     // Register the Hello World MBean  
  20.     mbs.registerMBean(mbean, name);  
  21.   
  22.     // Wait forever  
  23.     System.out.println("Waiting forever...");  
  24.     Thread.sleep(Long.MAX_VALUE);  
  25.     }  
  26. }  

 

4. 编译、测试运行上面的程序

 

javac com/example/mbeans/*.java

# 启动程序

java com.example.mbeans.Main

另起一个命令行或者cmd:

运行jconsole  (# JConsole is located in $(J2SE_HOME)/bin/jconsole) 可以看到,选择本地的进程 com.example.mbeans 连接 就可以进行管理了。

 


 

管理界面:

 



 
修改cacheSize 大小,可以在启动Main的命令行窗口看到修改生效。

 


 

其它examples下载:

 

 

 

 

  • jmx_examples.zip (102.9 KB)
  • 下载次数: 308
  • 查看图片附件
0 0
原创粉丝点击