简体   繁体   English

如何将Java类从一个Web容器加载到另一个Web容器?

[英]How to load a java class from one web container to another?

I have an application running in a jetty container where a custom class loader, will load classes from /SHARED/< jar files>.. Now I want any of these loaded classes to use another classes (WEB-INF/libs) from applications running in another jetty container at run time. 我有一个运行在码头容器中的应用程序,其中有一个自定义类加载器,将从/ SHARED / <jar文件>加载类。现在,我希望这些加载的类中的任何一个都可以使用正在运行的应用程序中的另一个类(WEB-INF / libs)在运行时放入另一个码头容器中。 As of now, am getting a class not found exception. 截至目前,正在获取未找到的类异常。

Web Container 1: Web容器1:

Jar location: /ci/Shared/< jars> To load the above jars, we have a customer class loader called SharedJarsClassLoader.. 罐子位置:/ ci / Shared / <罐子>要加载上述罐子,我们有一个名为SharedJarsClassLoader。的客户类加载器。

Web Container 2: Web容器2:

App_1/WEB-INF/libs App_2/WEB-INF/libs App_1 / WEB-INF / libs App_2 / WEB-INF / libs

My requirement is to supports for the classes in /ci/Shared/< jars> in container 1 to loaded classes from app1 & app2 jars from WEB-INF/libs.. 我的要求是支持容器1中的/ ci / Shared / <jars>中的类,以支持从WEB-INF / libs的app1和app2 jars中加载的类。

So of now, when the app starts, its shows class not found exception. 如此看来,当应用启动时,其show类未找到异常。 App is not able to load the classes from App_1/WEB-INF/libs & App_2/WEB-INF/libs. App无法从App_1 / WEB-INF / libs和App_2 / WEB-INF / libs加载类。

This is the class loader hierarchy.. 这是类加载器的层次结构。

sun.misc.Launcher$ExtClassLoader@4921a90                                         (Extensions class loader)
  sun.misc.Launcher$AppClassLoader@3da997a                                       (System class loader)
    startJarLoader@68758d51                                                      (Jetty class loader)
      com.test.jetty_ext.NewWebAppProvider$SharedJarsClassLoader@374d1cbd        (Shared jars class loader)
        WebAppClassLoader=settings@2fee2019                                      (Webapp class loader)
        WebAppClassLoader=taskt@6a57cf0b                                         (2nd Webapp class loader)

Here is the stack trace.. 这是堆栈跟踪。

Caused by: java.lang.ClassNotFoundException: com.test.ClassAInContainer3
    at java.net.URLClassLoader$1.run(URLClassLoader.java:366) ~[na:1.7.0_55]
    at java.net.URLClassLoader$1.run(URLClassLoader.java:355) ~[na:1.7.0_55]
    at java.security.AccessController.doPrivileged(Native Method) ~[na:1.7.0_55]
    at java.net.URLClassLoader.findClass(URLClassLoader.java:354) ~[na:1.7.0_55]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:425) ~[na:1.7.0_55]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ~[na:1.7.0_55]
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:258) ~[spring-core.jar:3.2.0.RELEASE]
    at org.springframework.amqp.support.converter.DefaultJavaTypeMapper.getClassIdType(DefaultJavaTypeMapper.java:82) ~[spring-amqp.jar:na]
    ... 16 common frames omitted
2015-08-19 05:38:16.123 UTC,WARN ,App1-web,messaging,com.test.Rabbit,null,SimpleAsyncTaskExecutor-1,Caught an exception while handling a rabbitmq message, Publisher/Consumer should have handled this.
org.springframework.amqp.support.converter.MessageConversionException: failed to resolve class name. Class not found [com.test.ClassAInContainer3]
    at org.springframework.amqp.support.converter.DefaultJavaTypeMapper.getClassIdType(DefaultJavaTypeMapper.java:85) ~[spring-amqp.jar:na]
    at org.springframework.amqp.support.converter.DefaultJavaTypeMapper.toJavaType(DefaultJavaTypeMapper.java:53) ~[spring-amqp.jar:na]
    at org.springframework.amqp.support.converter.JsonMessageConverter.fromMessage(JsonMessageConverter.java:117) ~[spring-amqp.jar:na]

Any inputs on how to resolve this issue? 关于如何解决此问题的任何意见?

To be able to do that, your custom class loader (CCL) must be a child of the web app classloader. 为此,您的自定义类加载器(CCL)必须是Web应用程序类加载器的子级。 The reason is that a classloader only ever asks it's parent for missing classes. 原因是一个类加载器只向其父请求缺少的类。 In your case, it's the other way around. 就您而言,这是另一回事。 That means: 这意味着:

  • app code accesses a custom class via the CCL. 应用程序代码通过CCL访问自定义类。 This works since the CCL is the parent of your app's class loader. 由于CCL是应用程序的类加载器的父级,因此可以使用。
  • The custom class now tries to discover another app class. 现在,自定义类尝试发现另一个应用程序类。 It asks CCL which doesn't know about is. 它询问不知道是什么的CCL。 Then CCL asks parents which also has no idea -> doom. 然后,CCL询问父母哪一个也不知道->厄运。

If you flip this upside down, custom classes will be able to access app code. 如果您颠倒过来,自定义类将能够访问应用程序代码。 Now you will have a problem to access the custom classes. 现在,您将无法访问自定义类。 In order to solve that, you must pass the CCL reference around and manually load types via the classloader API using the CCL and instantiate the type you need. 为了解决这个问题,您必须传递CCL参考,并使用CCL通过类加载器API手动加载类型,并实例化所需的类型。 You can't have Java "automatically" discover this classes. 您不能让Java“自动”发现此类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从另一个Java类加载一个? - How do I load one Java class from another? 如何在Java中动态运行时将.class文件从一个项目(项目2)加载到另一个Web项目(项目3)? - How can I load .class files from one project(project 2) to another web project(Project 3) at dynamically runtime in java? 加载实现另一个类的类-Java - Load a class which implements another one - Java 从Java类中依次调用url以进行登录,并从另一个进行web服务调用 - calling url from java class one after another one for logging in and another for web service call 如何从android中的另一个应用程序加载类 - how to load class from one another application in android 从Java Web Start中的同一jar内的另一个类加载类失败 - load class from another class within same jar in java web start fails 如何将Java对象从一个类传递到另一个类 - How to pass Java objects from one class to another class 如何将一种方法从一个 class 迁移到另一个? (安卓、Java) - How to migrate one method from one class to another? (Android, Java) 如何将对象从一类转移到另一类-JAVA - How to transfer object from one class to another class - JAVA 在Tomcat容器中让一个Java Web应用程序与另一个Java Web应用程序对话 - Have one Java web app talk to another in a Tomcat container
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM