Announcement Announcement Module
Collapse
No announcement yet.
call start on dynamic mbean Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • call start on dynamic mbean

    I've create a dynamic mbean and I export it with spring into Jboss

    Code:
    <bean id="exporter" 
      class="org.springframework.jmx.export.MBeanExporter"
      lazy-init="false">
      <property name="beans">
        <map>
          <entry key="bean:name=myConfigBean" value-ref="myConfigBean" />
        </map>
      </property>
    </bean>
    
    <bean id="myConfigBean" class="org.comp.myConfigBean" />
    My Mbean is showing up in the JBoss JMXConsole...GREAT.

    But it isn't calling start....not so great.

    Is there a Spring way to invoke the start method in deployment?

  • #2
    I once wrote a simple listener that would call 'start' and 'stop' on the MBeans of my choice for me. Here's the code:

    Code:
    public class MBeanStarter implements MBeanExporterListener {
        
        private static final Logger LOGGER = Logger.getLogger(MBeanStarter.class); 
        
        private MBeanServer mbeanServer;
        private Set objectNames = Collections.EMPTY_SET;
    
        public void mbeanRegistered(ObjectName objectName) {
            if (objectNames.contains(objectName.getCanonicalName())) {
                LOGGER.info("Invoking 'start' on MBean named " + objectName);
                try {
                    mbeanServer.invoke(objectName, "start", null, null);
                } catch (Exception e) {
                    LOGGER.error("Can't start MBean named " + objectName, e);
                }
            }
        }
    
        public void mbeanUnregistered(ObjectName objectName) {
            if (objectNames.contains(objectName.getCanonicalName())) {
                LOGGER.info("Invoking 'stop' on MBean named " + objectName);
                try {
                    mbeanServer.invoke(objectName, "stop", null, null);
                } catch (Exception e) {
                    LOGGER.error("Can't stop MBean named " + objectName, e);
                }
            }
        }
    
        public void setMbeanServer(MBeanServer mbeanServer) {
            this.mbeanServer = mbeanServer;
        }
    
        public void setObjectNames(Set objectNames) {
            this.objectNames = objectNames;
        }
    }
    You could use this in your applicationcontext; just inject the MBeanServer and a set of names (as Strings, not as ObjectNames) and register the bean as a listener in the exporter:

    Code:
    <bean id="exporter" 
      class="org.springframework.jmx.export.MBeanExporter"
      lazy-init="false">
      <property name="beans">
        <map>
          <entry key="bean:name=myConfigBean" value-ref="myConfigBean" />
        </map>
      </property>
      <property name="listeners">
        <list>
          <bean class="yourpackagename.MBeanStarter">
            <property name="mbeanServer" ref="mbeanServer"/>
            <property name="objectNames">
              <set>
                <value>bean:name=myConfigBean</value>
              </set>
            </property>
          </bean>
        </list>
      </property>
    </bean>
    This assumes you have configures an MBeanServer named "mbeanServer" somewhere in your xml.

    Joris

    Comment


    • #3
      &quot;stop&quot; method chicken egg problem ?

      The call to the "stop" method does not work in my setup ( spring 2.5.4) as the method

      Code:
      public void mbeanUnregistered(ObjectName objectName)
      will be called after un registering the bean.

      See:
      docs/2.5.x/api/org/springframework/jmx/export/MBeanExporterListener.html

      mbeanUnregistered:
      Code:
      " Called by MBeanExporter after an MBean has been successfully  unregistered from an MBeanServer."
      As a result javax.management.InstanceNotFoundException is thrown.


      Any idea how to deal with this in version 2.5.x ?

      Comment

      Working...
      X