0 Replies Latest reply on Dec 10, 2002 3:27 PM by Andrew Sharpe

    How do you get the configuration for an MBean?

    Andrew Sharpe Newbie

      David Jencks was nice enough to help me solve exactly that problem and he asked me to post the solution in this FAQ.

      What I wanted to accomplish was to start JBoss, add mbeans through code (not deploying with service files), and then get their configuration (...). Here is how you would do this:

      // Get a reference to JBoss' mbeanserver.
      MBeanServer server = (MBeanServer) MBeanServerFactory.findMBeanServer(null).get(0);
      // Create a unique identifier for my new mbean.
      ObjectName myObjectName = new ObjectName("MyDomain:service=MyService");
      // Create and register the new mbean.
      server.createMBean(MyMBeanClass.class.getName(), myObjectName);
      ObjectName serviceController = new ObjectName("jboss.system:service=ServiceController");
      // Invoke the "create" method of the ServiceController on the new mbean. This was a critical step I had missed.
      server.invoke(serviceController, "create", new Object[]{myObjectName}, new String[]{"javax.management.ObjectName"});

      Now the mbean has been created and added to the JBoss MBean server so here's how you would get its configuration string:
      ObjectName[] mbeans = new ObjectName[]{myObjectName};
      // Invoke the service controller's "listconfiguration" method which builds the string.
      String config = (String) server.invoke(serviceController, "listConfiguration", new Object[]{mbeans}, new String[]{mbeans.getClass().getName()});

      Now config will be something like:


      value



      If you want the mbean to have any dependencies just invoke the other create method of the ServiceController, passing in the dependencies in the second parameter (Collection).

      This is all from memory so excuse any typos. I think this should be enough for anyone to solve the problem should they come across it. I should mention that this code was testing in JBoss 3.0.3 and will probably not work in JBoss 2.4.x.

      If anyone has any problems with it I invite them to email me at andrewrwsharpe@yahoo.com David was nice enough to help me with it so the least I can do is pass on the favour.