JMX
来自ITwiki,开放的信息技术大百科
JMX是一种应用编程接口,可扩充对象和方法的集合体,可以用于跨越一系列不同的异构操作系统平台、系统体系结构和网络传输协议,灵活的开发无缝集成的系统、网络和服务管理应用它提供了用户界面指导、Java类和开发集成系统、网络及网络管理应用的规范。 JMX的作用是管理应用程序中所有软件或者硬件资源,由第三方实现的可视化管理操作平台对其所属的MBEAN进行管理。它分为服务层,代理层和MBEAN层,MBEAN即代表了所有系统中的资源。 一个静态MBEAN和代理示例: CLASS HelloWorldMBean: public interface HelloWorldMBean {
public void setGreeting(String greeting);
public String getGreeting();
public void printGreeting();
}
CLASS HelloWorld: import ...; public class HelloWorld extends NotificationBroadcasterSupport implements HelloWorldMBean {
private String greeting = null;
public HelloWorld()
{
this.greeting = "Hello World! I am a Standard MBean";
}
public HelloWorld(String greeting)
{
this.greeting = greeting;
}
public void setGreeting(String greeting)
{
this.greeting = greeting;
Notification notification = new Notification("jmxbook.ch2.helloWorld.test", this, -1, System
.currentTimeMillis(), greeting);
sendNotification( notification );
}
public String getGreeting()
{
return greeting;
}
public void printGreeting()
{
System.out.println(greeting);
}
} 一个MBEAN代理,通过代理可以发布或回收MBEAN,此处将其发布并注册了一个HTML适配器,HTML适配器作为MBEAN可视化管理平台。 CLASS HelloAgent: import ...; public class HelloAgent implements NotificationListener {
private MBeanServer mbs = null;
public HelloAgent()
{
mbs = MBeanServerFactory.createMBeanServer( "HelloAgent" );
HtmlAdaptorServer adapter = new HtmlAdaptorServer();
HelloWorld hw = new HelloWorld();
ObjectName adapterName = null;
ObjectName helloWorldName = null;
try
{
helloWorldName = new ObjectName("HelloAgent:name=helloWorld1");//key=property
mbs.registerMBean(hw, helloWorldName);//注册HELLOWORLD
adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092");
adapter.setPort(9092);
mbs.registerMBean(adapter, adapterName);//注册HTML ADAPTER
adapter.start();
hw.addNotificationListener( this, null, null );
}
catch (Exception e)
{
e.printStackTrace();
}
}
public static void main(String args[])
{
System.out.println("HelloAgent is running");
HelloAgent agent = new HelloAgent();
}
/**
* 监听方法
*
* @param notification
* @param handback
* @see:
*/
public void handleNotification(Notification notification, Object handback)
{
System.out.println( "Receiving notification..." );
System.out.println( notification.getType() );
System.out.println( notification.getMessage() );
}
}//class




