JMX代码入门(二)

来源:互联网 发布:网络信息平台是什么 编辑:程序博客网 时间:2024/06/05 17:50
package com.fanshadoop;import javax.management.Notification;import javax.management.NotificationBroadcasterSupport;/** * JMX notification是用来从MBean和agent发送信息到其他对象(已经注册接受信息)的Java对象。 * 对接收事件感兴趣的是notification的监听者,他们实现javax.management.NotificationListener接口。   JMX事件可以是任何事情:从MBean属性变更到MBean Server上新的MBean注册。 *  *JMX支持两中机制:实现接口javax.management.NotificationBroadcaster *                继承javax.management.NotificationBroadcasterSupport *NotificationBroadcasterSupport给MBean提供了方法允许其他对象注册作为Notification的监听者                 */public class HelloWorldNotification extends NotificationBroadcasterSupportimplements HelloWorldMBean {private String greeting;public HelloWorldNotification() {this.greeting = "Hello World! I am a Standard MBean";}public HelloWorldNotification(String greeting) {this.greeting = greeting;}@Overridepublic void setGreeting(String greeting) {this.greeting = greeting;/* * Notification构造器参数 * type:"."分割的值,标识notification * source:生成notification的MBean * sequenceNumber:notification的序列号 * timestamp:时间戳 * message:消息 */Notification notification = new Notification("jmxbook.ch2.helloWorld.test", this, -1,System.currentTimeMillis(), greeting);/* * sendNotification()是的NotificationBroadcasterSupport的方法, * 将notification发送所有监听者 */sendNotification(notification);}@Overridepublic String getGreeting() {return greeting;}@Overridepublic void printGreeting() {System.out.println( greeting );}}

public interface NotificationBroadcaster{   public void addNotificationListener(       NotificationListener listener,       NotificationFilter filter,       Object handback )throws IllegalArgumentException;   public MBeanNotificationInfo[] getNotificationInfo();   public void removeNotificationListener(      NotificationListener listener )      throws ListenerNotFoundException;}
    NotificationBroadcasterSupport类实现了NotificationBroadcaster接口,MBean实现此接口,提供给其他对象注册监听器的机制,主要通过addNotificationListener()方法。
    NotificationFilter参数是可选的,它允许MBean过滤哪些notification发送到监听器。

package com.fanshadoop.notification;import javax.management.MBeanServer;import javax.management.MBeanServerFactory;import javax.management.Notification;import javax.management.NotificationListener;import javax.management.ObjectName;import com.sun.jdmk.comm.HtmlAdaptorServer;public class HelloAgentWithListener implements NotificationListener {@Overridepublic void handleNotification(Notification notification, Object handback) {System.out.println( "Receiving notification..." );System.out.println( notification.getType() );System.out.println( notification.getMessage() );}private MBeanServer mbs = null;public HelloAgentWithListener() {/* * MBean server是一个用来包含和管理JMX MBean的Java对象, * MBean server是一个标准的JMX类,它是JMX agents的核心。 * MBeanServerFactory可以管理多个MBeanSever实例 * 这里createMBeanServer方法的参数为一组MBean的domain,domain唯一区分其他的MBean server * 如果HelloAgent已经存在,则会已经创建的MBeansever */mbs = MBeanServerFactory.createMBeanServer("HelloAgent");/* *agent通过构造协议适配器和连接器,向管理application开放MBean  */HtmlAdaptorServer adapter = new HtmlAdaptorServer();HelloWorld hw = new HelloWorld();/* * 在MBean上注册监听器 */hw.addNotificationListener(this, null, null);/* * ObjectName类为MBean提供了一个命名空间,它由两部分组成 * 1)domain name(与MBean server的domain一致) * 2)key=value属性列表,用来标识MBean,为MBean提供信息 * 可以提供诸如name,port,location和purpose等属性,属性以逗号分割,且key=value属性列表是唯一的 */ObjectName adapterName = null;ObjectName helloWorldName = null;try {helloWorldName = new ObjectName("HelloAgent:name=helloWorld");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");HelloAgentWithListener agent = new HelloAgentWithListener();}}

原创粉丝点击