<< 2013年9月17日 | 首页 | 2013年9月19日 >>

在 xfire中使用 GZip来 压缩 传输量 - secyaher的日志 - 网易博客

在XFire1.2以后支持SOAP消息的GZip压缩传输,在合适的地方启动GZip压缩可以减少网络传输的数据量,加快速度。

在XFire中启动GZip压缩要用到一个开源的Filter:PJL Compressing Filter。这个Filter的最新版本是1.6.4,自1.5.0开始该工程开始构建于JDK5.0,因此在JDK1.4环境下只能使用1.4.6(这个版本会与高版本同步更新)。[http://sourceforge.net/projects/pjl-comp-filter/]

一、服务端启动GZip:在服务端启动GZip只需将PJL Compressing Filter下的jar包(用到的)导入到web的/WEB-INF/lib目录下。并在web.xml文件中增加如下配置:

       <filter>

              <filter-name>CompressingFilter</filter-name>

              <filter-class>

                     com.planetj.servlet.filter.compression.CompressingFilter

              </filter-class>

              <init-param>

                     <param-name>debug</param-name>

                     <param-value>false</param-value>

              </init-param>

              <init-param>

                     <param-name>statsEnabled</param-name>

                     <param-value>true</param-value>

              </init-param>

       </filter>

       <filter-mapping>

              <filter-name>CompressingFilter</filter-name>

              <url-pattern>/*</url-pattern>

       </filter-mapping>

代码清单7-1:服务端加入GZip时web.xml中增加的配置

注:根据web.xml文法定义,这个定义必须位于servlet定义之前。

二、客户端启用GZip:客户端启动GZip只要将客户端的GZip的属性设为true却可。代码如下:

              String serviceURL = "http://192.168.0.225/ldaxfire/services/LDAService";

 

              // 创建service对象

              Service serviceModel = new ObjectServiceFactory().create(

                            ServiceInterface.class, null, "http://test.yicha.cn/adreport", null);

 

              XFireProxyFactory serviceFactory = new XFireProxyFactory();

 

              // 获取服务对象

              ServiceInterface service = (ServiceInterface) serviceFactory.create(

                            serviceModel, serviceURL);

 

              // 获取客户端代理

              Client client = ((XFireProxy) Proxy.getInvocationHandler(service))

                            .getClient();

 

              // 启动response压缩

              client.setProperty(CommonsHttpMessageSender.

                            GZIP_RESPONSE_ENABLED, Boolean.TRUE);

 

              // 启动request压缩

              client.setProperty(CommonsHttpMessageSender.

                            GZIP_RESPONSE_ENABLED,Boolean.TRUE);

 

              // 同时启动response和request压缩

              client.setProperty(CommonsHttpMessageSender.GZIP_ENABLED,

                            Boolean.TRUE);

 

              // 忽略超时

              client.setProperty(CommonsHttpMessageSender.HTTP_TIMEOUT, "0");

 

              // 调用服务

              data = service.serviceMethod();

代码清单7-2:客户端调用的修改

注:在同时启用时,不必再分别启用response和request的GZip压缩。

注意,当服务端没有启用GZip,客户端启用请求GZip压缩时,会产生SOAP解析错误,如果服务端启动了GZip压缩功能,客户端是否启用GZip都没有影响。

阅读全文……

标签 :

How to Use SOAP Compression with Apache Axis 1.X

Configuring the Server

The configuration of the client and the server are independent. First, you can set up and test the server. After the server configuration is working you can move on to the client side.

We will use a Servlet filter to decompress the requests and compress the responses. Although a lot of other filters can only compress the response, our free filter can both compress the response and decompress the request. To use the filter for SOAP compression, it has to be added to the Axis Web application containing the Web Services. The filter is packaged into the library 2wayfilter-1.2.jar. Download the library and copy it into the Axis Web application's lib folder.

Now, you can configure the Web application to use the filter. Include the following filter and filter-mapping elements in the Web application's deployment descriptor. The deployment descriptor is in the file web.xml in the folder WEB-INF.

<web-app>     ...     <filter>         <filter-name>2WayFilter</filter-name>         <filter-class>com.osmoticweb.gzipfilter.GZIP2WayFilter</filter-class>     </filter>     <filter-mapping>         <filter-name>2WayFilter</filter-name>         <url-pattern>/*</url-pattern>     </filter-mapping>     ... </web-app> 

Configuring the Client

To use the compression feature of the server, the client must be able to compress requests and decompress responses. Apache Axis can be extended with the SOAP compression transport sender which supports compression and decompression of SOAP messages. The extension can be installed without recompiling or modifying Axis itself.

Download the library soap-compression-1.2.jar containing the extension and place it into the classpath of the Axis client. The library contains the classcom.osmoticweb.soapcompression.CompressionHTTPSender which can be used instead of the built-in sender. To instruct the Axis client to use the new class a client side deployment descriptor is necessary. Create a file named client-config.wsdd, with the following content, and place it in the directory where the Axis client is started. See the Axis WSDD Reference for more details about the deployment descriptor.

The compression sender is compatible with Axis 1.1 or newer. The configuration is different depending on which version of Axis is used. The difference lies in how parameters are declared in the file client-config.wsdd. Use the corresponding configuration for your Axis version. For details see the SOAP compression sender description.

Axis 1.2 Beta or Newer

       
<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig"
            xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

    <transport name="http" pivot="java:com.osmoticweb.soapcompression.CompressionHTTPSender">          <parameter name="compressRequest" value="true"/>          <parameter name="acceptEncodingHeader" value="true"/>      </transport>

    <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender"/>
    <transport name="java" pivot="java:org.apache.axis.transport.java.JavaSender"/>
 
</deployment>

Axis 1.1

<?xml version="1.0" encoding="UTF-8"?>
<deployment name="defaultClientConfig"
            xmlns="http://xml.apache.org/axis/wsdd/"
            xmlns:java="http://xml.apache.org/axis/wsdd/providers/java">

    <transport name="http" pivot="java:com.osmoticweb.soapcompression.CompressionHTTPSender">         <requestFlow>              <handler type="java:com.osmoticweb.soapcompression.PutOptionsHandler">                  <parameter name="compressRequest" value="true"/>                  <parameter name="acceptEncodingHeader" value="true"/>              </handler>          </requestFlow>     </transport>

    <transport name="local" pivot="java:org.apache.axis.transport.local.LocalSender"/>
    <transport name="java" pivot="java:org.apache.axis.transport.java.JavaSender"/>
  
</deployment>

java org.apache.axis.utils.tcpmon

After starting tcpmon, a new session can be added to intercept our calls. For the test, we will use listen port 2000. The session will act as a listener for the target host 127.0.0.1 on target port8080. See the screenshot below.

Now the test page can be requested from a Web browser that supports HTTP compression, for example, Mozilla, Firefox and Microsoft Internet Explorer.

 

阅读全文……

标签 :