Loading...
середа, 28 травня 2014 р.

Expose POJO as MBean in Spring (JMX)

There are lots of tutorials for this purpose (for example: this link), but I was not able to figure out working solution from the spot. So why this happened? Let's have a closer look. In the example we have the following class:
public class JmxTestBean implements IJmxTestBean {

    private String name;
    private int age;
    private boolean isSuperman;

    //Getters and setters and some methods
}
The first question about interface IJmxTestBean: what is it and why we need it?
This interface contains just getters and setters and other methods that should be exposed. No problem with that. But next we need to declare our POJO as a bean and add it to the MBeanExporter. Again, everything is quite easy:
<beans>

 <!-- this bean must not be lazily initialized if the exporting is to happen -->
  <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
    <property name="beans">
      <map>
        <entry key="bean:name=testBean1" value-ref="testBean"/>
      </map>
    </property>
  </bean>

  <bean id="testBean" class="org.springframework.jmx.JmxTestBean">
    <property name="name" value="TEST"/>
    <property name="age" value="100"/>
  </bean>

</beans>
Here you should be VERY careful, because of the thing marked red. If you miss lazy-init="false", you should add the IJmxTestBean interface to the assembler:
<bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
    <property name="attributeSource" ref="attributeSource"/>
    <property name="interfaceMappings">
            <map>
                <entry key="spring:service=batch,bean=test bean"
                       value="some.package.ITestMBean"/>
            </map>
     </property>

</bean>

0 коментарі:

 
TOP