- 浏览: 745571 次
- 性别:
- 来自: 杭州
-
文章分类
最新评论
-
luger:
谢谢 解决了我的问题
spring import jar中的bean配置文件 -
diamondy:
sunny80062951414 写道没有fieldinfo信 ...
利用ASM和Javassist动态生成Class 类(set和get) -
sunny80062951414:
没有fieldinfo信息啊。
利用ASM和Javassist动态生成Class 类(set和get) -
somefuture:
求解!求思路!完全迷茫中。
珍藏经典面试题目一个 -
xiao1227372602:
lionbule 写道xiao1227372602 写道请问下 ...
Linux 命令 alternatives和update-alternatives
1、前言
JMX管理及使用日趋成熟。spring对bean的管理功能也很强大,通过annotation把某些方法暴露成MBean,然后通过JMX远程管理,在某些场景下会带来意想不到的效果。
本文只介绍两个内容:spring通过annotation暴露MBean、相关的xml配置。
2、spring通过annotation暴露MBean
2.1 涉及到三个重要的annotation:@ManagedResource @ManagedAttribute 和 @ManagedOperation。
将类的所有实例标识为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 看了上面官方解释,应该有个大概的了解,下面再举个例子。
@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 import jar中的bean配置文件
2012-06-12 17:28 11555在spring中import resource file,有两 ... -
Adding OSGi metadata to existing projects without changing the packaging type
2012-06-05 10:20 1934http://felix.apache.org/site/ap ... -
比较If else 和 try catch 的jvm指令
2012-06-01 10:40 2752编码时过度关注某个点性能,有时候整体性能适得其反!关于if e ... -
java 类加载实例分析之tomcat6.x
2012-03-26 18:47 0[前言] 研究OSGI已经有一段时间,而OSGI的灵魂就在于 ... -
Java 数字转字符串 自动补0
2012-02-20 14:18 3061java中数字转字符,在确定位数的情况下,左边自动补0以对齐 ... -
Java HotSpot VM Options
2012-01-05 11:03 1940http://www.oracle.com/techne ... -
利用ASM和Javassist动态生成Class 类(set和get)
2011-12-30 21:20 11588利用jvm的指令集直接构造class,简单的bean clas ... -
jvm 指令集
2011-12-30 19:46 2263JVM指令助记符 变量到操作数栈:iload,iload_ ... -
switch 和 if elseif 哪个效率高?
2011-12-30 11:23 3372switch 和 if elseif 哪个效率高? ... -
列表和数组的区别
2011-12-27 10:59 4333在数据结构中有多种集合的表现形式!而集合其实就是一个真实 ... -
JVM 选择合适的GC算法
2011-12-19 15:17 1952Selecting a Collector Unles ... -
动态修改JVM 部分参数
2011-12-05 17:00 7025参考地址: http://rednaxelafx.iteye. ... -
Java 快捷读取CLASSPATH下的资源文件
2011-11-30 11:26 17453在JVM中查找classpath下的资源文件,使用Class. ... -
java main函数为非守护线程(实时线程)
2011-11-23 14:29 4458一直认为main为守护线程,google线程相关的资料时,发现 ... -
Solution to Eclipse Warning with SVN
2011-11-14 16:58 1492Solution to Eclipse Warning wit ... -
Java简单数据类型
2011-09-29 11:27 1441简单类型 大小 ... -
spring3.0 单元测试
2011-09-27 18:42 2198spring3.0.0之后的版本,官方极力推荐使用如下方式做单 ... -
logback 之 创建 RollingFileAppender
2011-09-24 17:01 8238最近有需求要将context信息写入文件,以备后续数据分析。本 ... -
logback 之encoder和layout
2011-09-24 16:49 7039encoder 和 layout 在作用上没有本质区别。但是自 ... -
JAVA 排序算法
2011-08-30 21:57 2215import java.util.Random; ...
相关推荐
2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)容器 3.1. 简介 3.2. 基本原理 - 容器和bean 3.2.1. 容器 3.2.2. 实例化容器 3.2.3. 多种bean ...
2.7. 移植到Spring 2.5 2.7.1. 改变 2.8. 更新的样例应用 2.9. 改进的文档 I. 核心技术 3. IoC(控制反转)容器 3.1. 简介 3.2. 基本原理 - 容器和bean 3.2.1. 容器 3.2.2. 实例化容器 3.2.3. 多种bean ...
52.1. Customizing MBean Names 52.2. Disabling JMX Endpoints 52.3. Using Jolokia for JMX over HTTP 52.3.1. Customizing Jolokia 52.3.2. Disabling Jolokia 53. Loggers 53.1. Configure a Logger 54. Metrics...
10. **JMX Beans**:`<context:mbean-export>`和`<context:mbean-server>`用于将Spring Beans暴露为JMX管理资源,方便监控和管理。 了解并熟练掌握这些Spring配置元信息的概念和用法,能够帮助开发者更有效地设计和...
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; @Endpoint(id = "myendpoint") public class MyEndpoint { @ReadOperation public MyInfo info() { // 实现获取自定义信息的逻辑...
4. **JMX插件**:对于Java管理扩展(JMX),XDoclet能够自动生成MBean接口和实现,方便了系统的监控和管理。 5. **JPA插件**:随着Java Persistence API(JPA)的出现,XDoclet提供了相应的插件,可以生成JPA的实体...