【问题标题】:How to access a specific WebApp ClassLoader from a MBean/RMI class如何从 MBean/RMI 类访问特定的 WebApp 类加载器
【发布时间】:2013-08-03 13:29:33
【问题描述】:

我考虑部署在 Tomcat 中的 Web 应用程序。一些 MBean 已通过 JConsole 注册并可用。

当我在此 MBean 上调用操作时,似乎 RMI 调用的 ClassLoader 并不特定于已注册 MBean 的 WebApp。给定http://tomcat.apache.org/tomcat-7.0-doc/class-loader-howto.html,我猜它使用了Bootstrap、System或Common的ClassLoader。

如何使用已注册 MBean 的 WebApp 类加载器执行 RMI/MBean 调用?

【问题讨论】:

    标签: tomcat classloader


    【解决方案1】:

    通过保持引用已实例化 MBean 的 ClassLoader 已解决此问题:

        /** 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;
    }
    

    它不能解决这个问题,因为对这个 MBean 的调用是在一个线程中执行的,其中 ClassLoader 不是 WebApp ClassLoader,而是对类加载器的特定调用,例如:

        getClassLoader().loadClass(className)
    

    可以直接使用webApp类加载器来解决

    【讨论】:

      【解决方案2】:

      问题已经有点悬了,但我刚刚遇到了同样的问题。

      我观察到,MBean 类的类加载器实际上是 MBean 所在的 Web 应用程序的类加载器。

      也就是说,通过将类的类加载器附加到当前线程,您的 MBean 应该能够访问应用程序类:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-04-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-02-10
        • 1970-01-01
        相关资源
        最近更新 更多