7 Replies Latest reply on Oct 17, 2012 5:17 AM by Claus Ibsen

    Creating Custom MBeans

    Eric Bender Newbie

      Hello, I have some beans I use within my routes that I would like to be able to access some methods of via JMX.  I have tried with both my processors as well as simple POJO beans with no luck thus far.  I am using Camel 2.8.


      For the custom bean I have basically done this:


      @ManagedResource(description = "Message Counter")
      public class MessageCounter implements MessageCounterMBean {...}
      public String getStats() {...}


      Within my camel context I have:


      <jmxAgent id="agent" mbeanObjectDomainName="com.ihg.javelin.mdm-realtime" usePlatformMBeanServer="true" />   


      When I connect to the MBeans via jconsole I don't see anything listed, even within processors when I add the same annotations.  What am I missing or how do I register processor / custom beans with the platform server JMX?


      I have tried importing the following to use these annotations but it doesn't seem to matter.


      import org.springframework.jmx.export.annotation.ManagedAttribute;

      import org.springframework.jmx.export.annotation.ManagedResource;


      Though I know in camel 2.9 these have moved to camel.api.management...


      What do I need to do to get this to work in camel 2.8?


      Edited by: eric.bender on Oct 11, 2012 8:15 PM

        • 1. Re: Creating Custom MBeans
          Eric Bender Newbie

          Deleted this post to sum up more in original post.


          Edited by: eric.bender on Oct 11, 2012 8:18 PM

          • 2. Re: Creating Custom MBeans
            Claus Ibsen Master

            If you use Spring annotations you can use the Spring mbean exporter to have the bean exported into JMX.


            If you want Camel to detect and do this for you, when for example the bean is part of a Camel component or used in a Camel route as a processor / bean, then you need to implement the org.apache.camel.Service as Camel only enlist services in JMX out of the box.


            But looking at your code it seems its pure Spring JMX so use the spring jmx exporter.

            • 3. Re: Creating Custom MBeans
              Eric Bender Newbie

              Deleted this post as it is no longer relevant.


              Edited by: eric.bender on Oct 15, 2012 7:50 PM

              • 4. Re: Creating Custom MBeans
                Eric Bender Newbie

                Alright I made some progress.  I added this to my beans xml


                 <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
                    <property name="beans">
                        <entry key="bean:name=messageCounter1" value-ref="messageCounter"></entry>


                It now shows up under beans in JConsole, however it seems to not respect the spring annotations for the managed resource name, nor what methods and attributes to expose via JMX.  It seems all methods are exposed even though only two have the spring annotations.


                Any ideas as to how to get that to work better, and how to put it somewhere other than simply "beans" and locate it with the rest of the route specific entities?

                • 5. Re: Creating Custom MBeans
                  Claus Ibsen Master

                  There may be a way to instruct Spring to auto detect the beans and enlist them in JMX. They have some <xxx-scan> tags you can insert in the Spring XML file. I am not sure if that applies to JMX as well.


                  I dont know why the @ annotations dont seem to work for you.

                  Though for @ManagedAttribute, then make sure its a bean getter/setter, or either one.

                  And for @ManagedOperation it can be any other method.

                  • 6. Re: Creating Custom MBeans
                    Eric Bender Newbie

                    Figured it out.  To get the JMX exporter to pick up the annotations, you have to include the annotations attribute bean as a property of the exporter


                         <bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
                                <property name="assembler" ref="assembler"></property>
                                <property name="autodetect" value="true"></property>
                         <bean id="jmxAttributeSource"       class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"></bean>
                         <bean id="assembler" class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
                             <property name="attributeSource" ref="jmxAttributeSource"></property>


                    • 7. Re: Creating Custom MBeans
                      Claus Ibsen Master

                      Thanks for sharing your solution.


                      Its a lot of confusing XML though. Would be nice if there was a Spring XML namespace to make this simpler to use. Anyway it works