[转]JMX指南(二)
http://www.cnblogs.com/allenny/articles/179163.html
使用通知(notification)
通知其实就是一个监听器,我们可以为MBean注册一个监听器,当满足一定的条件时,MBean会发消息给监听器,然后监听器进行必要的处理。
一个MBean可以通过两种方法实现notification。
1:实现javax.management.NotificationBroadcaster接口
2:扩展javax.management.NotificationBroadcasterSupport类(这个类也实现了上面的接口)
要编写监听器,必须实现javax.management。NotificationListener接口
下面看一个简单的通知的例子,利用方式2实现
MBean的代码:
 import java.io.*;
import java.io.*; import javax.management.*;
import javax.management.*;
 public class HelloWorld extends NotificationBroadcasterSupport implements HelloWorldMBean {
public class HelloWorld extends NotificationBroadcasterSupport implements HelloWorldMBean {
 public HelloWorld() {
    public HelloWorld() { this.greeting = "Hello World! I am a Standard MBean";
        this.greeting = "Hello World! I am a Standard MBean"; }
    }
 public HelloWorld(String greeting) {
    public HelloWorld(String greeting) { this.greeting = greeting;
        this.greeting = greeting; }
    }
 public void setGreeting(String greeting) {
    public void setGreeting(String greeting) { this.greeting = greeting;
        this.greeting = greeting; Notification notification = new Notification("jmxbook.ch2.helloWorld.test", this, -1, System.currentTimeMillis(), greeting);
        Notification notification = new Notification("jmxbook.ch2.helloWorld.test", this, -1, System.currentTimeMillis(), greeting); sendNotification(notification);
        sendNotification(notification); }
    }
 public String getGreeting() {
    public String getGreeting() { return greeting;
        return greeting; }
    }
 public void printGreeting() {
    public void printGreeting() { System.out.println(greeting);
        System.out.println(greeting); }
    }
 private String greeting;
    private String greeting; }
}

上面的代码仅仅用了一个最简单的通知类:Notification
 public Notification(java.lang.String type, java.lang.Object source,
public Notification(java.lang.String type, java.lang.Object source, long sequenceNumber,long timeStamp,
long sequenceNumber,long timeStamp,  java.lang.String message)
java.lang.String message)
type用来标示通知, source为产生通知的MBean,sequenceNumber为一系列通知中的序号,timeStamp为通知创建的时间,message为具体的通知消息。
由于从javax.management.NotificationBroadcasterSupport继承,所以编写起来容易些。
下面看agent类
 import javax.management.*;
import javax.management.*; import com.sun.jdmk.comm.HtmlAdaptorServer;
import com.sun.jdmk.comm.HtmlAdaptorServer;
 public class HelloAgent implements NotificationListener
public class HelloAgent implements NotificationListener
 {
{ private MBeanServer mbs = null;
    private MBeanServer mbs = null;
 public HelloAgent() {
    public HelloAgent() { mbs = MBeanServerFactory.createMBeanServer("HelloAgent");
        mbs = MBeanServerFactory.createMBeanServer("HelloAgent"); HtmlAdaptorServer adapter = new HtmlAdaptorServer();
        HtmlAdaptorServer adapter = new HtmlAdaptorServer(); HelloWorld hw = new HelloWorld();
        HelloWorld hw = new HelloWorld();
 ObjectName adapterName = null;
        ObjectName adapterName = null; ObjectName helloWorldName = null;
        ObjectName helloWorldName = null;
 try {
        try { adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092");
            adapterName = new ObjectName("HelloAgent:name=htmladapter,port=9092"); mbs.registerMBean(adapter, adapterName);
            mbs.registerMBean(adapter, adapterName); adapter.start();
            adapter.start();
 helloWorldName = new ObjectName("HelloAgent:name=helloWorld1");
            helloWorldName = new ObjectName("HelloAgent:name=helloWorld1"); mbs.registerMBean(hw, helloWorldName);
            mbs.registerMBean(hw, helloWorldName);
 hw.addNotificationListener(this, null, null);
            hw.addNotificationListener(this, null, null);
 } catch (Exception e) {
        } catch (Exception e) { e.printStackTrace();
            e.printStackTrace(); }
        }
 }//constructor
    }//constructor
 public void handleNotification(Notification notif, Object handback) {
    public void handleNotification(Notification notif, Object handback) { System.out.println("Receiving notification");
        System.out.println("Receiving notification"); System.out.println(notif.getType());
        System.out.println(notif.getType()); System.out.println(notif.getMessage());
        System.out.println(notif.getMessage()); }
    }
 public static void main(String args[]) {
    public static void main(String args[]) { HelloAgent agent = new HelloAgent();
        HelloAgent agent = new HelloAgent(); }
    }
 }
}

在agent类中
hw.addNotificationListener( this, null, null );将agent作为helloworld MBean的监听器,并且agent类实现了NotificationListener 接口,方法handleNotification处理具体的通知到达的情况。
 
                    
                 


 
     
                
            
         浙公网安备 33010602011771号
浙公网安备 33010602011771号