spring提供了灵活的MBean方式
除了之前介绍过的Annotation
还有Metadata方式,类似于Annotation
这种方式在创建javadoc时候会给予警告但不会影响doc的创建
package com.xmlasia.spring.test.jmx;
/**
* @@org.springframework.jmx.export.metadata.ManagedResource
* (description="My Managed Bean",objectName="spring:bean=test")
*
*/
public class CommentMBeanManager {
private int clientStatus;
/**
* @@org.springframework.jmx.export.metadata.ManagedOperation
* (description="pause a single proccess")
*
*/
public void pause(String n) {
System.out.println("pause");
}
/**
* @@org.springframework.jmx.export.metadata.ManagedOperation
* (description="shut down the proccess")
*/
public void monitor() {
System.out.println("shutting down...");
}
public void publicMessage() {
System.out.println("publicMessage");
}
/**
* @@org.springframework.jmx.export.metadata.ManagedAttribute
* (description="client status")
*/
public int getClientStatus() {
return clientStatus;
}
public void setClientStatus(int clientStatus) {
this.clientStatus = clientStatus;
}
}
使用这种方式需要改变attributeSource的实现类
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- mbean -->
<bean id="mbeanManager"
class="com.xmlasia.spring.test.jmx.CommentMBeanManager" />
<!-- JMX configuration -->
<bean id="mbeanServer"
class="org.springframework.jmx.support.MBeanServerFactoryBean">
</bean>
<!-- 注意这里将更换为AttributesJmxAttributeSource -->
<bean id="attributeSource"
class="org.springframework.jmx.export.metadata.AttributesJmxAttributeSource">
<property name="attributes">
<bean
class="org.springframework.metadata.commons.CommonsAttributes" />
</property>
</bean>
<bean id="assembler"
class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
<property name="attributeSource" ref="attributeSource" />
</bean>
<bean id="mBeanExporter"
class="org.springframework.jmx.export.MBeanExporter">
<property name="server" ref="mbeanServer" />
<property name="assembler" ref="assembler" />
<property name="beans">
<!-- 将mbean注册到mBeanExporter -->
<map>
<entry key="mbean:name=testBean"
value-ref="mbeanManager" />
</map>
</property>
</bean>
</beans>
其他照旧不需修改
如果你有很多mbean spring也提供了很好的解决方法
你只需在修改mBeanExporter的属性autodetect=true
<bean id="mBeanExporter"
class="org.springframework.jmx.export.MBeanExporter">
<property name="server" ref="mbeanServer" />
<property name="assembler" ref="assembler" />
<property name="autodetect" value="true" />
</bean>
spring会搜索context中所有被ManagedResource标记的mbean自动进行注册
另一种方式类似于标准的jmx mbean实现,用interface来定义将要暴露的Attribute和Operation
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- mbean -->
<bean id="mbeanManager"
class="com.xmlasia.spring.test.jmx.CommentMBeanManager" />
<!-- JMX configuration -->
<bean id="mbeanServer"
class="org.springframework.jmx.support.MBeanServerFactoryBean">
</bean>
<!--使用接口来描述mbean-->
<bean id="assembler"
class="org.springframework.jmx.export.assembler.InterfaceBasedMBeanInfoAssembler">
<property name="managedInterfaces">
<value>
com.xmlasia.spring.test.jmx.ICommentMBeanManager
</value>
</property>
</bean>
<bean id="mBeanExporter"
class="org.springframework.jmx.export.MBeanExporter">
<property name="server" ref="mbeanServer" />
<property name="assembler" ref="assembler" />
<property name="beans">
<!-- 将mbean注册到mBeanExporter -->
<map>
<entry key="mbean:name=testBean"
value-ref="mbeanManager" />
</map>
</property>
</bean>
</beans>
我们可以通过refactor快速的将CommentMBeanManager的方法和属性抽取到ICommentMBeanManager中。
注意managedInterfaces是一个Class[]的类型不用担心很多接口的问题。
最后注册mbean是有3种行为的分别是
REGISTRATION_FAIL_ON_EXISTING mbean已经存在将会抛出InstanceAlreadyExistsException异常
REGISTRATION_IGNORE_EXISTING 忽视后来注册的mbean
REGISTRATION_REPLACE_EXISTING 覆盖掉之前注册的mbean
通过MBeanExporter的registrationBehaviorName属性进行设定
<bean id="mBeanExporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="server" ref="mbeanServer"/>
<property name="assembler" ref="assembler"/>
<property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING"/>
</bean>
同样用jconsole进行测试
要了解springjmx前还是要需要掌握jdk对jmx定义,jmx在维护application方法是有很好的应用的。
继续挖掘中