隨筆 - 6, 文章 - 3, 評論 - 3, 引用 - 0
          數(shù)據(jù)加載中……

          Spring 2.x jmx 及應(yīng)用(1)Annotation

          現(xiàn)在大部分企業(yè)的應(yīng)用程序都需要application提供監(jiān)視程序運行的健康狀況和硬件狀況的功能。
          舉個例子,前段時間做的一個項目,需要在一臺機器上監(jiān)視幾十臺client的操作,CPU使用率,內(nèi)存,硬盤等信息,當(dāng)CPU,內(nèi)存使用率過高時,發(fā)出通知到該client.甚至操作client的執(zhí)行動作,都可用jmx做到。
          今天將從簡單入手結(jié)合jdk的Annotation來實踐Spring對jmx的支持

           1<?xml version="1.0" encoding="UTF-8"?>
           2<beans xmlns="http://www.springframework.org/schema/beans"
           3       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           4       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
           5       
           6    <!-- mbean -->
           7 <bean id="mbeanManager" class="com.xmlasia.spring.test.jmx.MBeanManager"/>
           8 
           9 
          10 <!-- JMX configuration -->
          11 <!-- 創(chuàng)建一個mbeanServer -->
          12 <bean id="mbeanServer" 
          13    class="org.springframework.jmx.support.MBeanServerFactoryBean">
          14 </bean>
          15  
          16 <!-- MetadataMBeanInfoAssembler是AutodetectCapableMBeanInfoAssembler 唯一實現(xiàn)
          17  spring文檔中有專門介紹AutodetectCapableMBeanInfoAssembler的章節(jié)
          18  -->
          19 <bean id="assembler"
          20    class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
          21  <property name="attributeSource" ref="jmxAttributeSource"/>
          22 </bean>
          23 
          24 <!-- 解釋mbean中Annotation的類,我們可以看到他給注入到assembler,被org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler包裝-->
          25 <bean id="jmxAttributeSource"
          26    class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
          27
          28         <!-- spring jmx最核心的類 -->
          29 <bean id="mBeanExporter" class="org.springframework.jmx.export.MBeanExporter">
          30  <property name="server" ref="mbeanServer"/>
          31  <property name="assembler" ref="assembler"/>
          32  <property name="beans">
          33   <!-- 將mbean注冊到mBeanExporter -->
          34   <map>
          35    <entry key="mbean:name=testBean" value-ref="mbeanManager"/>
          36   </map>
          37  </property>
          38 </bean>
          39</beans>
          40



          以前jmx的應(yīng)用需要編寫很長的代碼來實現(xiàn),現(xiàn)在你只需完成以上配置就可以實現(xiàn)一個簡單的jmx server的程序,jmx server的實現(xiàn)有很多,通常情況下,jmx會需要一個容器例如jboss,現(xiàn)在的方法是將程序本身作為server,這個方式最經(jīng)典的例子就是個大apserver,我的第一次jmx體驗就是在看jboss源碼時發(fā)起的。實現(xiàn)方式不同會用到不同的協(xié)議,這里我們需要jmxremote.jar,你可在spring的lib中找到。


          現(xiàn)在來看下我們的mbean

           1package com.xmlasia.spring.test.jmx;
           2
           3import org.springframework.jmx.export.annotation.ManagedAttribute;
           4import org.springframework.jmx.export.annotation.ManagedOperation;
           5import org.springframework.jmx.export.annotation.ManagedOperationParameter;
           6import org.springframework.jmx.export.annotation.ManagedOperationParameters;
           7import org.springframework.jmx.export.annotation.ManagedResource;
           8
           9@ManagedResource
          10public class MBeanManager {
          11 private int clientStatus;
          12
          13 @ManagedOperation(description = "pause a single proccess")
          14 @ManagedOperationParameters( { @ManagedOperationParameter(name = "Name of proccess instance", description = "Mandatory") })
          15 public void pause(String n) {
          16  System.out.println("pause");
          17 }

          18
          19 @ManagedOperation(description = "shut down the proccess")
          20 public void monitor() {
          21  System.out.println("shutting down");
          22 }

          23
          24 public void publicMessage() {
          25  System.out.println("public Message to monitor server");
          26 }

          27
          28 @ManagedAttribute(description = "client status")
          29 public int getClientStatus() {
          30  return clientStatus;
          31 }

          32
          33 @ManagedAttribute(description = "client status")
          34 public void setClientStatus(int clientStatus) {
          35  this.clientStatus = clientStatus;
          36 }

          37}

          38



          我們可以到類中定義了幾個方法,monitor server可以通過clientStatus了解到client的狀態(tài),publicMessage是用來主動的發(fā)送消息到monitor server,所有不需要想外界暴露。monitor server可以通過
          獲得信息中的ObjectName和Ip來連接到client操作mbean.

          mbean在jdk的標(biāo)準實現(xiàn)是需要實現(xiàn)一個以MBean結(jié)尾的interface的,其中定義向外界暴露的方法和屬性
          現(xiàn)在之需要在類前定義@ManagedResource,在屬性方法前分別用@ManagedOperation或者@ManagedAttribute修飾就可以了,Annotation實在是太方便了。未來將會有更多的應(yīng)用。


          該測試了

           1
           2public static void main(String[] args){
           3  ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/jmxContext.xml");
           4  
           5  while(true){
           6   try {
           7    Thread.sleep(1000);
           8   }
           catch (InterruptedException e) {
           9    // TODO Auto-generated catch block
          10    e.printStackTrace();
          11   }

          12  }

          13 }



          現(xiàn)在我們只需要讓Spring加載上面那段bean的定義就好了。jmx mbean server的啟動是不會阻塞主線程的,所以需要保持主線程活著,不然加載完main方法就結(jié)束了,mbean server也掛了。

          jdk里有個強大的monitor server那就是jconsole,不過一次只有一個連接
          大家可以用jconsole來連接到mbean,調(diào)用方法或?qū)傩圆炜唇Y(jié)果。

          spring jmx很有多有用的功能本人正在挖掘,過段時間再放出一些心得,歡迎有興趣的一起討論。

          posted on 2007-07-24 17:02 馬甲丁 閱讀(233) 評論(0)  編輯  收藏


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 昌都县| 融水| 左云县| 贺州市| 民乐县| 郸城县| 芷江| 松溪县| 民丰县| 明光市| 泸州市| 射洪县| 梧州市| 改则县| 买车| 类乌齐县| 米泉市| 玉溪市| 栖霞市| 乐亭县| 涞源县| 甘肃省| 常德市| 赣州市| 萨嘎县| 贵港市| 桦南县| 江川县| 东平县| 宽城| 石棉县| 昌黎县| 龙川县| 保德县| 项城市| 湘西| 朝阳市| 金沙县| 秀山| 灵寿县| 锡林郭勒盟|