Terry.Li-彬

虚其心,可解天下之问;专其心,可治天下之学;静其心,可悟天下之理;恒其心,可成天下之业。

  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理 ::
  143 随笔 :: 344 文章 :: 130 评论 :: 0 Trackbacks

1、前言

JMX管理及使用日趋成熟。spring对bean的管理功能也很强大,通过annotation把某些方法暴露成MBean,然后通过JMX远程管理,在某些场景下会带来意想不到的效果。

 

本文只介绍两个内容:spring通过annotation暴露MBean、相关的xml配置。

 

 

2、spring通过annotation暴露MBean

 

      2.1   涉及到三个重要的annotation:@ManagedResource @ManagedAttribute 和 @ManagedOperation。

 

用途 Commons Attributes属性 JDK 5.0注解 属性/注解类型
将类的所有实例标识为JMX受控资源 ManagedResource @ManagedResource Class 类
将方法标识为JMX操作 ManagedOperation @ManagedOperation Method方法
将getter或者setter标识为部分JMX属性 ManagedAttribute @ManagedAttribute Method (only getters and setters) 方法(仅getters和setters)
定义操作参数说明 ManagedOperationParameter @ManagedOperationParameter@ManagedOperationParameters Method 方法

 

 

    2.2   看了上面官方解释,应该有个大概的了解,下面再举个例子。

 

Java代码  收藏代码
  1. @ManagedResource (objectName= "bean:name=lionbuleTest" , description= "My Managed Bean" )  
  2. public   class  AnnotationTestMBean{  
  3.     private String name;  
  4.     private int age;  
  5.       
  6.     @ManagedAttribute(description="The Name Attribute")  
  7.     public void setName(String name) {  
  8.         this.name = name;  
  9.     }  
  10.   
  11.     @ManagedAttribute()  
  12.     public String getName() {  
  13.         return name;  
  14.     }    
  15.     public int getAge() {  
  16.         return age;  
  17.     }  
  18.     public void setAge(int age) {  
  19.         this.age = age;  
  20.     }  
  21.       
  22.     @ManagedOperation(description="Add two numbers")  
  23.     @ManagedOperationParameters({  
  24.     @ManagedOperationParameter(name = "x", description = "The first number"),  
  25.     @ManagedOperationParameter(name = "y", description = "The second number")})  
  26.     public int add_1(int x, int y) {  
  27.         return x + y;  
  28.     }  
  29.   
  30.     @ManagedOperation  
  31.     public int add_2(int x, int y){  
  32.         return x + y;  
  33.     }  
  34.     
  35.     public void dontExposeMe() {  
  36.         throw new RuntimeException();  
  37.     }  
  38.   
  39. }  

 

   2.3  Jconsole的管理界面截图:

 

图1、查看已暴露MBean的属性

 

图2、查看已暴露MBean的方法

 

   2.4   解释说明:

 

         1、@ManagedResource @ManagedAttribute 和 @ManagedOperation 还有许多参数,具体使用请参考spring官方手册。( spring手册[2.5.3] ---- 20.3.4. 源代码级的元数据类型)

 

         2、@ManagedOperationParameters 是对@ManagedOperation的补充。具体看代码样例中的add1方法上的注解,然后再看图2(查看已暴露MBean的方法)的add1方法和add2的区别。添加参数说明的add1方法会显示出参数名,而add2方法则是默认的参数名p1/p2。

 

         3、没有添加@ManagedOperation和@ManagedAttribute的方法,在图2中就没有看到,说明添加了注解的方法暴露MBean是可用的。

 

         4、@ManagedOperation和@ManagedAttribute的区别,请查看2.1的详解。

 

3、xml配置

    方式一、通用spring bean配置

Xml代码  收藏代码
  1.     < bean   id = "jmxAttributeSource"   
  2.          class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>  
  3.    <!-- 使用注解元数据创建管理接口 -->  
  4.    <bean id="assembler"  
  5.          class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">  
  6.        <property name="attributeSource" ref="jmxAttributeSource"/>  
  7.    </bean>  
  8.    <!-- 从注解中得到ObjectName  -->  
  9.    <bean id="namingStrategy"  
  10.          class="org.springframework.jmx.export.naming.MetadataNamingStrategy">  
  11.        <property name="attributeSource" ref="jmxAttributeSource"/>  
  12.    </bean>  
  13.    <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">  
  14.        <property name="assembler" ref="assembler"/>  
  15.        <property name="namingStrategy" ref="namingStrategy"/>  
  16.        <property name="autodetect" value="true"/>  
  17.    </bean>  
  18.   
  19. <!-- 配置服务器端连接器RMI -->   
  20. < bean   class = "org.springframework.remoting.rmi.RmiRegistryFactoryBean" >           
  21.     <property name="port" value="2088"/>  
  22. </ bean >   
  23. < bean   id = "serverConnector"   
  24.       class="org.springframework.jmx.support.ConnectorServerFactoryBean">  
  25.   <property name="objectName" value="connector:name=rmi"/>  
  26.          <!-- 客户端链接地址配置 -->  
  27.   <property name="serviceUrl"  
  28.             value="service:jmx:rmi://localhost/jndi/rmi://localhost:2088/myconnector"/>  
  29. </ bean >   
  30.   
  31. <!-- 自定义的mbean -->   
  32.    <bean id="annotationTestMBean" class="com.lionbule.biz.test.AnnotationTestMBean">  
  33.        <property name="name" value="TEST"/>  
  34.        <property name="age" value="100"/>  
  35.    </bean>  

    配置中,已经加了相应的注释。还不是很清楚,可以查询spring官方手册。

 

    方式二、简化spring-context配置

Xml代码  收藏代码
  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.     xmlns:context="http://www.springframework.org/schema/context"  
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans  
  6.                         http://www.springframework.org/schema/beans/spring-beans.xsd  
  7.                         http://www.springframework.org/schema/context  
  8.                         http://www.springframework.org/schema/context/spring-context.xsd"  
  9.     default-autowire="byName">  
  10.   
  11.     <context:mbean-export default-domain="ShowCase" registration="replaceExisting" />  
  12.     <!-- 自定义的mbean -->  
  13.     <bean id="annotationTestMBean" class="com.lionbule.biz.test.AnnotationTestMBean">  
  14.         <property name="name" value="TEST"/>  
  15.         <property name="age" value="100"/>  
  16.     </bean>  
  17. </ beans >   

    注意:方式二对ClassName和beanName有约束,请以‘MBean’结尾。 

 

4、客户端工具连接

 

    常用的客户端工具:Jconsole、jvisualvm、jmanager等。

    连接方式有多种,但本案只介绍了RMI连接管理,所以本案的链接地址为:

Xml代码  收藏代码
  1. service:jmx:rmi://localhost/jndi/rmi://localhost:2088/myconnector  

 

参考资料:

1、spring官方手册

     http://static.springsource.org/spring/docs/2.5.x/reference/jmx.html#jmx-interface

2.  http://wiki.springside.org.cn/display/SpringSide3/JMX

posted on 2013-01-31 17:02 礼物 阅读(2443) 评论(0)  编辑  收藏 所属分类: JMX

只有注册用户登录后才能发表评论。

网站导航: