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();}}
- JMX代码入门(二)
- JMX 入门(二)
- JMX代码入门(一)
- JMX入门
- JMX入门
- jmx入门
- JMX入门
- JMX入门
- JMX入门
- JMX入门
- JMX入门
- JMX 入门
- JMX 入门
- JMX-入门
- 【JMX学习】1、jmx入门
- jmx入门例子
- JMX 入门例子
- JMX 入门例子
- 开始学习Python
- “省了14块9毛”
- ios6.0以前版本开发 异常信息:reason: ‘Could not instantiate class named NSLayoutConstraint’
- 《coredump问题原理探究》Linux x86版3.4节栈布局之函数参数
- Android中使Dialog显示时背景不变暗
- JMX代码入门(二)
- android的selector背景选择器
- DES加密算法实现过程
- code4app的cocos2d连连看demo源代码笔记
- 周赛(1)——中国象棋
- cvFindContours && cvDrawContours 的应用2-----图像内轮廓填充
- 水平垂直的 js 插件
- 4Human 代理用法说明
- ARM中常用英文缩写解释