4 Replies Latest reply on Nov 7, 2003 4:28 PM by sys user

    trying to register mbean - not showing up in jmx-console

    sys user Master

      Hi - I'm trying to extend a message driven bean such that I can get some statistics using jmx.

      Here is the implementation -

      // MdbBean.java
      // Message Driven bean
      package testmdb;

      import javax.ejb.EJBException;
      import javax.ejb.MessageDrivenBean;
      import javax.ejb.MessageDrivenContext;
      import javax.jms.Message;
      import javax.jms.MessageListener;
      import javax.jms.TextMessage;
      import javax.jms.JMSException;
      import javax.management.ObjectName;
      import javax.management.MBeanServer;
      import javax.management.MBeanServerFactory;

      public class MdbBean implements MessageDrivenBean,
      MessageListener,
      MdbBeanMBean
      {

      private transient MessageDrivenContext mdbContext;
      private int messagesProcessed = 0;
      private int instances = 0;
      private static MBeanServer _server = null;
      private static ObjectName _objectName = null;

      // ------------------------------------------------------------------
      // MessageDrivenBean implementation
      // ------------------------------------------------------------------

      /**
      * Default constructor
      *
      */
      public MdbBean()
      {

      }

      public void setMessageDrivenContext(MessageDrivenContext ctx)
      {
      System.out.println( "MdbBean setMessageDrivenContext");
      mdbContext = ctx;
      }

      public void ejbRemove()
      {
      System.out.println( "MdbBean ejbRemove");
      }

      public void ejbCreate()
      {
      if (_server==null)
      {
      _server = MBeanServerFactory.createMBeanServer();
      try
      {
      _objectName = new ObjectName("jboss:type=mdb,name=test_mbean");
      _server.registerMBean((Object)this,_objectName);
      }
      catch (Exception ex)
      {
      System.err.println("Exception caught: "+ex);
      throw new EJBException (ex);
      }
      }
      System.out.println( "MdbBean ejbCreate");
      }
      /*
      public static MBeanServer getMBeanServer()
      {
      return _server;
      }
      */

      /**
      * onMessage method
      */
      public void onMessage(Message message)
      {
      messagesProcessed++;
      System.out.println( "MdbBean onMessage");
      try
      {
      TextMessage mess = (TextMessage)message;
      System.out.println( "Message received: "+mess.getText());
      }
      catch(JMSException ex)
      {
      System.err.println("Exception caught: "+ex);
      }
      }

      // JMX functions
      public int getNumberMessagesProcessed()
      {
      return messagesProcessed;
      }

      public int getNumberInstances()
      {
      return instances;
      }
      }


      It seems to build and load fine but I can't see it in the jmx-console web page - am I doing something wrong? or missing some configuration step?

      Thanks in advance,
      H

        • 1. Re: trying to register mbean - not showing up in jmx-console
          Adrian Brock Master

          findMBeanServer not createMBeanServer

          If you are not worried about portability there is
          org.jboss.mx.util.MBeanServerLocator.locateJBoss()
          in 3.2.2

          Regards,
          Adrian

          • 2. Re: trying to register mbean - not showing up in jmx-console
            Adrian Brock Master

            Also, what you are trying to do won't work.

            1) There are many MDBs instances, it will fail when it
            creates the second instance.
            2) You are allowing a point of entry into the EJB that
            is outside the control of the container. This can be
            unpredictable.

            I'd suggest writing an interceptor to collect the
            statistics but I think you'll find the statistics you
            want on http://localhost:8080/web-console
            or JSR77 anyway.

            Regards,
            Adrian

            • 3. Re: trying to register mbean - not showing up in jmx-console
              sys user Master

              Adrian - thanks for the response.

              I did try -
              _server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);

              after doing some further reading - but this also failed to show up in the console (oddly, in testing with Jonas, this same code worked). your further point though about this not being a long term workable solution is one that I came to as well. so I created an MBean - MdbManagerMBean, which for now just does createInstance, removeInstance, incrementInstanceMessageCount and things like that , the thought being that each message driven bean that is booted up can report statistics and so on to a central place.

              I think an interceptor seems to be the most elegant approach but a downside is that it is very jboss specific.

              Well - still learning the ropes but at least I have a slightly better handle on where things are.

              Thanks for the link to the web-console, I hadn't tried this before.

              • 4. Re: trying to register mbean - not showing up in jmx-console
                sys user Master

                fyi - I think I know why the MBean was not showing up in the cosole - even when using findMBeanServer, it looks like the thread is not spawned right away in my configuration, it is only spawned after a message is first sent