<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 設置資源、屬性或操作友好的描述 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 閱讀(2605) 評論(2)  編輯  收藏 所屬分類: JMX

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


    網站導航:
     
    主站蜘蛛池模板: 亚洲第一福利视频| 亚洲美女视频网站| 最近2019年免费中文字幕高清| 亚洲国产亚洲片在线观看播放| 免费国产a国产片高清网站| 免费福利电影在线观看| 亚洲情A成黄在线观看动漫软件 | 亚洲中文字幕人成乱码| 国产一级淫片免费播放| 日韩免费的视频在线观看香蕉| 亚洲日本中文字幕天天更新| 亚洲AV无码成人精品区蜜桃| 在线jlzzjlzz免费播放| 免费在线看黄网站| 久久亚洲精品无码网站| 亚洲久本草在线中文字幕| 亚洲AV无码一区二三区| 69视频在线是免费观看| 一级白嫩美女毛片免费| 亚洲中文字幕无码一去台湾| 亚洲精品制服丝袜四区| 国产一精品一aⅴ一免费| 亚洲三级在线免费观看| 成人片黄网站色大片免费观看APP| 亚洲乱码国产乱码精华| 亚洲黄网在线观看| 亚洲精品无码专区在线在线播放| 免费网站看v片在线香蕉| 久久精品无码专区免费青青| 成在线人免费无码高潮喷水| 亚洲jizzjizz少妇| 亚洲国产精品成人久久久| 亚洲国产精品VA在线观看麻豆| 亚洲Av无码乱码在线znlu| 岛国大片免费在线观看| **aaaaa毛片免费同男同女| 男人天堂免费视频| www免费插插视频| 国产午夜亚洲精品不卡免下载 | 亚洲人色大成年网站在线观看| 综合亚洲伊人午夜网 |