Feb 16 2009

Exposing MBeans with Spring

Published by Gilles at 9:21 pm under J2EE, Java, Spring

Using JDK 5.0 Annotations

An other way to expose Mbeans is to use the JDK5 annotations.
provides a set of annotations that mirror the Commons Attribute attribute classes and an implementation of the JmxAttributeSource strategy interface, the AnnotationsJmxAttributeSource class, that allows the MBeanInfoAssembler to read them.

Putting all together

  • applicationContext-.xml
XML:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:tx="http://www.springframework.org/schema/tx"
  5. xmlns:util="http://www.springframework.org/schema/util"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/-beans-2.5.xsd
  8. http://www.springframework.org/schema/util http://www.springframework.org/schema/util/-util-2.5.xsd
  9. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/-tx-2.5.xsd">
  10.        <bean id="mbeanServer" class="org.springframework..support.MBeanServerFactoryBean">
  11. <!-- indicate to first look for a server -->
  12. <property name="locateExistingServerIfPossible" value="true"/>
  13. </bean>
  14. <bean id="exporter" class="org.springframework..export.MBeanExporter">
  15. <property name="assembler" ref="assembler"/>
  16. <property name="namingStrategy" ref="namingStrategy"/>
  17. <property name="autodetect" value="true"/>
  18. </bean>
  19. <bean name="com.mycompany.bean:type=beanType" class="com.mycompany.class"/>
  20. <bean id="jmxAttributeSource"
  21. class="org.springframework..export.annotation.AnnotationJmxAttributeSource"/>
  22. <!-- will create management interface using annotation metadata -->
  23. <bean id="assembler"
  24. class="org.springframework..export.assembler.MetadataMBeanInfoAssembler">
  25. <property name="attributeSource" ref="jmxAttributeSource"/>
  26. </bean>
  27. <!-- will pick up the ObjectName from the annotation -->
  28.     <bean id="namingStrategy"class="org.springframework..export.naming.MetadataNamingStrategy">
  29.     <property name="attributeSource" ref="jmxAttributeSource"/>
  30.     </bean>
  31. </beans>

  • Mbean trough annotations
JAVA:
  1. @ManagedResource(objectName="com.mycompany:type=beanImpl", description="MBean desc", log=true,
  2. logFile=".log", currencyTimeLimit=15, persistPolicy="never")
  3. public class XXX{
  4.     @ManagedOperation(description="zzz")
  5.     public void getX(){
  6. }

Pages: 1 2 3 4 5

5 Responses to “Exposing MBeans with Spring”

  1. KPon 25 Feb 2009 at 10:27 pm

    I was trying to set up a JMX managed standalone Spring application and the article helped a lot. Thanks!!!

  2. markon 06 Nov 2009 at 11:49 am

    Hello, I found your post about the Spring mbeans in the WebSphere 6.1. I am new to Spring and JMX and looking for a step by step guide to register and calling by wsadmin some Spring’s mbeans provided by the app developer. I read your post and do understand the websphere naming strategy but where do I need to place this class WebsphereNamingStrategy ?

    br
    Mark

  3. Gilleson 06 Nov 2009 at 11:59 am

    Hello Mark,
    You can place your WebsphereNamingStrategy class where you want. For my part, I place it in an “util” package…
    Cheers,
    Gilles

  4. markon 06 Nov 2009 at 12:24 pm

    Hello Gilles,
    Thanks a lot for a quick response. I just have one question if there is any possibility to enable the Spring’s mbean in WAS 6.1 without adding any new code to the project ? I do not have the source code and do not want to extend the EAR file.
    br
    Mark

  5. hoschilordon 26 Aug 2010 at 8:23 am

    excellent info, thanks!
    was trying to get this running on websphere and also figured out that websphere changes the objectnames of the mbeans..your sources saved a lot of time for me yeehaa :-)

Trackback URI | Comments RSS

Leave a Reply