| 
			
	
	
		    这篇写一个简单的HelloWorld例子。首先准备环境,我使用的JDK1.6,1.5应该也可以。还需要去oracle下载JMX RI包,地址为:http://www.oracle.com/technetwork/java/javase/tech/download-jsp-141676.html,下载“JMX 1.2 Reference Implementation”,解压以后lib目录下有两个jar包,把jmxtool.jar加到CLASSPATH中就可以了,它里面有一个接下去要用到的Html适配器。
 1、首先写一个HelloWorld MBean,它由一个接口和一个实现类组成,代码如下:
 
   public interface HelloWorldMBean  { 
  public void setGreeting(String greeting); 
  public String getGreeting(); 
  public void printGreeting(); 
  }写实现类HelloWorld:
 
   public class HelloWorld 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; 
  } 
  
   public String getGreeting()  { 
  return greeting; 
  } 
  
   public void printGreeting()  { 
  System.out.println(greeting); 
  } 
  
  }这样,一个HelloWorld的MBean就完成了,这是一个标准MBean。必须把MBean注册到Agent才能使用,接下来写一个Agent。
 
 2、定义JMX Agent:HelloAgent,他有三个任务:
 1)、创建MBean Server实例。
 2)、创建HTML适配器和HTML客户端连接。
 3)、注册一个新的HelloWorld的MBean实例。
 代码如下:
 
   public class HelloAgent  { 
  
  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"); 
  mbs.registerMBean(hw, helloWorldName); 
  adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092"); 
  adapter.setPort(9092); 
  mbs.registerMBean(adapter, adapterName); 
  adapter.start(); 
   } catch (Exception e)  { 
  e.printStackTrace(); 
  } 
  } 
  
   public static void main(String args[])  { 
  System.out.println("HelloAgent is running"); 
  HelloAgent agent = new HelloAgent(); 
  } 
  }这段代码首先创建一个MBean Server实例和一个Html适配器实例,MBean Server使用工厂类创建,创建的时候传入字符串作为MBean Server的域名,域名是区别MBean Server的标识。
 接下来实例化HelloWorld MBean,并在MBean Server中注册。注册的时候使用一个ObjectName实例,ObjectName类在JMX中为MBean提供了一套命名系统,是注册在MBean Server中的唯一标识。它有两部分组成:
 1)、域名:这个域名通常和MBean Server的域名一致,如果不一致,则意味着与其他MBean隔离。
 2)、零个或多个key=value串,中间用逗号隔开,这个串用来区别MBean,也可以为MBean提供信息。
 下一步是注册Html适配器,Html适配器也是一个MBean,并启动适配器。
 
 以上两步就是我们例子的代码,基本结构图如下:
 
  
 
 3、运行例子。HelloAgent类有main方法,直接运行就可以了,如果成功就会出现“HelloAgent is running”。然后打开浏览器,输入:http://localhost:9092/,因为代码中Html的适配器端口设置为9092。
 
 以上3步完成了整个HelloWorld例子,通过浏览器提供了3种页面:
 1、Agent页面,也就是第一个看到的页面,上面Agent内包含的MBean的一个总览,它显示了所有的注册在里面的MBean,通过注册时候使用的ObjectName实例来显示。可以通过最上面的文本框来过滤需要显示的MBean。
 2、MBean页面,点击Agent页面中的某个MBean可以进入该MBean页面。我们点击第一个name=helloWorld1的MBean,显示有一下几个信息:
 a)、MBean注册时提供的ObjectName,HelloAgent:name=helloWorld1
 b)、类的名字,在这个例子中是HelloWorld。
 c)、描述,对于Stand MBean,描述有MBean Server创建。
 d)、属性列表,MBean暴露的属性列表,这个MBean有一个属性Greeting是个可读写属性(RW,因为有getter和setter),你可以在Value列的文本框中输入字符串,点击Apply,就动态设置了Greeting的值。
 e)、暴露的操作列表,这个MBean有一个操作printGreeting,点击printGreeting按钮可以调用该操作,会显示”printGreeting Successful“的信息,在控制台可以看到打印出了你刚才输入的Greeting属性的值。
 f)、Reload Period,指的是MBean Server是否要重新实例化这个MBean,如果是,多久一次。
 g)、Unregister按钮,反注册这个MBean。
 本例中还有一个MBean,就是Html适配器,因为它也在Agent注册了成为一个MBean.
 3、Admin页面,点击Agent页面的Admin按钮就进入了Admin页面。通过这个页面可以增加或者删除MBean,页面上有4个文本框分别如下:
 a)、Domain-显示了当前Agent的Domain,
 b)、Keys,也就是ObjectName类的属性串。
 c)、Java Class,想创建的MBean的完整类名。
 d)、Class Loader,这个是可选的,其他都是必须的。
 在Action选项框下面有一个Constructors选项,如果选了这个,点击Send Request,就会显示MBean的所有构造器,可以使用其中的一个来创建MBean实例了。
 
 MBean通知
 加入通知代码到HelloWorld的MBean,JMX提供了两种方法使MBean可以作为其他MBean的监听对象。第一种是实现javax.management.NotificationBroadcaster接口,第二种时继承javax.management.NotificationBroadcasterSupport类。
 实现接口的好处是你还可以继承其他类,继承类的好处是你不需要写实现接口的方法代码。我们选择使用继承类的方法,HelloWorld类代码修改成如下:
 
   public class HelloWorld extends NotificationBroadcasterSupport implements HelloWorldMBean  { 
   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("jmxtest.ch02.helloWorld.test", this, -1, 
  System.currentTimeMillis(), greeting); 
  sendNotification(notification); 
  } 
  
   public String getGreeting()  { 
  return greeting; 
  } 
  
   public void printGreeting()  { 
  System.out.println(greeting); 
  } 
  
  private String greeting; 
  }在setGreeting方法中增加了一些代码,首先实例化一个通知,然后发送这个通知,也就是说在greeting属性设置的时候,发送通知到对次事件感兴趣的对象。实例化通话需要5个参数,第一个是这个通知的标识符,第二个是参数是通知源,也就是产生通知的MBean,第三个是个序列号,第四个是发送时间,第五个是发送的消息字符串。
 
 修改HelloAgent,代码如下:
 
   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  { 
  adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092"); 
  mbs.registerMBean(adapter, adapterName); 
  adapter.setPort(9092); 
  adapter.start(); 
  helloWorldName = new ObjectName("HelloAgent:name=helloWorld1"); 
  mbs.registerMBean(hw, helloWorldName); 
  hw.addNotificationListener(this, null, null); 
  
   } catch (Exception e)  { 
  e.printStackTrace(); 
  } 
  } 
  
   public void handleNotification(Notification notif, Object handback)  { 
  System.out.println("Receiving notification  "); 
  System.out.println(notif.getType()); 
  System.out.println(notif.getMessage()); 
  } 
  
   public static void main(String args[])  { 
  System.out.println("HelloAgent is running"); 
  HelloAgent agent = new HelloAgent(); 
  } 
  }在构造器中增加了“hw.addNotificationListener(this, null, null);”这行代码,它就是把HelloAgent加入到HelloWorld MBean的监听列表中。还增加了一个方法handleNotification,这个是实现了NotificationListener接口的方法,当HelloWorld Mbean的greeting属性被设置的时候,就会调用这个方法,其实是很简单的一个观察者模式。
 运行这个例子,修改greeting属性,点击Apply,看到控制台打印了handleNotification方法中的一些信息。
 
   |