天天看点

JMX简单实例

1.建立MBean

  1. package com.lht.jmx;   
  2. public interface HelloMBean {   
  3.  // operations   
  4.  public void sayHello();   
  5.  public int add(int x, int y);   
  6.  // attributes   
  7.  // a read-only attribute called Name of type String   
  8.  public String getName();   
  9.  // a read-write attribute called CacheSize of type int   
  10.  public int getCacheSize();   
  11.  public void setCacheSize(int size);   
  12. }   

2.建立实现MBean的类

  1. package com.lht.jmx;   
  2. public class Hello implements HelloMBean {   
  3.     public void sayHello() {   
  4.     System.out.println("hello, world");   
  5.     }   
  6.     public int add(int x, int y) {   
  7.     return x + y;   
  8.     }   
  9.     public String getName() {   
  10.     return this.name;   
  11.     }   
  12.     public int getCacheSize() {   
  13.     return this.cacheSize;   
  14.     }   
  15.     public synchronized void setCacheSize(int size) {   
  16.     this.cacheSize = size;   
  17.     System.out.println("Cache size now " + this.cacheSize);   
  18.     }   
  19.     private final String name = "Reginald";   
  20.     private int cacheSize = DEFAULT_CACHE_SIZE;   
  21.     private static final int DEFAULT_CACHE_SIZE = 200;   
  22. }   

3.客户端代码

  1. package com.lht.jmx;   
  2. import java.lang.management.*;   
  3. import javax.management.*;   
  4. import com.sun.jdmk.comm.HtmlAdaptorServer;   
  5. public class Main {   
  6.     public static void main(String[] args) throws Exception {   
  7.          MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();   
  8.          final HtmlAdaptorServer htmlAdaptor = new HtmlAdaptorServer();    
  9.          final ObjectName htmlAdaptorON = new ObjectName("com.example.mbeans:name=HtmlAdaptor");    
  10.          mbs.registerMBean(htmlAdaptor, htmlAdaptorON);    
  11.          htmlAdaptor.setPort(9999);    
  12.         System.out.print("Starting the HtmlAdaptor....");    
  13.         htmlAdaptor.start();   
  14.     }   
  15. }   

4.运行上面的java代码

控制台信息:Starting the HtmlAdaptor....

5.在浏览器中输入

http://localhost:9999/

这时候你就可以看见一个打开的网页,现在你就可以利用这个网页来进行MBean的管理了!

更详细信息:http://www.itisedu.com/phrase/200604261751455.html

                     http://dev2dev.bea.com.cn/techdoc/20005040805.html