[英]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:
这意味着:
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.