JMX(Java Management Extensions,Java管理扩展)
JMX官网
概念性的东西在官网上有解释,这里就不多解释,直奔主题.
先写一个简单的测试类,MyTest.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| public class MyTest implements MyTestMBean { private String name = "world"; @Override public String getName() { return this.name; } @Override public void setName(String name) { this.name = name; } @Override public String sayHello(String name) { return "hello," + name; } @Override public void changeName(String name) { setName(name); } }
|
把想暴露的方法封装成一个接口
注意:
- 接口的名称必须是类名+MBean,这里就是MyTestMBean.java
- 修饰符必须是public,interface的默认public abstract 是不行的.
1 2 3 4 5 6
| public interface MyTestMBean { String getName(); void setName(String name); String sayHello(String name); void changeName(String name); }
|
写个测试类试试:Main.java
1 2 3 4 5 6 7 8 9 10 11 12 13
| public class Main { public static void main(String args[]) throws Exception { MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("name.chengchao:type=MyTest"); MyTest mbean = new MyTest(); mBeanServer.registerMBean(mbean, name); System.out.println("started..."); Thread.sleep(Long.MAX_VALUE); }
}
|
打开jconsole
可以看到:yTest下面有"属性"(pojo)和"操作"
点点看,还能执行方法,是不是很神奇啊.到这里一个mbean的helloworld就完成了.