我如何添加jboss 7.1模块,该模块包含实现/扩展服务器主ear文件中的类的类? [英] How can i add a jboss 7.1 module that contain classes that implements/extends from classes in the main ear file of the server?

查看:136
本文介绍了我如何添加jboss 7.1模块,该模块包含实现/扩展服务器主ear文件中的类的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带耳文件的JBoss服务器.我的耳朵锉里有一个战争锉. war文件具有一个jar文件"server-artifact.jar".我的服务器的服务端点在那个jar中. jar文件中的类会动态加载一个类以执行操作.

I have a JBoss server that has an ear file. My ear file has a war file. The war file has a jar file "server-artifact.jar". My server's service endpoint is in that jar. The class in the jar file loads a class dynamically to perform an action.

Class<?> clazz = (Class<?>) Class.forName("com.test.TestExternalAccess");
try {
  TestExternalAccessParent extClassObject = (TestExternalAccessParent) clazz.newInstance();
  extClassObject.sayHelloToExternalAccess();
} catch (InstantiationException | IllegalAccessException e) {
  e.printStackTrace();
}

包含"TestExternalAccessParent"(它是一个接口)的jar文件也是war文件的一部分.类"TestExternalAccess"是一个具体的类,旨在作为服务器的可插拔单元.为了实现这一目标,我创建了一个jboss模块并将其放在modules文件夹中(如何?):

The jar file that contains "TestExternalAccessParent" which is an interface, is part of the war file too. The class "TestExternalAccess" is a concrete class that is meant to be a pluggable unit for my server. In order to achieve this, i created a jboss module and put it in the modules folder (how?):

<module xmlns="urn:jboss:module:1.1" name="com.test">
  <resources>
    <resource-root path="externalLibrary-0.0.1-SNAPSHOT.jar"/>
  </resources>
</module>

我还编辑了jboss-deployment-structure.xml并添加了依赖项 <module name="com.test" /> 我启动服务器并运行它.当动态加载类时发生以下异常: java.lang.ClassNotFoundException: com.test.TestExternalAccess from [Module "deployment.myservice-ear.ear:main" from Service Module Loader]

I also edited the jboss-deployment-structure.xml and added the dependency <module name="com.test" /> I start my server and run it. I get the following exception when the dynamic loading of the class happens : java.lang.ClassNotFoundException: com.test.TestExternalAccess from [Module "deployment.myservice-ear.ear:main" from Service Module Loader]

我尝试过的一些事情: 1)尝试从外部模块加载一个类,该类未在主ear文件的jar文件中实现接口,并且可以正常工作. 2)更改了我的模块,使其包含包含接口的jar文件.

Some things i have tried: 1) Tried loading a class from the external module that does not implement an interface in the jar file of the main ear file and that works fine. 2) Changed my module to include the jar file that contains the interface.

<module xmlns="urn:jboss:module:1.1" name="com.test">
  <resources>
    <resource-root path="externalLibrary-0.0.1-SNAPSHOT.jar"/>
    <resource-root path="externalParentLibrary-0.0.1-SNAPSHOT.jar"/>
  </resources>
</module>

那也很好. 3)向我的模块添加了以下依赖性:

That works fine too. 3) Added the following dependency to my module:

<dependencies>
  <module name="deployment.myservice-ear.ear"/>
</dependencies>

那是行不通的.

加载我的耳朵的类加载器使用另一个类加载器加载我的外部模块并获得对该模块中类的访问权限.但是我的外部模块中的类似乎无法访问耳朵中的罐子. 我怎样才能做到这一点?我想添加可以访问服务器的ear文件类中的类的外部库模块.

The classloader that loaded my ear uses another classloader to load my external module and gain access to the classes in that module. But the classes in my external module cannot seem to be able to access the jars in the ear. How can i make this happen? I want to add external library modules that has access to classes in my server's ear file classes.

推荐答案

我找到了解决问题的方法.在使用jboss-deployment-structure.xml和模块玩了一段时间后,我意识到无法使外部模块使用或扩展耳朵/战争"类. 为了实现我的目标,我必须使我的jar文件在外部位置,由加载耳朵库的同一类加载器加载.可以通过将资源直接添加到ear文件中来实现,如下所示(jboss-deployment-structure.xml):

I found a solution to my problem. After playing around for a while with the jboss-deployment-structure.xml and modules, i realized that it is not possible to make an external module use or extend classes of the ear/war. To achieve my goal, i had to make my jar file which is at an external location be loaded by the same class loader that loads the ear libraries. This can be achieved by adding a resource directly to the ear file as below(jboss-deployment-structure.xml):

<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure xmlns="urn:jboss:deployment-structure:1.2">
<ear-subdeployments-isolated>false</ear-subdeployments-isolated>
<deployment>
    <dependencies>
        <module name="org.jboss.as.jmx"/>
        <module name="org.jboss.logmanager"/>
    </dependencies>
<resources>
    <resource-root path="../../../../../../../../../externalLib/externalLibrary-0.0.1-SNAPSHOT.jar" /> 
</resources>
</deployment>

<sub-deployment name="myservice.war">
</sub-deployment>
<sub-deployment name="admin.war">
</sub-deployment>   

资源根路径是相对于standalone/deployments文件夹中的ear文件的.以这种方式添加资源根目录等效于将库添加到ear文件的lib文件夹中.因此,这可以确保将我的外部库也作为主类加载器的一部分加载,该主类加载器将我的所有框架库都放在耳朵上.

The resource-root path is relative to the ear file in the standalone/deployments folder. Adding the resource root as this way is equivalent to adding the library to the lib folder of the ear file. So, this ensures that my external library is also loaded as part of the the main classloader that loads the ear where all my framework libraries are.

这篇关于我如何添加jboss 7.1模块,该模块包含实现/扩展服务器主ear文件中的类的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆