<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 設置資源、屬性或操作友好的描述 ManagedResourceManagedAttributeManagedOperation、 ManagedOperationParameter
    currencyTimeLimit 描述符字段,用于設置 currencyTimeLimit 的值 ManagedResource、ManagedAttribute
    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 閱讀(2594) 評論(2)  編輯  收藏 所屬分類: JMX

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲色图综合在线| 久久亚洲色一区二区三区| 久久精品夜色国产亚洲av| caoporn成人免费公开| 国产一级大片免费看| 青青青亚洲精品国产| 免费v片在线观看| 色哟哟国产精品免费观看| 大胆亚洲人体视频| 美女巨胸喷奶水视频www免费| 亚洲男人的天堂在线va拉文| 久久国产免费直播| 国产V亚洲V天堂无码| 免费福利在线视频| 亚洲国产综合自在线另类| 久久久高清免费视频| 亚洲精品456人成在线| 国产免费人人看大香伊| 一级毛片免费播放视频| 国产亚洲一区二区在线观看| 久久国产乱子伦免费精品| 亚洲国产日韩在线成人蜜芽| 四虎永久在线精品免费网址| 在线亚洲精品视频| 亚洲精品乱码久久久久66| 99热在线免费播放| 亚洲色偷偷综合亚洲av78 | 2021在线永久免费视频| 亚洲欧洲自拍拍偷综合| 免费看又爽又黄禁片视频1000| WWW亚洲色大成网络.COM| 国产AV无码专区亚洲AV手机麻豆| 久久国产乱子伦精品免费看| 久久精品国产亚洲αv忘忧草| 日本人护士免费xxxx视频| 巨胸喷奶水www永久免费| 亚洲综合久久成人69| 免费大香伊蕉在人线国产| a级毛片免费全部播放无码| 亚洲综合色7777情网站777| 亚洲一区二区三区免费|