Controlling the ObjectNames for your beans - Java-Springs

Behind the scenes, the MBean Exporter delegates to an implementation of the ObjectNamingStrategy to obtain Objec tNames for each of the beans it is registering. The default implementation, Key Naming Strategy, will, by default, use the key of the beans Map as the ObjectName. In addition, the Key Naming Strategy can map the key of the beans Map to an entry in a Properties file (or files) to resolve the ObjectName. In addition to the Key Naming Strategy, Spring provides two additional Object Naming Strategy implementations:
the Identity Naming Strategy that builds an ObjectName based on the JVM identity of the bean and the Metadata NamingStrategy that uses source level metadata to obtain the ObjectName.

Reading ObjectNames from Properties
You can configure your own KeyNamingStrategy instance and configure it to read Object Names from a Properties instance rather than use bean key. The Key NamingStrategy will attempt to locate an entry in the Properties with a key corresponding to the bean key. If no entry is found or if the Properties instance is null then the bean key itself is used.

The code below shows a sample configuration for the KeyNamingStrategy:

<beans>
<bean id="exporter"class="org.springframework.
jmx.export.MBeanExporter">
<property name="beans">
<map>
<entry key="testBean"value-ref="testBean"/>
</map>
</property>
<property name="namingStrategy" ref="namingStrategy"/>
</bean>
<bean id="testBean"class="org.springframework.jmx.JmxTestBean">
<property name="name"value="TEST"/>
<property name="age"value="100"/>
</bean>
<bean id="namingStrategy"class="org.springframework.
jmx.export.naming.KeyNamingStrategy">
<property name="mappings">
<props>
<prop key="testBean">bean:name=testBean1</prop>
</props>
</property>
<property name="mappingLocations">
<value>names1.properties,names2.properties</value>
</property>
</bean
</beans>

Here an instance of KeyNaming Strategy is configured with a Properties instance that is merged from the Properties instance defined by the mapping property and the properties files located in the paths defined by the mappings property. In this configuration, the testBean bean will be given the Object Name bean:name=testBean1 since this is the entry in the Properties instance that has a key corresponding to the bean key.

If no entry in the Properties instance can be found then the bean key name is used as the ObjectName.

Using the MetadataNamingStrategy
The Metadata Naming Strategy uses the objectName property of the ManagedResource attribute on each bean to create the Object Name. The code below shows the configuration for the Meta data Naming Strategy:

<beans>
<bean id="exporter" class="org.springframework.jmx.
export.MBeanExporter">
<property name="beans">
<map>
<entry key="testBean" value-ref="testBean"/>
</map>
</property>
<property name="namingStrategy" ref="namingStrategy"/>
</bean>
<bean id="testBean" class="org.springframework.jmx.JmxTestBean">
<property name="name" value="TEST"/>
<property name="age" value="100"/>
</bean>
<bean id="namingStrategy" class="org.springframework.jmx.export.
naming.MetadataNamingStrategy">
<property name="attributeSource" ref="attributeSource"/>
</bean>
<bean id="attributeSource"
class="org.springframework.jmx.export.metadata.
AttributesJmxAttributeSource"/>
</beans>

If no objectName has been provided for the ManagedResource attribute, then an ObjectName will be created with the following format:

[fully-qualified-package-name]:type=[short-classname],
name=[bean-name]
. For example, the generated
ObjectName for the following bean would be:com.foo:
type=MyClass,name=myBean
.
<bean id="myBean" class="com.foo.MyClass"/>

The <context:mbean-export/> element
If you are using at least Java 5, then a convenience subclass of MBean Exporter is available: Annotation MBean Exporter. When defining an instance of this subclass, the naming Strategy, assembler, and attributeSource configuration is no longer needed, since it will always use standard Java annotation-based meta data (autodetection is always enabled as well). In fact, an even simpler syntax is supported by Spring's 'context' namespace.. Rather than defining an MBeanExporter bean, just provide this single element:

<context:mbean-export/>

You can provide a reference to a particular MBean server if necessary, and the defaultDomain attribute (a property of AnnotationMBeanExporter) accepts an alternate value for the generated MBean ObjectNames' domains.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java-Springs Topics