10 Replies Latest reply on May 4, 2002 11:47 AM by sebjerke

    XMBean examples from Juha's JMX book

    sebjerke

      Hi,

      I bought and read the book on JMX; thanks a lot for writing it, it
      certainly helped my understanding on JMX a lot!!

      I wanted to try XMBean from the book. I used the downloaded code
      from the book, along with the dtd, and the xml description of User
      (with some slight changes to make it fit). However, since the XMBean
      implements the MBeanRegistration interface, and then also contains
      the preRegister method, I don't get the example to run; I get an error like
      this:

      javax.management.MBeanRegistrationException: Exception thrown in preRegister method
      at com.sun.management.jmx.MBeanServerImpl.preRegisterInvoker(MBeanServerImpl.java:2257)
      at com.sun.management.jmx.MBeanServerImpl.registerMBean(MBeanServerImpl.java:864)
      at book.jmx.examples.DipsAgent.run(Agent.java:64)
      at book.jmx.examples.DipsAgent.main(Agent.java:46)

      Do anyone have any clue what causes this?

      I looked at the code for the given class in the RI, and it didn't help me
      much.

      I'd be very grateful for any comments on this!

        • 1. Re: XMBean examples from Juha's JMX book

          Something in the preRegister() method is not agreeing with you. Try getting the root cause from the MBeanRegistrationException and print it out to see the real reason... probably not finding the URL for the xml definition, or something similar.

          -- Juha

          • 2. Re: XMBean examples from Juha's JMX book
            sebjerke

            Hi Juha,

            Well, I think the error is that I have an operation taking an int as parameter. When coming as far as to createSignature(ModelMBeanOperationInfo info) ofXMBeanOperation.java taken from your book, an exception is thrown when calling loadClass(), since type in my case is an int:

            String type = sign.getType();
            paramTypes
            = DefaultLoaderRepository.loadClass(type);

            Am I correct when I say that your code here doesn't support primitive type data types as parameters to operations?

            I have a more general question; I see from the current JBossMX code (taken from CVS a few days ago) that the XMBean is quite changed compared to the one in your book. I wonder if you could say a few things why for instance XMBeanOperation.java and XMBeanAttribute.java are gone? And maybe also short comment the big changes from the book's XMBean?

            Thanks for your help!
            --
            Sven Erik

            • 3. Re: XMBean examples from Juha's JMX book

              > Well, I think the error is that I have an operation
              > taking an int as parameter. When coming as far as to
              > createSignature(ModelMBeanOperationInfo info)
              > ofXMBeanOperation.java taken from your book, an
              > exception is thrown when calling loadClass(), since
              > type in my case is an int:
              >
              > String type = sign.getType();
              > paramTypes
              = DefaultLoaderRepository.loadClass(type);

              oi, that might be a bug indeed.


              > I have a more general question; I see from the
              > current JBossMX code (taken from CVS a few days ago)
              > that the XMBean is quite changed compared to the one
              > in your book. I wonder if you could say a few things
              > why for instance XMBeanOperation.java and
              > XMBeanAttribute.java are gone?

              yes, I wanted the functionality from these two classes implemented as interceptors, as it seemed to me to simplify the overall design when new behaviour is added to the model mbean.


              > And maybe also short comment the big changes from the book's XMBean?

              well the biggest one is the book version is currently a bit more completed. However, I don't think I'll be going back to that design so at some point this will be turned the other way around.

              • 4. Re: XMBean examples from Juha's JMX book

                > > String type = sign.getType();
                > > paramTypes
                = DefaultLoaderRepository.loadClass(type);
                >
                > oi, that might be a bug indeed.
                >

                On the other hand, now that I think about it, Adrian implemented a fix in JBossMX DefaultLoaderRepository to recognize primary type names and handle them correctly, so the book XMBean implementation might work if you try to run it with JBoss JMX implementation.

                -- Juha

                • 5. Re: XMBean examples from Juha's JMX book
                  sebjerke

                  Hi,
                  When compiling and running it with JBossMX (alpha2), and still using the HtmlAdaptorServer from Sun, it works :-)
                  (And for the record; it doesn't work with the RI from Sun).

                  Thanks for your help!
                  --
                  Sven Erik

                  • 6. Re: XMBean examples from Juha's JMX book

                    Please confirm this.

                    In order to use the XMBean WITH native data types in the method signatures, one should use ONLY JBossMX and never the JMXRI because of the above found bug.

                    Right?

                    Thanks
                    Madhu

                    • 7. Re: XMBean examples from Juha's JMX book

                      Yes,

                      Technically, it is not a bug because it is not
                      required by the standard. Standard support across
                      all JMX implementations is some way off.

                      Look at point 23 in the DELAYED section
                      http://jcp.org/aboutJava/communityprocess/maintenance/jsr003/final_list.html

                      Regards,
                      Adrian

                      • 8. Re: XMBean examples from Juha's JMX book
                        sebjerke

                        > Please confirm this.
                        > In order to use the XMBean WITH native data types in
                        > the method signatures, one should use ONLY JBossMX
                        > and never the JMXRI because of the above found bug.
                        > Right?

                        Inspired by Juha's tip, I had a look at the code of org.joss.mx.loading.LoaderRepository, and modified
                        the createSignature of XMBeanOperation to use i.e. Integer.TYPE in the case of primitive data types in method signatures. That way, the XMBean also works with the Sun RI.

                        I hope this helps! If not, tell me, and I'll post what I did in more detail.

                        --
                        Sven Erik

                        • 9. Re: XMBean examples from Juha's JMX book

                          please submit the changes to code if possible

                          I want to include bug fixes, corrections, etc. in the subsequent printings (and you'll get your name on the website too ;)

                          -- Juha

                          • 10. Re: XMBean examples from Juha's JMX book
                            sebjerke

                            > please submit the changes to code if possible

                            Ok, here are the changes I did. In
                            XMBeanOperation.createSignature(ModelMBeanOperationInfo info):
                            ORIGINAL CODE WAS:
                            for (int j = 0; j < paramTypes.length; ++j) {
                            try {
                            String type = sign[j].getType();
                            paramTypes[j] = DefaultLoaderRepository.loadClass(type);
                            }
                            CHANGED TO:
                            for ( int j = 0; j<paramTypes.length; ++j) {
                            try {
                            String type = sign[j].getType();
                            Class cls = (Class) nativeClassBySignature.get(type);
                            if ( cls != null ) {
                            paramTypes[j] = cls;
                            }
                            else {
                            paramTypes[j] = DefaultLoaderRepository.loadClass(type);
                            }

                            The class NativeClassMapping was more or less taken directly from org.jboss.mx.loading.LoaderRepository, as indicated in an earlier post:

                            import java.util.Vector;
                            import java.util.HashMap;
                            public abstract class NativeClassMapping {
                            protected static HashMap nativeClassBySignature = new HashMap();
                            static {
                            nativeClassBySignature.put(boolean.class.getName(), Boolean.TYPE);
                            nativeClassBySignature.put(byte.class.getName(), Byte.TYPE);
                            nativeClassBySignature.put(char.class.getName(), Character.TYPE);
                            nativeClassBySignature.put(double.class.getName(), Double.TYPE);
                            nativeClassBySignature.put(float.class.getName(), Float.TYPE);
                            nativeClassBySignature.put(int.class.getName(), Integer.TYPE);
                            nativeClassBySignature.put(long.class.getName(), Long.TYPE);
                            nativeClassBySignature.put(short.class.getName(), Short.TYPE);
                            nativeClassBySignature.put(void.class.getName(), Void.TYPE);
                            }
                            }

                            XMBeanOperation has to extend NativeClassBySignature, of course.

                            I don't know how elegant this solution is, but it works for me. Sorry for my late reply, but hope this is what you wanted. Also thanks again to Juha for his book, and thanks to all in this forum for all posts; it has helped me a lot to understand JMX better.

                            --
                            Sven Erik