spring通过annotation注解注册MBean到JMX实现监控java运行状态
2016-08-08 10:06
573 查看
原文:spring通过annotation注解注册MBean到JMX实现监控java运行状态
源代码下载地址:http://www.zuidaima.com/share/1828116267568128.htm
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注解 属性/注解类型
2.2 看了上面官方解释,应该有个大概的了解,下面再举个例子。
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配置
配置中,已经加了相应的注释。还不是很清楚,可以查询spring官方手册。
方式二、简化spring-context配置
注意:方式二对ClassName和beanName有约束,请以‘MBean’结尾。
4、客户端工具连接
常用的客户端工具:Jconsole、jvisualvm、jmanager等。
连接方式有多种,但本案只介绍了RMI连接管理,所以本案的链接地址为:
参考资料:
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
源代码下载地址:http://www.zuidaima.com/share/1828116267568128.htm
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 方法 |
@ManagedResource (objectName= "bean:name=lionbuleTest" , description= "My Managed Bean" ) public class AnnotationTestMBean{ private String name; private int age; @ManagedAttribute(description="The Name Attribute") public void setName(String name) { this.name = name; } @ManagedAttribute() public String getName() { return name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } @ManagedOperation(description="Add two numbers") @ManagedOperationParameters({ @ManagedOperationParameter(name = "x", description = "The first number"), @ManagedOperationParameter(name = "y", description = "The second number")}) public int add_1(int x, int y) { return x + y; } @ManagedOperation public int add_2(int x, int y){ return x + y; } public void dontExposeMe() { throw new RuntimeException(); } }
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配置
<bean id = "jmxAttributeSource" class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/> <!-- 使用注解元数据创建管理接口 --> <bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler"> <property name="attributeSource" ref="jmxAttributeSource"/> </bean> <!-- 从注解中得到ObjectName --> <bean id="namingStrategy" class="org.springframework.jmx.export.naming.MetadataNamingStrategy"> <property name="attributeSource" ref="jmxAttributeSource"/> </bean> <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"> <property name="assembler" ref="assembler"/> <property name="namingStrategy" ref="namingStrategy"/> <property name="autodetect" value="true"/> </bean> <!-- 配置服务器端连接器RMI --> <bean class = "org.springframework.remoting.rmi.RmiRegistryFactoryBean" > <property name="port" value="2088"/> </bean > <bean id = "serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean"> <property name="objectName" value="connector:name=rmi"/> <!-- 客户端链接地址配置 --> <property name="serviceUrl" value="service:jmx:rmi://localhost/jndi/rmi://localhost:2088/myconnector"/> </bean > <!-- 自定义的mbean --> <bean id="annotationTestMBean" class="com.lionbule.biz.test.AnnotationTestMBean"> <property name="name" value="TEST"/> <property name="age" value="100"/> </bean>
配置中,已经加了相应的注释。还不是很清楚,可以查询spring官方手册。
方式二、简化spring-context配置
<? xml version = "1.0" encoding = "UTF-8" ?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName"> <context:mbean-export default-domain="ShowCase" registration="replaceExisting" /> <!-- 自定义的mbean --> <bean id="annotationTestMBean" class="com.lionbule.biz.test.AnnotationTestMBean"> <property name="name" value="TEST"/> <property name="age" value="100"/> </bean> </beans >
注意:方式二对ClassName和beanName有约束,请以‘MBean’结尾。
4、客户端工具连接
常用的客户端工具:Jconsole、jvisualvm、jmanager等。
连接方式有多种,但本案只介绍了RMI连接管理,所以本案的链接地址为:
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
相关文章推荐
- spring通过annotation注解注册MBean到JMX实现监控java运行状态
- spring通过annotation注解注册MBean到JMX实现监控java运行状态
- Spring注解实现Bean注册时异常解决方案 - Context initialization failed java.lang.IllegalArgumentException
- springAop Schedule,注解annotation 实现任务监控
- 通过Annotation实现对程序的运行状态监听,简单实现
- Java实现对程序运行状态的监控
- Spring通过java注解的方式进行配置annotation
- Java 实现程序运行状态监控 之我的思路(守护进程)
- java spring 使用注解来实现缓存
- 一个简单的基于注解的 Controller (spring 2.5 annotation 方式实现mvc )
- Spring零配置通过注解实现Bean依赖注入总结<转>
- 利用JMX监控Websphere Application Server6.1的J2EE应用服务运行状态
- Java 学习笔记05:Spring 基于注解(Annotation)的IOC
- 通过SpringMVC+Annotation实现方法、按钮级别的细粒度权限控制
- 通过Spring实现对自定义注解属性进行资源注入
- 通过JMX监控管理weblogic的运行
- Java程序员从笨鸟到菜鸟之(七十二)细谈Spring(四)利用注解实现spring基本配置详解
- 通过weblogic 9 mbean监控其应用状态
- Java的注解机制——Spring自动装配的实现原理
- 通过JMX监控管理weblogic的运行