JMX - MethodExclusionMBeanInfoAssembler
Hi,
I was trying to exclude some of the methods exposed in a mBean.
I'm still able to access the method resetResetCount though i've excluded it.
Please let me know in case i've missed some thing. The code is attached.
Thanks in advance,
Ramu.
SimpleStandard.java
Code:
package test.jmx;
import javax.management.AttributeChangeNotification;
import javax.management.MBeanNotificationInfo;
import javax.management.NotificationBroadcasterSupport;
public class SimpleStandard
extends NotificationBroadcasterSupport
implements SimpleStandardMBean {
private String state = "initial state";
private int nbChanges = 0;
private int nbResets = 0;
public String getState() {
return state;
}
public void setState(String s) {
state = s;
nbChanges++;
}
public int getNbChanges() {
return nbChanges;
}
public void reset() {
AttributeChangeNotification acn =
new AttributeChangeNotification(this,
0,
0,
"NbChanges reset",
"NbChanges",
"Integer",
new Integer(nbChanges),
new Integer(0));
state = "initial state";
nbChanges = 0;
nbResets++;
sendNotification(acn);
}
public int getNbResets() {
return nbResets;
}
public MBeanNotificationInfo[] getNotificationInfo() {
return new MBeanNotificationInfo[] {
new MBeanNotificationInfo(
new String[] { AttributeChangeNotification.ATTRIBUTE_CHANGE },
AttributeChangeNotification.class.getName(),
"This notification is emitted when the reset() method is called.")
};
}
public void resetResetCount() {
this.nbResets = 0 ;
}
}
Server.java
Code:
package test.jmx.spring;
import javax.management.MBeanInfo;
import javax.management.MBeanOperationInfo;
import javax.management.MBeanServer;
import javax.management.ObjectInstance;
import javax.management.ObjectName;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jmx.export.MBeanExporter;
import org.springframework.jmx.export.assembler.MethodExclusionMBeanInfoAssembler;
public class Server {
public static void main(String[] args) throws Exception{
ApplicationContext context =
new ClassPathXmlApplicationContext("server.xml");
MBeanServer mBeanServer = (MBeanServer)context.getBean("mbeanServer");
ObjectName objName = ObjectName.getInstance("sample:name=standard mbean");
MBeanInfo info = mBeanServer.getMBeanInfo(objName);
MBeanOperationInfo[] opsInfo = info.getOperations();
System.out.println(info.getDescription());
for(int i =0 ;i < opsInfo.length ; i++)
System.out.println(""+(i+1)+"."+opsInfo[i].getName());
mBeanServer.invoke(objName, "resetResetCount", null, null);
}
}
the context.xml
Code:
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"/>
<bean id="serverConnector" class="org.springframework.jmx.support.ConnectorServerFactoryBean">
<property name="objectName"><value>system:name=rmiconnector</value></property>
<property name="serviceUrl"><value>service:jmx:rmi://localhost/jndi/rmi://localhost:9999/test</value></property>
<property name="environment">
<props>
<prop key="jmx.remote.jndi.rebind">true</prop>
</props>
</property>
</bean>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="sample:name=standard mbean" value-ref="standard"/>
</map>
</property>
<property name="assembler">
<bean class="org.springframework.jmx.export.assembler.MethodExclusionMBeanInfoAssembler">
<property name="ignoredMethodMappings">
<props>
<prop key="sample:name=standard mbean">resetResetCount</prop>
</props>
</property>
</bean>
</property>
</bean>
<bean id="standard" class="test.jmx.SimpleStandard">
<property name="state" value="TEST"/>
</bean>
<!--
<bean id="exporterHttp" class="org.springframework.jmx.export.MBeanExporter">
<property name="autodetect" value="true"/>
<property name="beans">
<map>
<entry key="system:name=http" value-ref="htmlAdapter"/>
</map>
</property>
</bean>
<bean id="htmlAdapter"
class="com.sun.jdmk.comm.HtmlAdaptorServer" init-method="start">
<property name="port" value="9292"/>
</bean>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter">
<property name="autodetect"><value>true</value></property>
<property name="server">
<ref bean="mbeanServer"/>
</property>
<property name="beans">
<map>
<entry key=""><ref bean="standard"/></entry>
</map>
</property>
</bean>
<bean id="standard" class=""/>
-->
</beans>
MethodExclusionMBeanInfoAssembler - Try Bean Name Instead of ObjectName
I don't use the MethodExclusionMBeanInfoAssembler, but based on the Javadoc comments for the setIgnoredMethodMappings(Properties) method in that class and based on other applications of Spring XML, I suggest that you try using your Spring bean's name for the property key rather than using the exposed MBean ObjectName for the property key. In other words, replace
Code:
<prop key="sample:name=standard mbean">resetResetCount</prop>
with
Code:
<prop key="standard">resetResetCount</prop>
and try that to see if that resolves the problem.
I personally prefer the alternative and better documented approaches for controlling Spring MBean exposure such as use of the MethodNameBasedMBeanInfoAssembler, the InterfaceBasedMBeanInfoAssembler, and the MetadataMBeanInfoAssembler.