隨筆 - 6, 文章 - 3, 評論 - 3, 引用 - 0
          數據加載中……

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

          現在大部分企業的應用程序都需要application提供監視程序運行的健康狀況和硬件狀況的功能。
          舉個例子,前段時間做的一個項目,需要在一臺機器上監視幾十臺client的操作,CPU使用率,內存,硬盤等信息,當CPU,內存使用率過高時,發出通知到該client.甚至操作client的執行動作,都可用jmx做到。
          今天將從簡單入手結合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 <!-- 創建一個mbeanServer -->
          12 <bean id="mbeanServer" 
          13    class="org.springframework.jmx.support.MBeanServerFactoryBean">
          14 </bean>
          15  
          16 <!-- MetadataMBeanInfoAssembler是AutodetectCapableMBeanInfoAssembler 唯一實現
          17  spring文檔中有專門介紹AutodetectCapableMBeanInfoAssembler的章節
          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的應用需要編寫很長的代碼來實現,現在你只需完成以上配置就可以實現一個簡單的jmx server的程序,jmx server的實現有很多,通常情況下,jmx會需要一個容器例如jboss,現在的方法是將程序本身作為server,這個方式最經典的例子就是個大apserver,我的第一次jmx體驗就是在看jboss源碼時發起的。實現方式不同會用到不同的協議,這里我們需要jmxremote.jar,你可在spring的lib中找到。


          現在來看下我們的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的狀態,publicMessage是用來主動的發送消息到monitor server,所有不需要想外界暴露。monitor server可以通過
          獲得信息中的ObjectName和Ip來連接到client操作mbean.

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


          該測試了

           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 }



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

          jdk里有個強大的monitor server那就是jconsole,不過一次只有一個連接
          大家可以用jconsole來連接到mbean,調用方法或屬性察看結果。

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

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


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


          網站導航:
           
          主站蜘蛛池模板: 特克斯县| 前郭尔| 东乡族自治县| 永仁县| 荣昌县| 泰宁县| 靖宇县| 沙河市| 郁南县| 略阳县| 桐乡市| 泰宁县| 南丰县| 襄樊市| 新乡市| 稻城县| 雷州市| 东安县| 宜春市| 敖汉旗| 遵化市| 榆林市| 邵阳市| 福建省| 保定市| 新龙县| 苏尼特右旗| 郸城县| 满城县| 永丰县| 日土县| 奉化市| 田东县| 新巴尔虎左旗| 二连浩特市| 石狮市| 新郑市| 巧家县| 离岛区| 巴彦淖尔市| 桐庐县|