上篇的简介大概介绍了JMX的作用和基本类型和实现方法这里作一个实例进行说明!
1.建立MBean
2.建立实现MBean的类
3.客户端代码
1.建立MBean
- package com.lht.jmx;
- /* HelloMBean.java - MBean interface describing the management
- operations and attributes for the Hello World MBean. In this case
- there are two operations, "sayHello" and "add", and two attributes,
- "Name" and "CacheSize". */
- public interface HelloMBean {
- // operations
- public void sayHello();
- public int add(int x, int y);
- // attributes
- // a read-only attribute called Name of type String
- public String getName();
- // a read-write attribute called CacheSize of type int
- public int getCacheSize();
- public void setCacheSize(int size);
- }
2.建立实现MBean的类
- package com.lht.jmx;
- public class Hello implements HelloMBean {
- public void sayHello() {
- System.out.println("hello, world");
- }
- public int add(int x, int y) {
- return x + y;
- }
- /* Getter for the Name attribute. The pattern shown here is
- frequent: the getter returns a private field representing the
- attribute value. In our case, the attribute value never
- changes, but for other attributes it might change as the
- application runs. Consider an attribute representing
- statistics such as uptime or memory usage, for example. Being
- read-only just means that it can't be changed through the
- management interface. */
- public String getName() {
- return this.name;
- }
- /* Getter for the CacheSize attribute. The pattern shown here is
- frequent: the getter returns a private field representing the
- attribute value, and the setter changes that field. */
- public int getCacheSize() {
- return this.cacheSize;
- }
- /* Setter for the CacheSize attribute. To avoid problems with
- stale values in multithreaded situations, it is a good idea
- for setters to be synchronized. */
- public synchronized void setCacheSize(int size) {
- this.cacheSize = size;
- /* In a real application, changing the attribute would
- typically have effects beyond just modifying the cacheSize
- field. For example, resizing the cache might mean
- discarding entries or allocating new ones. The logic for
- these effects would be here. */
- System.out.println("Cache size now " + this.cacheSize);
- }
- private final String name = "Reginald";
- private int cacheSize = DEFAULT_CACHE_SIZE;
- private static final int DEFAULT_CACHE_SIZE = 200;
- }
3.客户端代码
- package com.lht.jmx;
- import java.lang.management.*;
- import javax.management.*;
- import com.sun.jdmk.comm.HtmlAdaptorServer;
- public class Main {
- public static void main(String[] args) throws Exception {
- MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
- final HtmlAdaptorServer htmlAdaptor = new HtmlAdaptorServer();
- final ObjectName htmlAdaptorON = new ObjectName("com.example.mbeans:name=HtmlAdaptor");
- mbs.registerMBean(htmlAdaptor, htmlAdaptorON);
- htmlAdaptor.setPort(9999);
- System.out.print("Starting the HtmlAdaptor....");
- htmlAdaptor.start();
- }
- }
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