现在的位置: 首页 > 综合 > 正文

jmx通知机制

2013年10月22日 ⁄ 综合 ⁄ 共 1428字 ⁄ 字号 评论关闭

最近写代码用到jmx通知机制,小记一下

JMX notification model分别依靠下列组件来实现三个角色:
A. NotificationBroadcaster接口, 事件广播发出者,该接口允许希望得到通知的监听者注册。
B. Notification, Notification 类表示 MBean 所发出的通知。它包含对源 MBean 的引用,可以被直接使用,也可以根据传递的事件的需要而被扩
展。 
C. NotificationListener接口, 事件监听者或者接受者。

代码中实现的通知机制

广播类

      //广播类 必须实现NotificationBroadcaster接口,但是java提供了一个NotificationBroadcasterSupport类实现了NotificationBroadcaster接口,可以用作发送通知的
MBean 的超类,
并提供一个方法叫sendNotification

//SipTraceMXBean收到跟踪消息,通过此方法将通知消息发送给监听者
private void sendToConsole(TracePacket packet) {
Notification notif = new Notification(cC.getJmxNotifType(), cT, -1);
notif.setUserData(packet.getAttributeMap());
cT.sendNotification(notif);
}

监听者

       

//监听类 必须实现NotificationListener接口

public class ConsoleListener implements NotificationListener, Serializable{

public void handleNotification(Notification notification, Object handback) {}

Object notif = notification.getUserData();

//通知消息数据的处理

。。。。。。。。

}

       

handback -一个不透明对象,用于帮助侦听器关联关于 MBean 发出者的信息。在 addListener 调用期间将此对象传递到 MBean,并在不经修改的情况下重发到侦听器。MBean 对象不应使用或修改该对象。

监听者注册

      

//监听端通过获取到通知端的jmx连接,注册listener

url =new JMXServiceURL((String) c.get("traceserver"));

jmxConnector =JMXConnectorFactory.connect(url);

MBeanServerConnection   mbsc =jmxConnector.getMBeanServerConnection();

ConsoleListener cL = new ConsoleListener();

mbsc.addNotificationListener(objectName, cL, null, null);

void addNotificationListener(ObjectName name,

NotificationListener
 listener,
NotificationFilter
 filter,
Object
 handback) throws
InstanceNotFoundException
,
IOException

抱歉!评论已关闭.