ofbiz的服务(service)export成web service的方法(7)

xmlrpc调用可以使用压缩

xmlrpc的报文的冗余是很多的,大量的标签将会消耗掉你的带宽,所以对xmlrpc的返回的报文进行压缩很有必要的。以前写的代码:
  1. import java.net.MalformedURLException;
  2. import java.net.URL;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Iterator;
  6. import java.util.List;
  7. import org.apache.xmlrpc.XmlRpcException;
  8. import org.apache.xmlrpc.client.XmlRpcClient;
  9. import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
  10.     
  11. public class testFindPersonByFirstName {
  12.     /**
  13.      * @param args
  14.      * @throws MalformedURLException 
  15.      * @throws XmlRpcException 
  16.      */
  17.     public static void main(String[] args) throws MalformedURLException, XmlRpcException {
  18.         // TODO Auto-generated method stub
  19.         XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
  20.         config.setServerURL(new URL("http://127.0.0.1:18080/webtools/control/xmlrpc"));
  21.         XmlRpcClient client = new XmlRpcClient();
  22.         client.setConfig(config);
  23.         Object[] params = new Object[]{"John"};
  24.         HashMap result = (HashMap) client.execute("findPersonByfirstName", params);
  25.         
  26.         Object[] oz = (Object [])result.get("personList");
  27.         System.out.println("partyId firstName   lastName");
  28.         for(int i=0;i<oz.length;i++){
  29.             HashMap person = (HashMap)oz[i];
  30.             System.out.println(person.get("partyId")+"  "+person.get("firstName")+" "+person.get("lastName"));
  31.         }
  32.     }
  33. }

我们用TcpMon抓一下包,返回的数据:
  1. HTTP/1.1 200 OK
  2. Server: Apache-Coyote/1.1
  3. Set-Cookie: JSESSIONID=B5E7115DD5E2568D75AF6CD19B0DB2E4.jvm1; Path=/webtools
  4. Set-Cookie: OFBiz.Visitor=10311Expires=Sat, 28-Nov-2009 07:57:12 GMT; Path=/
  5. Content-Type: text/xml;charset=UTF-8
  6. Transfer-Encoding: chunked
  7. Date: Fri, 28 Nov 2008 07:57:12 GMT
  8. 1011
  9. <?xml version="1.0" encoding="UTF-8"?><methodResponse><params><param><value><struct><member><name>personList</name><value><array><data><value><struct><member><name>partyId</name><value>10000</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10001</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10002</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10003</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10004</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10010</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10020</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10030</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10040</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10050</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10060</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10070</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10071</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10072</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10080</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10081</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10082</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10090</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value><value><struct><member><name>partyId</name><value>10100</value></member><member><name>firstName</name><value>John</value></member><member><name>lastName</name><value>Tower</value></member></struct></value></data></array></value></member></struct></value></param></params></methodResponse>
  10. 0
恐怖吧!!!!数据长度为1011。

我们修改一下代码,增加一行:
  1. config.setGzipRequesting(true);
再试试,数据返回成功,但是返回部分已经用gzip压缩过了,可惜的是tcpmon看不到压缩后有多长,是107吗?不清楚,但是从gzip的算法来看,上述的报文的压缩率应该是很高的,因为有太多的冗余
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值