永无止境的程序

..::[......]::..

导航

[转]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 javax.management.
*;

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("jmxbook.ch2.helloWorld.test"this-1, System.currentTimeMillis(), greeting);
        sendNotification(notification);
    }


    
public String getGreeting() {
        
return greeting;
    }


    
public void printGreeting() {
        System.
out.println(greeting);
    }


    
private String greeting;
}



上面的代码仅仅用了一个最简单的通知类:Notification

public Notification(java.lang.String type, java.lang.Object source,
long sequenceNumber,long timeStamp, 
java.lang.String message)


type用来标示通知, source为产生通知的MBean,sequenceNumber为一系列通知中的序号,timeStamp为通知创建的时间,message为具体的通知消息。
由于从javax.management.NotificationBroadcasterSupport继承,所以编写起来容易些。

下面看agent类

import javax.management.*;
import com.sun.jdmk.comm.HtmlAdaptorServer;

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.start();

            helloWorldName 
= new ObjectName("HelloAgent:name=helloWorld1");
            mbs.registerMBean(hw, helloWorldName);

            hw.addNotificationListener(
thisnullnull);

        }
 catch (Exception e) {
            e.printStackTrace();
        }


    }
//constructor

    
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[]) {
        HelloAgent agent 
= new HelloAgent();
    }


}


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

http://www.cnblogs.com/allenny/articles/179208.html

posted on 2005-06-22 17:26  AlleNny  阅读(658)  评论(1编辑  收藏  举报