<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • 如何壓縮Web Service數據

    時間:2024-07-15 09:39:16 J2EE培訓 我要投稿
    • 相關推薦

    如何壓縮Web Service數據

      在現實應用中有些時候會有比較大的數據對象需要傳輸,或者在一個比較慢的網絡環境下發布調用web service,此時可以通過壓縮數據流的方式來減小數據包的大小,從而提高web service的性能。下面yjbys小編為大家準備了關于如何壓縮Web Service數據的文章,歡迎閱讀。

      1. 首先模擬一個可以存放大數據的pojo對象,這個對象可以通過構造參數給定的size來模擬一個size大小的字符串。

      package com.googlecode.garbagecan.cxfstudy.compress;

      public class BigData {

      private String name;

      private String data;

      public BigData() {

      }

      public BigData(String name, int size) {

      this.name = name;

      StringBuilder sb = new StringBuilder();

      for (int i = 0; i < size; i++) {

      sb.append("0");

      }

      this.data = sb.toString();

      }

      public String getName() {

      return name;

      }

      public void setName(String name) {

      this.name = name;

      }

      public String getData() {

      return data;

      }

      public void setData(String data) {

      this.data = data;

      }

      }

      2. Web Service接口類,和普通的接口定義沒有什么區別。

      package com.googlecode.garbagecan.cxfstudy.compress;

      import javax.jws.WebMethod;

      import javax.jws.WebParam;

      import javax.jws.WebResult;

      import javax.jws.WebService;

      @WebService

      public interface BigDataService {

      @WebMethod

      @WebResult BigData getBigData(@WebParam String name, @WebParam int size);

      }

      3. Web Service實現類

      package com.googlecode.garbagecan.cxfstudy.compress;

      public class BigDataServiceImpl implements BigDataService {

      public BigData getBigData(String name, int size) {

      BigData bigData = new BigData(name, size);

      return bigData;

      }

      }

      4. 測試類,這片文章使用了JUnit測試類來做測試。setUpBeforeClass方法用來啟動Service, testGetBigData方法用來測試web service。

      注意setUpBeforeClass方法中的

      factoryBean.getInInterceptors().add(new GZIPInInterceptor());

      factoryBean.getOutInterceptors().add(new GZIPOutInterceptor());

      和testGetBigData方法中的

      endpoint.getInInterceptors().add(new GZIPInInterceptor());

      endpoint.getOutInterceptors().add(new GZIPOutInterceptor());

      上面兩段代碼就是告訴CXF使用壓縮Interceptor來壓縮和解壓縮數據包。

      package com.googlecode.garbagecan.cxfstudy.compress;

      import org.apache.cxf.endpoint.Client;

      import org.apache.cxf.endpoint.Endpoint;

      import org.apache.cxf.frontend.ClientProxy;

      import org.apache.cxf.interceptor.LoggingInInterceptor;

      import org.apache.cxf.interceptor.LoggingOutInterceptor;

      import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

      import org.apache.cxf.jaxws.JaxWsServerFactoryBean;

      import org.apache.cxf.transport.http.gzip.GZIPInInterceptor;

      import org.apache.cxf.transport.http.gzip.GZIPOutInterceptor;

      import org.junit.Assert;

      import org.junit.BeforeClass;

      import org.junit.Test;

      public class BigDataServiceTest {

      private static final String address = "http://localhost:9000/ws/compress/bigDataService";

      @BeforeClass

      public static void setUpBeforeClass() throws Exception {

      JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();

      factoryBean.getInInterceptors().add(new LoggingInInterceptor());

      factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());

      factoryBean.getInInterceptors().add(new GZIPInInterceptor());

      factoryBean.getOutInterceptors().add(new GZIPOutInterceptor());

      factoryBean.setServiceClass(BigDataServiceImpl.class);

      factoryBean.setAddress(address);

      factoryBean.create();

      }

      @Test

      public void testGetBigData() {

      JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();

      factoryBean.setAddress(address);

      factoryBean.setServiceClass(BigDataService.class);

      Object obj = factoryBean.create();

      Client client = ClientProxy.getClient(obj);

      Endpoint endpoint = client.getEndpoint();

      endpoint.getInInterceptors().add(new GZIPInInterceptor());

      endpoint.getOutInterceptors().add(new GZIPOutInterceptor());

      BigDataService service = (BigDataService) obj;

      Assert.assertNotNull(service);

      String name = "my big data";

      int size = 1024 * 1024 * 10;

      long start = System.currentTimeMillis();

      BigData bigData = service.getBigData(name, size);

      long stop = System.currentTimeMillis();

      System.out.println("Time: " + (stop - start));

      Assert.assertNotNull(bigData);

      Assert.assertEquals(name, bigData.getName());

      Assert.assertEquals(size, bigData.getData().length());

      }

      }

      5. 運行此unit test,可以在日志中看到數據包前后大小和內容。

    【如何壓縮Web Service數據】相關文章:

    Web Service的開發與應用基礎07-12

    web數據挖掘技術分析與研究09-06

    Web數據挖掘技術探析論文10-22

    如何面試Web前端開發10-10

    Flash Web結構之數據顯示層介紹07-27

    如何由淺入深實踐學習 Web 標準10-10

    如何識別和防御Web網頁木馬09-11

    如何保證Web服務器安全06-30

    Excel讓如何數據按需排序10-01

    Excel如何橫向輸入數據07-28

    主站蜘蛛池模板: 精品国产免费一区二区三区| 无码少妇精品一区二区免费动态 | 999久久久无码国产精品| 国产亚洲精品线观看动态图| 欧美精品国产一区二区| 久久亚洲精精品中文字幕| 日韩亚洲精品福利 | 久久婷婷国产综合精品| 欧美日韩精品久久久久| 国产精品成| 欧美极品欧美精品欧美视频| 99久久这里只有精品| 精品国产一区AV天美传媒| 小辣椒福利视频精品导航| 青青热久久国产久精品 | 精品国内自产拍在线观看| 亚洲综合精品一二三区在线| 国产精品天天影视久久综合网| 久久精品国产亚洲AV麻豆网站| 真实国产乱子伦精品视频| 中文字幕精品久久| 一本一本久久A久久综合精品| 日韩精品无码久久一区二区三| 麻豆精品视频在线观看91| 久久久精品无码专区不卡| 国产综合成人色产三级高清在线精品发布| 国产精品伦理久久久久久| 成人午夜精品亚洲日韩| 91精品国产高清久久久久久国产嫩草 | 中国精品18videosex性中国| 亚洲精品99久久久久中文字幕| 久久露脸国产精品| 午夜成人精品福利网站在线观看| 日本一区二区三区精品国产| 日韩精品一区二区三区中文字幕| 亚洲AV无码之日韩精品| 亚洲精品高清无码视频| 久久久精品2019免费观看| 国产欧美亚洲精品A| 国产成人亚洲合集青青草原精品 | 精品一区二区无码AV|