2016年3月18日星期五

EAP_050:加载定制的 org.apache.httpcomponents module

环境:OS X EI Capitan 10.11.3 + JBoss EAP 6.4.0

1. 为了能够看到类的加载路径,修改 standalone.conf,增加 -verbose:class 参数
JAVA_OPTS="-Xms1303m -Xmx1303m -XX:MaxPermSize=256m -Djava.net.preferIPv4Stack=true -verbose:class"

2. 修改 index.jsp,修改后内容如下:
<html>
  <head>
    <title>JBoss Module Testing
    </title>
  </head>
  <body>

    <%
     org.apache.http.impl.client.DefaultHttpClient client = new org.apache.http.impl.client.DefaultHttpClient();
   
System.out.println("### The class of " + client + " is " + client.getClass().getName());
System.out.println("### Its classloader is " + client.getClass().getClassLoader());
out.println("### The class of " + client + " is " + client.getClass().getName());
out.println("### Its classloader is " + client.getClass().getClassLoader());
    %>
    Hi Check the Console/Log of your JBoss
  </body>
</html>

3. 有两种方式可以加载定制的org.apache.httpcomponents module

3.1. 方式一:以 module 的方式增加
(1)修改 jboss-deployment-structure.xml,修改后内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
   <deployment>
       <exclusions>
           <module name="org.apache.httpcomponents" />
       </exclusions>
       <dependencies>
           <module name="org.apache.httpcomponents" slot="4.4.1"/>
       </dependencies>
   </deployment>
</jboss-deployment-structure>
(2)在 EAP_HOME/modules/system/layers/base/org/apache/httpcomponents/ 目录下创建 4.4.1 目录,并把 httpclient-4.4.1.jar 和 httpcore-4.4.1.jar 复制到该目录下。
创建一个 module.xml 文件,内容如下:
<module xmlns="urn:jboss:module:1.1" name="org.apache.httpcomponents" slot="4.4.1">
    <properties>
        <property name="jboss.api" value="private"/>
    </properties>

    <resources>
        <resource-root path="httpclient-4.4.1.jar"/>
        <resource-root path="httpcore-4.4.1.jar"/>
        <resource-root path="httpmime-4.3.6.redhat-1.jar"/>
        <!-- Insert resources here -->
    </resources>

    <dependencies>
        <module name="javax.api"/>
        <module name="org.apache.commons.codec"/>
        <module name="org.apache.commons.logging"/>
        <module name="org.apache.james.mime4j"/>
    </dependencies>
</module>
(3)重新打包部署
cd /Users/maping/mygit/CustomModuleTest/my-webapp
mvn clean package
(4)访问 http://localhost:8080/my-webapp/index.jsp
在 Console 中会看到如下输出:
[Loaded org.apache.http.client.HttpClient from jar:file:/Users/maping/Redhat/eap/jboss-eap-6.4/modules/system/layers/base/org/apache/httpcomponents/4.4.1/httpclient-4.4.1.jar!/]
[Loaded org.apache.http.impl.client.CloseableHttpClient from jar:file:/Users/maping/Redhat/eap/jboss-eap-6.4/modules/system/layers/base/org/apache/httpcomponents/4.4.1/httpclient-4.4.1.jar!/]
[Loaded org.apache.http.impl.client.AbstractHttpClient from jar:file:/Users/maping/Redhat/eap/jboss-eap-6.4/modules/system/layers/base/org/apache/httpcomponents/4.4.1/httpclient-4.4.1.jar!/]
[Loaded org.apache.http.impl.client.DefaultHttpClient from jar:file:/Users/maping/Redhat/eap/jboss-eap-6.4/modules/system/layers/base/org/apache/httpcomponents/4.4.1/httpclient-4.4.1.jar!/]
说明是从 EAP_HOME/modules/system/layers/base/org/apache/httpcomponents/4.4.1/httpclient-4.4.1.jar 加载到 org.apache.http.client.HttpClient 等类的。

3.2. 方式二:直接添加在 WEB-INF/lib 目录下
(1)修改 jboss-deployment-structure.xml,修改后内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<jboss-deployment-structure>
   <deployment>
       <exclusions>
           <module name="org.apache.httpcomponents" />
       </exclusions>    
   </deployment>
</jboss-deployment-structure>
(2)在 WEB-INF 目录下增加 lib 目录,并把 httpclient-4.4.1.jar 和 httpcore-4.4.1.jar 复制到该目录下。
(3)重新打包部署
cd /Users/maping/mygit/CustomModuleTest/my-webapp
mvn clean package
(4)访问 http://localhost:8080/my-webapp/index.jsp
在 Console 中会看到如下输出:
[Loaded org.apache.http.client.HttpClient from vfs:/content/my-webapp.war/WEB-INF/lib/httpclient-4.4.1.jar]
[Loaded org.apache.http.impl.client.CloseableHttpClient from vfs:/content/my-webapp.war/WEB-INF/lib/httpclient-4.4.1.jar]
[Loaded org.apache.http.impl.client.AbstractHttpClient from vfs:/content/my-webapp.war/WEB-INF/lib/httpclient-4.4.1.jar]
[Loaded org.apache.http.impl.client.DefaultHttpClient from vfs:/content/my-webapp.war/WEB-INF/lib/httpclient-4.4.1.jar]
[Loaded org.apache.http.client.ClientProtocolException from vfs:/content/my-webapp.war/WEB-INF/lib/httpclient-4.4.1.jar]
说明是从 WEB-INF/lib/httpclient-4.4.1.jar 加载到 org.apache.http.client.HttpClient 等类的。

没有评论: