活到老,學到老

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            9 Posts :: 1 Stories :: 2 Comments :: 0 Trackbacks
              這篇寫一個簡單的HelloWorld例子。
              首先準備環(huán)境,我使用的JDK1.6,1.5應該也可以。還需要去oracle下載JMX RI包,地址為:http://www.oracle.com/technetwork/java/javase/tech/download-jsp-141676.html,下載“JMX 1.2 Reference Implementation”,解壓以后lib目錄下有兩個jar包,把jmxtool.jar加到CLASSPATH中就可以了,它里面有一個接下去要用到的Html適配器。
              1、首先寫一個HelloWorld MBean,它由一個接口和一個實現(xiàn)類組成,代碼如下:
          public interface HelloWorldMBean {
              
          public void
           setGreeting(String greeting);
              
          public
           String getGreeting();
              
          public void
           printGreeting();
          }

              寫實現(xiàn)類HelloWorld:
          public class HelloWorld implements HelloWorldMBean {

              
          private String greeting = null
          ;

              
          public HelloWorld() 
          {
                  
          this.greeting = "Hello World! I am a Standard MBean"
          ;
              }


              
          public HelloWorld(String greeting) {
                  
          this.greeting =
           greeting;
              }


              
          public void setGreeting(String greeting) {
                  
          this.greeting =
           greeting;
              }


              
          public String getGreeting() {
                  
          return
           greeting;
              }


              
          public void printGreeting() {
                  System.out.println(greeting);
              }


          }
              
              這樣,一個HelloWorld的MBean就完成了,這是一個標準MBean。必須把MBean注冊到Agent才能使用,接下來寫一個Agent。

              2、定義JMX Agent:HelloAgent,他有三個任務:
              1)、創(chuàng)建MBean Server實例。
              2)、創(chuàng)建HTML適配器和HTML客戶端連接。
              3)、注冊一個新的HelloWorld的MBean實例。
              代碼如下:
              
          public class HelloAgent {

              
          private MBeanServer mbs = null;

              
          public HelloAgent() {
                  mbs 
          = MBeanServerFactory.createMBeanServer("HelloAgent");

                  HtmlAdaptorServer adapter 
          = new HtmlAdaptorServer();
                  HelloWorld hw 
          = new HelloWorld();
                  ObjectName adapterName 
          = null;
                  ObjectName helloWorldName 
          = null;
                  
          try {
                      helloWorldName 
          = new ObjectName("HelloAgent:name=helloWorld1");
                      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");
                  HelloAgent agent 
          = new HelloAgent();
              }

          }

              這段代碼首先創(chuàng)建一個MBean Server實例和一個Html適配器實例,MBean Server使用工廠類創(chuàng)建,創(chuàng)建的時候傳入字符串作為MBean Server的域名,域名是區(qū)別MBean Server的標識。
              接下來實例化HelloWorld MBean,并在MBean Server中注冊。注冊的時候使用一個ObjectName實例,ObjectName類在JMX中為MBean提供了一套命名系統(tǒng),是注冊在MBean Server中的唯一標識。它有兩部分組成:
              1)、域名:這個域名通常和MBean Server的域名一致,如果不一致,則意味著與其他MBean隔離。
              2)、零個或多個key=value串,中間用逗號隔開,這個串用來區(qū)別MBean,也可以為MBean提供信息。
              下一步是注冊Html適配器,Html適配器也是一個MBean,并啟動適配器。

              以上兩步就是我們例子的代碼,基本結構圖如下:
              


              3、運行例子。HelloAgent類有main方法,直接運行就可以了,如果成功就會出現(xiàn)“HelloAgent is running”。然后打開瀏覽器,輸入:http://localhost:9092/,因為代碼中Html的適配器端口設置為9092。

              以上3步完成了整個HelloWorld例子,通過瀏覽器提供了3種頁面:
              1、Agent頁面,也就是第一個看到的頁面,上面Agent內包含的MBean的一個總覽,它顯示了所有的注冊在里面的MBean,通過注冊時候使用的ObjectName實例來顯示。可以通過最上面的文本框來過濾需要顯示的MBean。
              2、MBean頁面,點擊Agent頁面中的某個MBean可以進入該MBean頁面。我們點擊第一個name=helloWorld1的MBean,顯示有一下幾個信息:
                  a)、MBean注冊時提供的ObjectName,HelloAgent:name=helloWorld1
                  b)、類的名字,在這個例子中是HelloWorld。 
                  c)、描述,對于Stand MBean,描述有MBean Server創(chuàng)建。
                  d)、屬性列表,MBean暴露的屬性列表,這個MBean有一個屬性Greeting是個可讀寫屬性(RW,因為有getter和setter),你可以在Value列的文本框中輸入字符串,點擊Apply,就動態(tài)設置了Greeting的值。
                  e)、暴露的操作列表,這個MBean有一個操作printGreeting,點擊printGreeting按鈕可以調用該操作,會顯示”printGreeting Successful“的信息,在控制臺可以看到打印出了你剛才輸入的Greeting屬性的值。
                  f)、Reload Period,指的是MBean Server是否要重新實例化這個MBean,如果是,多久一次。
                  g)、Unregister按鈕,反注冊這個MBean。
              本例中還有一個MBean,就是Html適配器,因為它也在Agent注冊了成為一個MBean.
              3、Admin頁面,點擊Agent頁面的Admin按鈕就進入了Admin頁面。通過這個頁面可以增加或者刪除MBean,頁面上有4個文本框分別如下:
                  a)、Domain-顯示了當前Agent的Domain,
                  b)、Keys,也就是ObjectName類的屬性串。
                  c)、Java Class,想創(chuàng)建的MBean的完整類名。
                  d)、Class Loader,這個是可選的,其他都是必須的。
                  在Action選項框下面有一個Constructors選項,如果選了這個,點擊Send Request,就會顯示MBean的所有構造器,可以使用其中的一個來創(chuàng)建MBean實例了。

              MBean通知
              加入通知代碼到HelloWorld的MBean,JMX提供了兩種方法使MBean可以作為其他MBean的監(jiān)聽對象。第一種是實現(xiàn)javax.management.NotificationBroadcaster接口,第二種時繼承javax.management.NotificationBroadcasterSupport類。
              實現(xiàn)接口的好處是你還可以繼承其他類,繼承類的好處是你不需要寫實現(xiàn)接口的方法代碼。我們選擇使用繼承類的方法,HelloWorld類代碼修改成如下:
              
          public class HelloWorld extends NotificationBroadcasterSupport implements HelloWorldMBean {
              
          public HelloWorld() {
                  
          this.greeting = "Hello World! I am a Standard MBean";
              }


              
          public HelloWorld(String greeting) {
                  
          this.greeting = greeting;
              }


              
          public void setGreeting(String greeting) {
                  
          this.greeting = greeting;
                  Notification notification 
          = new Notification("jmxtest.ch02.helloWorld.test"this-1,
                      System.currentTimeMillis(), greeting);
                  sendNotification(notification);
              }


              
          public String getGreeting() {
                  
          return greeting;
              }


              
          public void printGreeting() {
                  System.out.println(greeting);
              }


              
          private String greeting;
          }

              在setGreeting方法中增加了一些代碼,首先實例化一個通知,然后發(fā)送這個通知,也就是說在greeting屬性設置的時候,發(fā)送通知到對次事件感興趣的對象。實例化通話需要5個參數,第一個是這個通知的標識符,第二個是參數是通知源,也就是產生通知的MBean,第三個是個序列號,第四個是發(fā)送時間,第五個是發(fā)送的消息字符串。
              
              修改HelloAgent,代碼如下:
              
          public class HelloAgent implements NotificationListener {

              
          private MBeanServer mbs = null;

              
          public HelloAgent() {
                  mbs 
          = MBeanServerFactory.createMBeanServer("HelloAgent");
                  HtmlAdaptorServer adapter 
          = new HtmlAdaptorServer();
                  HelloWorld hw 
          = new HelloWorld();
                  ObjectName adapterName 
          = null;
                  ObjectName helloWorldName 
          = null;
                  
          try {
                      adapterName 
          = new ObjectName("HelloAgent:name=htmladapter,port=9092");
                      mbs.registerMBean(adapter, adapterName);
                      adapter.setPort(
          9092);
                      adapter.start();
                      helloWorldName 
          = new ObjectName("HelloAgent:name=helloWorld1");
                      mbs.registerMBean(hw, helloWorldName);
                      hw.addNotificationListener(
          thisnullnull);

                  }
           catch (Exception e) {
                      e.printStackTrace();
                  }

              }


              
          public void handleNotification(Notification notif, Object handback) {
                  System.out.println(
          "Receiving notification");
                  System.out.println(notif.getType());
                  System.out.println(notif.getMessage());
              }


              
          public static void main(String args[]) {
                  System.out.println(
          "HelloAgent is running");
                  HelloAgent agent 
          = new HelloAgent();
              }

          }

              在構造器中增加了“hw.addNotificationListener(this, null, null);”這行代碼,它就是把HelloAgent加入到HelloWorld MBean的監(jiān)聽列表中。還增加了一個方法handleNotification,這個是實現(xiàn)了NotificationListener接口的方法,當HelloWorld Mbean的greeting屬性被設置的時候,就會調用這個方法,其實是很簡單的一個觀察者模式。
              運行這個例子,修改greeting屬性,點擊Apply,看到控制臺打印了handleNotification方法中的一些信息。
              
          posted on 2011-04-05 16:20 simon.shen 閱讀(2964) 評論(1)  編輯  收藏 所屬分類: Java企業(yè)開發(fā)

          Feedback

          # re: JMX In Action 總結(二)--HelloWorld例子 2011-04-05 18:30 兵丸網絡
          寫得很不錯。多謝分享。  回復  更多評論
            

          主站蜘蛛池模板: 南宁市| 清新县| 日土县| 陆河县| 东乡族自治县| 成都市| 阿巴嘎旗| 宁强县| 昆山市| 象州县| 孟村| 新兴县| 晋州市| 杭州市| 石阡县| 泸水县| 达拉特旗| 新兴县| 岳阳县| 威信县| 宣汉县| 隆化县| 贡山| 香河县| 台南市| 大方县| 公安县| 常熟市| 张家口市| 布尔津县| 普兰店市| 城步| 昭平县| 武平县| 香港| 龙岩市| 富顺县| 巴林右旗| 灵台县| 肇东市| 泸溪县|