5 Replies Latest reply on May 10, 2006 6:49 AM by Dimitris Andreadis

    Using queryMBean

    Peter Johnson Master

      I am trying to get all MBeans from a given namespace and that have a particular attribute, but I can't seem to get the queryMBean method to work. According to the examples that I have seen, the following code should work:

      ObjectName name = new ObjectName("jboss.management.local:*");
      QueryExp qe = Query.match(Query.attr("j2eeType"), Query.value("Servlet"));
      Set s = getServer().queryMBeans(name, qe);

      But I get no results back. However, if I don't pass any query expression, I get all MBeans in the namespace:

      Set s = getServer().queryMBeans(name, null);

      and many of those MBeans have names whose j2eeType attribute is set to Servlet.

      Anyone have any ideas what am I doing wrong?

        • 1. Re: Using queryMBean
          Dimitris Andreadis Master

          Try a 'null' QueryExp and use the ObjectName:


          • 2. Re: Using queryMBean
            Peter Johnson Master

            OK, that works. I thought that I had gotten an exception when I tried that earlier. Oh, now I remember, what I tried at first was (this is only one of many variations of the object name string containing wild cards):

            ObjectName name = new ObjectName("jboss.management.local:EJBModule=*,*");
            getServer().queryMBeans(name, qe);

            This gave me the exception:

            Exception in thread "main" javax.management.MalformedObjectNameException: jboss.
            management.local:EJBModule=*,* is not a valid ObjectName. The value * cannot con
            tain * use quote backslash * quote or ObjectName.quote(String)

            This lead me to attempt to figure out how query expressions worked, which lead to the code I originally posted. My end goal is to come up with a query that will give me all mbeans that have a particular attribute, regardless of the value of the attribute.

            By the way, I am close to figuring out why the query isn't working. I'll post my conclusions when I am done.

            • 3. Re: Using queryMBean
              Peter Johnson Master

              I have reached my conclusion: I don't know what I am doing. :-(

              I now realize that the query expression will filter MBeans based on the values of the properties defined for the MBean. I mistakenly thought that parts of the MBean name were the attributes that I could query on. That is, an MBean with name "foo.bar:name1=xxx,name2=yyy" had attributes name1 and name2 and thus I could query on those. But actually, if this MBean has properties prop1 and prop2, I can use a query to find an MBean with particular values for properties prop1 or prop2. The tutorials that were confusing to me at first are now a lot clearer.

              But, I still have room to complain. :-)

              First, class org.jboss.mx.server.MBeanServerImpl, method queryMBean has this code:

               return queryExp.apply(objectName);
              catch (Exception e)
               return false;

              Not quite sure who thought it was a good idea to throw away the exception. So, I added a logging statement to the code to find out what exception was being thrown and noticed this exception:
              Exception in thread "main" java.lang.NullPointerException
               at javax.management.BadAttributeValueExpException.toString(BadAttributeValueExpException.java:71)
               at org.apache.log4j.spi.VectorWriter.println(ThrowableInformation.java:88)
              . . .

              OK, so printing of the stack trace blew up. Seems to be "a bad thing" to me. Looking at the above toString method, I see:

              return "Bad attribute value expression: " + val.toString();

              Hmm, not only is the call to toString on variable val superfluous, but it is also a bad idea because if val is nul? (you get the idea). OK, so get rid of the toString call on val so that I can finally get my exception printed out. And I then found another instance where an exception was being thrown away in javax.management.AttributeValueExp.getAttribute:

               MBeanServer mbs = QueryEval.getMBeanServer();
               return QueryEval.getMBeanServer().getAttribute(name, attr);
              catch (Exception e)
               return null;

              The toString bug should be fixed. Should I write a JIRA?

              For the dropped exceptions, I thought it would be nice to at least log them. But, given the way the code is written, every query will generate a lot of exceptions because if you query for a particular property, an exception is thrown for every MBEan that doesn't have such a property. But this could be handled by catching something other than Exception. Not sure if this is worth opening a JIRA or not.

              • 4. Re: Using queryMBean
                Dimitris Andreadis Master

                The point is those exception were never meant to be seen and it just happened that you came across them, only because of using QueryExp the wrong way :)

                Maybe open a "minior" JIRA case proposing a fix for the NPE, and possibly some trace logging for the exceptions messages (the full stack traces are not needed, I think), but again, I'm not sure if people will want to enable tracing of jmx query expression.

                • 5. Re: Using queryMBean
                  Dimitris Andreadis Master

                  So the "powerpoint" version of the conclusion is:

                  0) key/value pairs in ObjectNames are not related to mbean attribute values
                  1) Use ObjectName patterns to match a set of MBean ObjectNames
                  2) Use QueryExp to match specific attributes on those MBeans.