在 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都没有影响。

阅读全文……

你可能感兴趣的:(webservice)