JMX简单实例

1.建立MBean

  1. package com.lht.jmx;   
  2.   
  3. /* HelloMBean.java - MBean interface describing the management  
  4. operations and attributes for the Hello World MBean.  In this case  
  5. there are two operations, "sayHello" and "add", and two attributes,  
  6. "Name" and "CacheSize". */  
  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. }   
  23.   

2.建立实现MBean的类
  1. package com.lht.jmx;   
  2.   
  3. public class Hello implements HelloMBean {   
  4.     public void sayHello() {   
  5.     System.out.println("hello, world");   
  6.     }   
  7.   
  8.     public int add(int x, int y) {   
  9.     return x + y;   
  10.     }   
  11.   
  12.     /* Getter for the Name attribute.  The pattern shown here is  
  13.        frequent: the getter returns a private field representing the  
  14.        attribute value.  In our case, the attribute value never  
  15.        changes, but for other attributes it might change as the  
  16.        application runs.  Consider an attribute representing  
  17.        statistics such as uptime or memory usage, for example.  Being  
  18.        read-only just means that it can't be changed through the  
  19.        management interface.  */  
  20.     public String getName() {   
  21.     return this.name;   
  22.     }   
  23.   
  24.     /* Getter for the CacheSize attribute.  The pattern shown here is  
  25.        frequent: the getter returns a private field representing the  
  26.        attribute value, and the setter changes that field.  */  
  27.     public int getCacheSize() {   
  28.     return this.cacheSize;   
  29.     }   
  30.   
  31.     /* Setter for the CacheSize attribute.  To avoid problems with  
  32.        stale values in multithreaded situations, it is a good idea  
  33.        for setters to be synchronized.  */  
  34.     public synchronized void setCacheSize(int size) {   
  35.     this.cacheSize = size;   
  36.   
  37.     /* In a real application, changing the attribute would  
  38.        typically have effects beyond just modifying the cacheSize  
  39.        field.  For example, resizing the cache might mean  
  40.        discarding entries or allocating new ones.  The logic for  
  41.        these effects would be here.  */  
  42.     System.out.println("Cache size now " + this.cacheSize);   
  43.     }   
  44.   
  45.     private final String name = "Reginald";   
  46.     private int cacheSize = DEFAULT_CACHE_SIZE;   
  47.     private static final int DEFAULT_CACHE_SIZE = 200;   
  48. }   
  49.   

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


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

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值