<dfn id="w48us"></dfn><ul id="w48us"></ul>
  • <ul id="w48us"></ul>
  • <del id="w48us"></del>
    <ul id="w48us"></ul>
  • 如何壓縮Web Service數(shù)據(jù)

    時間:2024-07-15 09:39:16 J2EE培訓(xùn) 我要投稿
    • 相關(guān)推薦

    如何壓縮Web Service數(shù)據(jù)

      在現(xiàn)實應(yīng)用中有些時候會有比較大的數(shù)據(jù)對象需要傳輸,或者在一個比較慢的網(wǎng)絡(luò)環(huán)境下發(fā)布調(diào)用web service,此時可以通過壓縮數(shù)據(jù)流的方式來減小數(shù)據(jù)包的大小,從而提高web service的性能。下面yjbys小編為大家準(zhǔn)備了關(guān)于如何壓縮Web Service數(shù)據(jù)的文章,歡迎閱讀。

      1. 首先模擬一個可以存放大數(shù)據(jù)的pojo對象,這個對象可以通過構(gòu)造參數(shù)給定的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接口類,和普通的接口定義沒有什么區(qū)別。

      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實現(xiàn)類

      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來壓縮和解壓縮數(shù)據(jù)包。

      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,可以在日志中看到數(shù)據(jù)包前后大小和內(nèi)容。

    【如何壓縮Web Service數(shù)據(jù)】相關(guān)文章:

    Web Service的開發(fā)與應(yīng)用基礎(chǔ)07-12

    web數(shù)據(jù)挖掘技術(shù)分析與研究09-06

    Web數(shù)據(jù)挖掘技術(shù)探析論文10-22

    如何面試Web前端開發(fā)10-10

    Flash Web結(jié)構(gòu)之?dāng)?shù)據(jù)顯示層介紹07-27

    如何由淺入深實踐學(xué)習(xí) Web 標(biāo)準(zhǔn)10-10

    如何識別和防御Web網(wǎng)頁木馬09-11

    如何保證Web服務(wù)器安全06-30

    Excel讓如何數(shù)據(jù)按需排序10-01

    Excel如何橫向輸入數(shù)據(jù)07-28

    主站蜘蛛池模板: 国产精品无码AV一区二区三区| 亚洲精品国产精品乱码视色| 亚洲欧美一级久久精品| 久久精品这里热有精品| 久久99热只有频精品8| 久久精品无码专区免费| 97精品伊人久久大香线蕉app| 日产国产精品亚洲系列| 亚洲精品国产成人专区| 999国内精品永久免费视频| 亚洲精品国精品久久99热一| 青春草无码精品视频在线观| 国产精品免费大片一区二区| 欧美精品1区2区| 98视频精品全部国产| 亚洲精品国产电影| 精品人伦一区二区三区潘金莲| 日韩精品一区二区三区在线观看| 99久免费精品视频在线观看| 久久97精品久久久久久久不卡| 国产精品网站在线观看免费传媒 | 国产欧美日韩精品a在线观看 | 国产精品久久久久久久| 日产精品99久久久久久| 在线精品动漫一区二区无广告| 久久93精品国产91久久综合| 国产精品热久久毛片| 91po国产在线精品免费观看| 国产精品视频分类一区| 99爱在线精品免费观看| 人妻少妇偷人精品无码| 乱精品一区字幕二区| 久久免费的精品国产V∧| 久久久无码人妻精品无码| 久久精品无码一区二区无码 | 亚洲精品无码久久久久久| 亚洲乱码日产精品a级毛片久久| 亚洲综合精品网站| 亚洲精品色午夜无码专区日韩| 综合人妻久久一区二区精品| 中文精品久久久久人妻不卡|