How can i add a jboss 7.1 module that contain clas

2020-04-12 09:36发布

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();
}

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>

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]

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>

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

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

That DOES NOT work.

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.

1条回答
做自己的国王
2楼-- · 2020-04-12 09:58

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>   

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.

查看更多
登录 后发表回答