posts - 156,  comments - 601,  trackbacks - 0
          自JDK5.0 引入注解(Annotation)后,讓Java的開發簡化了很多,讓開發者幾乎擺脫復雜的
          配置文件的煩惱。本文將介紹Spring提供的一套相當于Commons Attribut屬性類的注解和一個策略接口 JmxAttributeSource 的實現類 AnnotationsJmxAttributeSource, 這個類允許 MBeanInfoAssembler 來讀這些注解。本文就給大家展示一下,使用Spring的JMX annotation,如何簡單快速讓POJO對象輸出到JMX.

          里面的出現的類名的功能,在下面介紹,先來看一個例子:

          先編寫一個POJO對象
           1 import java.util.ArrayList;
           2 import java.util.List;
           3 
           4 import org.springframework.jmx.export.annotation.ManagedAttribute;
           5 import org.springframework.jmx.export.annotation.ManagedOperation;
           6 import org.springframework.jmx.export.annotation.ManagedOperationParameter;
           7 import org.springframework.jmx.export.annotation.ManagedOperationParameters;
           8 import org.springframework.jmx.export.annotation.ManagedResource;
           9 
          10 //實例標記為由JMX管理的資源
          11 @ManagedResource(objectName="bean:name=testJmxBean", description="My Managed Bean", log=true,
          12     logFile="jmx.log", currencyTimeLimit=15, persistPolicy="OnUpdate", persistPeriod=200,
          13     persistLocation="foo", persistName="bar")
          14 public class AnnotationTestBean  {
          15 
          16   private String name;
          17   private int age;
          18   
          19   private List<String> values;
          20 
          21   //把getter或setter標記為JMX的屬性
          22   @ManagedAttribute(description="The Age Attribute", currencyTimeLimit=1)
          23   public int getAge() {
          24     return age;
          25   }
          26 
          27   public void setAge(int age) {
          28     this.age = age;
          29   }
          30   
          31   @ManagedAttribute(description="The values Attribute", currencyTimeLimit=1)
          32   public List<String> getValues() {
          33       values = new ArrayList<String>(2);
          34       values.add("hello");
          35       values.add("world");
          36       return values;
          37   }
          38 
          39   @ManagedAttribute(description="The Name Attribute",
          40       currencyTimeLimit=20,
          41       defaultValue="bar",
          42       persistPolicy="OnUpdate")
          43   public void setName(String name) {
          44     this.name = name;
          45     System.out.println("set: " + name);
          46   }
          47 
          48   @ManagedAttribute(defaultValue="foo", persistPeriod=300)
          49   public String getName() {
          50       System.out.println("get: " + name);
          51     return name;
          52   }
          53 
          54   //把方法標記為JMX的操作
          55   @ManagedOperation(description="Add two numbers")
          56   @ManagedOperationParameters({
          57     @ManagedOperationParameter(name = "x", description = "The first number"),
          58     @ManagedOperationParameter(name = "y", description = "The second number")})
          59   public int add(int x, int y) {
          60     return x + y;
          61   }
          62 
          63   public void dontExposeMe() {
          64     throw new RuntimeException();
          65   }
          66 }
          67 

          這里你可以看到,用屬性 ManagedResource 來標記類 JmxTestBean, 這個 ManagedResource 是用一系列屬性來配置的。 這些屬性用于配置由 MBeanExporter 產生的MBean的輸出。

          接下來,配置xml,來配置 MBeanExporter
           1 <beans xmlns="http://www.springframework.org/schema/beans"
           2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           3     xmlns:context="http://www.springframework.org/schema/context"
           4     xsi:schemaLocation="
           5 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
           6 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.0.xsd">
           7 
           8     <bean id="exporter"
           9         class="org.springframework.jmx.export.MBeanExporter">
          10         <property name="assembler" ref="assembler" />
          11         <property name="namingStrategy" ref="namingStrategy" />
          12         <property name="autodetect" value="true" />
          13     </bean>
          14 
          15     <bean id="jmxAttributeSource"
          16         class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource" />
          17 
          18     <!-- will create management interface using annotation metadata -->
          19     <bean id="assembler"
          20         class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
          21         <property name="attributeSource" ref="jmxAttributeSource" />
          22     </bean>
          23 
          24     <!-- will pick up ObjectName from annotation -->
          25     <bean id="namingStrategy"
          26         class="org.springframework.jmx.export.naming.MetadataNamingStrategy">
          27         <property name="attributeSource" ref="jmxAttributeSource" />
          28     </bean>
          29 
          30     <bean id="testBean"
          31         class="AnnotationTestBean">
          32         <property name="name" value="TEST" />
          33         <property name="age" value="100" />
          34     </bean>
          35 
          36 
          37 
          38 </beans>

          接下來,編寫一個測試代碼看一下效果
           1 import org.springframework.context.support.ClassPathXmlApplicationContext;
           2 
           3 
           4 public class Main {
           5     
           6 
           7     /**
           8      * @param args
           9      * @throws InterruptedException 
          10      */
          11     public static void main(String[] args) throws InterruptedException {
          12 
          13         ClassPathXmlApplicationContext context;
          14         
          15         context = new ClassPathXmlApplicationContext("run.xml");
          16         
          17         context.start();
          18  
                  while (true) {
                      Thread.sleep(10000);
                  }     
          19     }
          20 
          21 }

          注: 在運行的時候,需要在Jvm 加上參數"-Dcom.sun.management.jmxremote",否則就不能通過JConsole來查看 JMX 輸出情況。

          接下就可以運行 JConsole 就可以查看到結果。

          附:Annotation 說明:

          目的 Commons Attributes屬性 JDK 5.0 注解 屬性 / 注解類型
          Class 所有的實例標記為由JMX管理的資源 ManagedResource @ManagedResource
          把方法標記為JMX的操作 ManagedOperation @ManagedOperation 方法
          把getter或setter標記為JMX的半個屬性 ManagedAttribute @ManagedAttribute 方法(僅 getters 和 setters)
          定義描述操作參數 ManagedOperationParameter @ManagedOperationParameter@ManagedOperationParameters @ManagedOperationParameter@ManagedOperationParameters 方法


          接下來的配置參數可以用于這些源碼級的元數據類型:

          表 20.3. 源碼級的元數據參數

          參數 描述 適用于
          ObjectName 由類 MetadataNamingStrategy 使用,決定一個管理資源的 ObjectName ManagedResource
          description 設置資源、屬性或操作友好的描述 ManagedResourceManagedAttributeManagedOperationManagedOperationParameter
          currencyTimeLimit 描述符字段,用于設置 currencyTimeLimit 的值 ManagedResourceManagedAttribute
          defaultValue 描述符字段,用于設置 defaultValue 的值 ManagedAttribute
          log 描述符字段,用于設置 log 的值 ManagedResource
          logFile 描述符字段,用于設置 logFile 的值 ManagedResource
          persistPolicy 描述符字段,用于設置 persistPolicy 的值 ManagedResource
          persistPeriod 描述符字段,用于設置 persistPeriod 的值 ManagedResource
          persistLocation 描述符字段,用于設置 persistLocation 的值 ManagedResource
          persistName 描述符字段,用于設置 persistName 的值 ManagedResource
          name 設置一個操作參數的顯示名字 ManagedOperationParameter
          index 設置操作參


          注: 本文大部分內容來自 Spring參考文檔,如果需要了解更多知識,請參見Spring refrence.

          Good Luck!
          Yours Matthew!






          posted on 2008-11-26 22:20 x.matthew 閱讀(2599) 評論(2)  編輯  收藏 所屬分類: JMX

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 长葛市| 武夷山市| 都匀市| 黎平县| 嵩明县| 潼关县| 乐安县| 曲水县| 望城县| 太仆寺旗| 资阳市| 连城县| 宜城市| 博乐市| 德令哈市| 嘉鱼县| 资阳市| 万州区| 梓潼县| 浦县| 天等县| 雷波县| 蓬莱市| 阿拉善左旗| 山阴县| 和政县| 绥德县| 大足县| 扶沟县| 曲沃县| 华阴市| 临海市| 巴塘县| 土默特右旗| 长寿区| 孟连| 乌拉特后旗| 静海县| 南康市| 永胜县| 新营市|