JMX代码入门(二)

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 NotificationBroadcasterSupport
		implements HelloWorldMBean {
	private String greeting;
	public HelloWorldNotification() {
		this.greeting = "Hello World! I am a Standard MBean";
	}
	public HelloWorldNotification(String greeting) {
		this.greeting = greeting;
	}
	@Override
	public 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);
	}
	@Override
	public String getGreeting() {
		return greeting;
	}
	@Override
	public 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 {	
	@Override
	public 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();
	}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值