JMX Newbie class loading question.

Hi I'm new-ish to JMX so I thought that this forum would be a good place to post this question.

I'm attempting to build a modular system using JMX, where the MBean server is started using a main(args[]) class.

My aim is to put all the custom mbeans in separate JAR files (in alib folder) that are loaded in by using URL classloaders.

The problem I'm having is with class loading.

My systems system starts something like this:

(1) The Main.java class reads in the command line parameters (it's started from a windows .BAT file)

(2) The Main.java class creates a ServerConfig object which contains all the command line params and additional configuration information

(3) I then create ServerBuilder object which reads in all the params from the ServerConfig.

The ServerBuilder object creates a new URLClassLoader which is used to create a Server object

(4) In the Server object i create the MBean Server using:

MBeanServerFactory.createMBeanServer(String name)

I then add some startup standard MBeans using server.registerMBean() whcih works fine! These objects are already instances in within the Server object

However when I make a call to server.createMBean(classname, null) i get the annoying error (the MBean implements the MBeanRegistration interface and methods):

java.lang.RuntimeException: javax.management.ReflectionException: The MBeanclass could not be loaded by thedefault loader repository

The MBean i'm trying to register is in a separate JAR file that is loaded in by the URLClassloader in ServerBuilder.

I'm using JDK1.5.0_08 and using the built in Sun JMX.

Could anyone please help with the following questions?

(1) How does class loading work in the MBeanServer?

(2) Why can I create an instance of the MBean but the MBeanServer cannot?

(3) Is there any way to find out what the classloader is referencing for the MBeanServer instance?

(4) How can you set the class loaders for a specific MBeanServer?

Phew...

Thanks

Martin

Could anyone please explain to me why the MBean server cannot see the

[2246 byte] By [martinspinksa] at [2007-10-3 5:30:11]
# 1

First of all, if registerMBean is enough for you then you should stick with it. createMBean introduces exactly the sort of problems you are seeing. It is only really useful if you need to call it remotely, when registerMBean is unavailable.

If you do need to support createMBean, then you'll want to have a look at Chapter 8, "Advanced Dynamic Loading", in the JMX specification at <http://download.java.net/jdk6/docs/technotes/guides/jmx/JMX_1_4_specification.pdf>. (This is the specification for Java SE 6, but this particular aspect hasn't changed since the previous version which is in J2SE 5.0.)

Basically you need to have your URLClassLoader be an MBean and register it in the MBeanServer. The quickest way to achieve this is to use the existing MLet class, which is a subclass of URLClassLoader. However, MLet brings along a whole bunch of functionality which you probably don't need, so I think a better approach would be to define your own ClassLoader MBean like this:

public interface MyClassLoaderMBean {}

public class MyClassLoader extends URLClassLoader

implements MyClassLoaderMBean {

public MyClassLoader(URL[] urls, ClassLoader parent) {

super(urls, parent);

}

}

Then you can either use MyClassLoader where you currently use URLClassLoader, or you can specify your existing URLClassLoader as the parent of the MyClassLoader and give MyClassLoader an empty set of URLs.

emcmanusa at 2007-7-14 23:37:39 > top of Java-index,Core,Monitoring & Management...
# 2
Hi sorry for taking so long to thank you for your response.I tried creating a new URL classloader MBean and this worked.Thank you so much!Martin
martin@realise.coma at 2007-7-14 23:37:39 > top of Java-index,Core,Monitoring & Management...
# 3

Please write how did you do it.

I've put something like that to the application.xml file:

<bean name="AppConsole:port=8481" class="javax.management.loading.MLet">

</bean>

So now I have this extra bean in the same MBeanServer on which I wanna execute instantiate method putting as an argument name of class (I have some mbeans of this class defined in the application.xml file already).

But I still get this exception:

javax.management.ReflectionException: The MBean class could not be loaded by the default loader repository

Please show me how have you done it.

Thanks in advance,

krutny

krutnya at 2007-7-14 23:37:39 > top of Java-index,Core,Monitoring & Management...