0
点赞
收藏
分享

微信扫一扫

JMX简单实例


1.建立MBean


  1. package
  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
  9. // operations
  10.   
  11. public void
  12. public int add(int x, int
  13.   
  14. // attributes
  15.   
  16. // a read-only attribute called Name of type String
  17. public
  18.   
  19. // a read-write attribute called CacheSize of type int
  20. public int
  21. public void setCacheSize(int
  22. }   
  23.   



2.建立实现MBean的类


  1. package
  2.   
  3. public class Hello implements
  4. public void
  5. "hello, world");   
  6.     }   
  7.   
  8. public int add(int x, int
  9. return
  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
  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
  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
  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. "Cache size now " + this.cacheSize);   
  43.     }   
  44.   
  45. private final String name = "Reginald";   
  46. private int
  47. private static final int DEFAULT_CACHE_SIZE = 200;   
  48. }   
  49.   



3.客户端代码


  1. package
  2. import
  3. import
  4.   
  5. import
  6.   
  7. public class
  8.     
  9. public static void main(String[] args) throws
  10.        
  11.          MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();   
  12. final HtmlAdaptorServer htmlAdaptor = new
  13. final ObjectName htmlAdaptorON = new ObjectName("com.example.mbeans:name=HtmlAdaptor");    
  14.          mbs.registerMBean(htmlAdaptor, htmlAdaptorON);    
  15. 9999);    
  16.             
  17. "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


 

举报

相关推荐

0 条评论