5 Replies Latest reply on Apr 29, 2003 11:25 PM by stevecoh1

    Accessing an MBean from within JVM

    stevecoh1

      Dumb newby question.

      How do I access an MBean and its methods via code, from within the same JVM?

      The documents treat this as a question too trivial to be discussed but how DO you access a Standard MBean and its methods from within code?

      I can see where I can get to the setAttribute() and getAttribute() methods through the server, but then I look at the implementation in the source code and I see that your code is expecting the beans it gets from the server to be Dynamic MBeans, so that won't work. I don't see a method in the MBeanServer to get the MBean itself so I can't do that and then call its methods directly.

      Here's what I want to do (oversimplified)

      MyBean mybean = (MyBean)server.getMBean(ObjectName)

      int num = myBean.getNum()
      num = num + 1;
      myBean.setNum(num);

      I know I can't do that, but what is the right way to do it?

      I know I'm missing something simple but I don't know what it is.

        • 1. Re: Accessing an MBean from within JVM

          You don't access the MBean directly.

          One of the main points to MBeans is that they
          are decoupled, you don't hold direct references
          to the service you are trying to use.

          Having that, it is possible with an MBean operation
          like:

          public MyClass getInstance()
          {
          return this;
          }

          I wouldn't recommend this approach.

          If you are worried about the performance, don't :-)

          You can always have a
          public void add(int) operation.

          Regards,
          Adrian

          • 2. Re: Accessing an MBean from within JVM
            stevecoh1

            Thanks. I had more or less come to the same conclusion and had figured out most of the points in my earlier question. However, it is blowing up on me in a most annoying way when I try to access the MBean indirectly.

            Can you explain what is happening here:
            see attached file.

            • 3. Re: Accessing an MBean from within JVM
              stevecoh1

              Anyone answering might want to see the MBean code too, I suppose :-)

              • 4. Re: Accessing an MBean from within JVM

                Change the method to something like:

                public int getLastStoryHandled() {
                try
                {
                if (this.lastStoryHandled < 0) {
                load();
                }
                return this.lastStoryHandled;
                }
                catch (RuntimeException e)
                {
                e.printStackTrace();
                throw e;
                }
                }

                Regards,
                Adrian

                • 5. Re: Accessing an MBean from within JVM
                  stevecoh1

                  Thanks.
                  Doh! I didn't even need to put the logging in. There was an uninitialized null variable in my bean. I forgot to initialize it. I spent so much time tracking down errors due to naming mismatches I forgot to look at my own code.

                  Might be a good idea for the JBoss code to show a bit more stack trace there, though.