<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

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


    網站導航:
     
    主站蜘蛛池模板: 中文字幕不卡免费视频| 亚洲AV成人一区二区三区观看 | 四虎国产精品免费久久| 亚洲美女精品视频| 69视频在线观看高清免费| 亚洲一区影音先锋色资源| 99re免费99re在线视频手机版| 亚洲伦理一区二区| 免费观看无遮挡www的视频| 亚洲午夜在线播放| 啦啦啦在线免费视频| 国产亚洲一卡2卡3卡4卡新区| 国产一级特黄高清免费大片| 无套内射无矿码免费看黄| 久久亚洲色一区二区三区| 永久免费av无码网站yy| 久久久亚洲欧洲日产国码aⅴ| 国产精品1024永久免费视频| 亚洲高清中文字幕免费| 免费v片在线观看品善网| 皇色在线免费视频| 久久精品国产亚洲AV无码娇色| 久久国内免费视频| 白白色免费在线视频| 亚洲熟妇无码AV在线播放| 亚洲黄色免费电影| 欧洲亚洲国产精华液| 亚洲精品无码专区久久久 | 毛片免费在线观看| 亚洲人成电影网站| 亚洲国产成人精品91久久久| 三年片免费观看大全国语| 亚洲欧洲日产韩国在线| 男人的天堂亚洲一区二区三区| 免费观看四虎精品成人| 亚洲欧洲日产国码久在线观看| 在线免费视频一区| 免费国产在线视频| mm1313亚洲国产精品无码试看| 日韩亚洲一区二区三区| 四虎影视www四虎免费|