Exposing your Beans to JMX
The core class in Spring JMX is the JmxMBeanAdapter. Using this class you can expose any Spring managed bean as a JMX managed resource. The configuration code below shows how you do this:
<beans>
<bean id="jmxAdapter" class="org.springframework.jmx.export.JmxMBeanAdapter">
<property name="beans">
<map>
<entry key="bean:name=testBean1">
<ref local="testBean"/>
</entry>
</map>
</property>
</bean>
<bean id="testBean" class="org.springframework.jmx.JmxTestBean">
<property name="name">
<value>TEST</value>
</property>
<property name="age">
<value>100</value>
</property>
</bean>
</beans>
The beans that you want to expose are passed in as Map to beans property of JmxMBeanAdapter.
The JmxMBeanAdapter uses three components internally, the implementations of which can be swapped out:
- Metadata Assembler
- Invoker
- Naming Strategy
The Metadata Assembler
The Metadata Assembler is responsible for creating the MBean managed interface metadata. The default implementation, ReflectiveModelMBeanInfoAssembler, creates the appropriate metadata to expose all methods as JMX operations and all JavaBeans properties as JMX attributes. The other available implementation is MetadataModelMBeanInfoAssembler. This implementation allows you to control which operations and attributes are exposed using source level metadata. The code belows shows a bean whose JMX interface is controlled using source level metadata:
package org.springframework.jmx;
/**
* @@org.springframework.jmx.metadata.support.ManagedResource(description="My Managed
* Bean",
* objectName="spring:bean=test")
*/
public class JmxTestBean {
private String name;
private int age;
/**
* @@org.springframework.jmx.metadata.support.ManagedAttribute(description="The Name Attribute")
*/
public void setName(String name) {
this.name = name;
}
/**
* @@org.springframework.jmx.metadata.support.ManagedAttribute()
*/
public String getName() {
return name;
}
/**
* @@org.springframework.jmx.metadata.support.ManagedOperation(description="Add Two Numbers Together")
*/
public int add(int x, int y) {
return x + y;
}
}
The ManagedOperation attribute is used to expose methods as operations, the ManagedAttribute is used to expose properties as attribute. You can selectively mark the get/set methods of the property to control attribute access. The ManagedResource attribute is marker but is used by another component that we will see shortly.
You can configure a different metadata assembler by setting the assembler property of the JmxMBeanAdapter.
The Invoker
The invoker responds to requests from the MBeanServer to invoke operations and read/write attributes. The default implementation is ReflectiveMBeanInvoker which uses reflection to execute the appropriate methods. You can swap this out for a CglibMBeanInvoker which is slightly faster on a 1.4 VM but significantly faster on a 1.3 VM.
Naming Strategy
The naming strategy is responsible for giving MBeans their JMX ObjectNames. The default naming strategy is KeyNamingStrategy which uses the key of the beans Map passed to the JmxMBeanAdapter. The PropertiesNamingStrategy can be used associate the Map key with a entry in a properties file from which the ObjectName will be looked up. The MetadataNamingStrategy can be used to specify the ObjectName as argument of the ManagedResource attribute as shown in the previous code snippet.
Have you given some thought to providing a Metadata Assembler that exposes only the attributes in the bean configuration and the init and destroy operations.