http://www.blogjava.net/hengheng123456789/articles/66095.htmlhtml
Mbean之間的通訊是必不可少的,Notification就起到了在Mbean之間溝通橋樑的做用。JMX notification 由四部分組成:
* Notification 這個至關於一個信息包,封裝了須要傳遞的信息
* Notification broadcaster 這至關於一個廣播器,把消息廣播出去
* Notification listerner 這是一個監聽器,用於監聽廣播出來的Notification消息
* Notification filter 這是一個過濾器,過濾掉不須要的Notification消息
Notification broadcaster不須要咱們實現,JMX的內部已經有了。Notification filter通常也不多用。下面的例子主要用到了Notification、Notification listerner。java
public interface JackMBean { void hi(); } /** * 必需繼承NotificationBroadcasterSupport * 此類只有一個hi方法,方法只有兩句:建立一個Notification消息包,而後將包發出去 * 若是你還要在消息包上附加其餘數據,Notification還有一個setUserData方法可供使用 */ public class Jack extends NotificationBroadcasterSupport implements JackMBean { private int seq = 0; @Override public void hi() { Notification n = new Notification(//建立一個信息包 "jack.hi",//給這個Notification起個名稱 this, //由誰發出的Notification ++seq,//一系列通知中的序列號,能夠設任意數值 System.currentTimeMillis(),//發出時間 "Jack");//發出的消息文本 //發出去 sendNotification(n); } } public class HelloListener implements NotificationListener { public void handleNotification(Notification n, Object handback) { System.out.println("type=" + n.getType()); System.out.println("source=" + n.getSource()); System.out.println("seq=" + n.getSequenceNumber()); System.out.println("send time=" + n.getTimeStamp()); System.out.println("message=" + n.getMessage()); if (handback != null) { if (handback instanceof HelloWorld) { HelloWorld hello = (HelloWorld) handback; hello.printHello(n.getMessage()); } } } } public class HelloNotifyAgent { public static void main (String args[]) throws Exception{ MBeanServer server = ManagementFactory.getPlatformMBeanServer(); ObjectName helloName = new ObjectName("HelloAgent:name=HelloWorld"); HelloWorld hello=new HelloWorld(); server.registerMBean(hello, helloName); ObjectName adapterName = new ObjectName("HelloAgent:name=htmlAdapter"); HtmlAdaptorServer adapter = new HtmlAdaptorServer(); server.registerMBean(adapter, adapterName); Jack jack = new Jack(); server.registerMBean(jack, new ObjectName("HelloAgent:name=jack")); jack.addNotificationListener(new HelloListener(), null, hello); adapter.start(); System.out.println("start....."); } }
Notification和Java的事件模型是同樣的,另外若是你買了《Eclipse從入門到精通》,你會發現第22.4節也使用了和Notification和Java的事件模型相同的設計方式。Notification在咱們的實際項目中也用到了,象咱們如今的給移動作的項目中(基於JMX實現),分散在各地方的工做站的日誌,就是經過Notification方式,把每條產生的日誌封裝在 Notification中實時發回主控服務器的。有機會我會發這一系統的關於日誌的設計方案寫一下,它實現了對各地工做站的集中的、實時的監控,很是實用。服務器
another exampleide
public interface HelloMBean { void sayHello(); int add(int x, int y); String getName(); int getCacheSize(); void setCacheSize(int size); } /** * 要生成通知,MBean必須實現NotificationEmitter接口或 * 繼承NotificationBroadcasterSupport類。 * 要發送一個通知必須構造Notification類的實例 * 或者子類(如NotificationChangedAttribute),通知實例產生後, * 由NotificationBroadcasterSupport的sendNotification方法發送通知。 */ public class Hello extends NotificationBroadcasterSupport implements HelloMBean { private final String name = "Reginald"; private int cacheSize = DEFAULT_CACHE_SIZE; private static final int DEFAULT_CACHE_SIZE = 200; private long sequenceNumber = 1; public void sayHello() { System.out.println("hello, world"); } public int add(int x, int y) { return x + y; } public String getName() { return this.name; } public int getCacheSize() { return this.cacheSize; } public synchronized void setCacheSize(int size) { int oldSize = this.cacheSize; this.cacheSize = size; System.out.println("Cache size now " + this.cacheSize); /** * To send a notification,you need to construct an instance of class * Notification or a Subclass(such as AttributeChangedNotification),and * pass the instance to NotificationBroadcastSupport.sendNotification. * */ Notification n = new AttributeChangeNotification(this, sequenceNumber++, System.currentTimeMillis(), "CacheSize changed", "CacheSize", "int", oldSize, this.cacheSize); sendNotification(n); } @Override public MBeanNotificationInfo[] getNotificationInfo() { String[] types = new String[] { AttributeChangeNotification.ATTRIBUTE_CHANGE }; String name = AttributeChangeNotification.class.getName(); String description = "An attribute of this MBean has changed"; MBeanNotificationInfo info = new MBeanNotificationInfo(types, name, description); return new MBeanNotificationInfo[] { info }; } } public class MBeanRun { public static void main(String[] args) throws Exception{ MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); ObjectName name = new ObjectName("com.jmx.demo:type=Hello"); Hello mbean = new Hello(); mbs.registerMBean(mbean, name); System.out.println("Waiting forever..."); Thread.sleep(Long.MAX_VALUE); } }
getNotificationInfo方法的做用,暫時只知道會讓「」通知「」多一個子節點:this