1
votes

I consider a web application deployed in Tomcat. Some MBean is registered and available through the JConsole.

When I call an operation on this MBean, it seems the ClassLoader of the RMI call is not specific to the WebApp which has registered the MBean. Given http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html, I guess it uses the ClassLoader of Bootstrap, System or Common.

How could I execute the RMI/MBean call with the WebApp classloader having registered the MBean?

2

2 Answers

1
votes

This has been workarounded by keep a referencing the ClassLoader which has instanciated the MBean:

    /** the classLoader to use for future usages */
protected final ClassLoader instanciatingClassLoader;

/** Default constructor */
public MyMonitoringBean() {
            // Keep in reference the classLoader used to instanciate this object
    this.instanciatingClassLoader = Thread.currentThread().getContextClassLoader();
}

protected ClassLoader getClassLoader() {
    return instanciatingClassLoader;
}

It does not solve this ticket since calls to this MBean are executed in a Thread with as ClassLoader not the WebApp ClassLoader, but specific calls to the class loader like:

    getClassLoader().loadClass(className)

can be resolved by using directly the webApp classloader

0
votes

The question is already somewhat hung, but I encountered the same problem just right now.

I observed, that the class loader of the MBean class is actually the class loader of your webapplication in which the MBean resides.

That is by attaching the class' class loader to the current thread your MBean should be able to access application classes:

    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());