Dashboard > Spring Documentation Sandbox > Spring JMX > Exposing your Beans
  Spring Documentation Sandbox Log In View a printable version of the current page.  
  Exposing your Beans
Added by Rob Harrop, last edited by Hugh Madden on May 11, 2005  (view change)
Labels: 
(None)

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.

Posted by Anonymous at Aug 18, 2004 17:31

There is a bug in JmxMBeanAdapter that shows only if a beans map with more than one entry is used. Since the 'invoker' variable in JmxMBeanAdapter is shared by all instances of ModelMBeanImpl and each one of those calls setManagedResource(...), the invoker ends up being only able to invoke methods on the last registered MBean

Posted by Anonymous at Oct 08, 2004 07:50

How about supporting standard MBeans? Perhaps the Reflective... could detect that the bean is a standard MBean and just register it.

What about beans that implement the MBeanRegistration interface?

How about allowing more than one MBean view of a bean? This would allow different roles to see different views.

Would it be possible to have a bean that defined model mbean properties for a bean, so that you could specify the attributes and operations completely in XML instead of with metadata? This would allow the registration of components where you don't have the source or don't want to recompile to update things.

Posted by Anonymous at Oct 13, 2004 19:43
Site running on a free Atlassian Confluence Open Source Project License granted to Spring Framework. Evaluate Confluence today.
Powered by Atlassian Confluence, the Enterprise Wiki. (Version: 2.5.5 Build:#811 Jul 25, 2007) - Bug/feature request - Contact Administrators