自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Apache CXF實戰(zhàn)之五:壓縮Web Service數(shù)據(jù)

開發(fā) 后端
在現(xiàn)實應(yīng)用中有些時候會有比較大的數(shù)據(jù)對象需要傳輸,或者在一個比較慢的網(wǎng)絡(luò)環(huán)境下發(fā)布調(diào)用web service,此時可以通過壓縮數(shù)據(jù)流的方式來減小數(shù)據(jù)包的大小,從而提高web service的性能。下面來看看怎樣來做到這一點。

在現(xiàn)實應(yīng)用中有些時候會有比較大的數(shù)據(jù)對象需要傳輸,或者在一個比較慢的網(wǎng)絡(luò)環(huán)境下發(fā)布調(diào)用web service,此時可以通過壓縮數(shù)據(jù)流的方式來減小數(shù)據(jù)包的大小,從而提高web service的性能。下面來看看怎樣來做到這一點。

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

  1. package com.googlecode.garbagecan.cxfstudy.compress;  
  2.  
  3.  
  4. public class BigData {  
  5.       
  6.     private String name;  
  7.       
  8.     private String data;  
  9.       
  10.     public BigData() {  
  11.           
  12.     }  
  13.       
  14.     public BigData(String name, int size) {  
  15.         this.name = name;  
  16.         StringBuilder sb = new StringBuilder();  
  17.         for (int i = 0; i < size; i++) {  
  18.             sb.append("0");  
  19.         }  
  20.         this.data = sb.toString();  
  21.     }  
  22.  
  23.     public String getName() {  
  24.         return name;  
  25.     }  
  26.  
  27.     public void setName(String name) {  
  28.         this.name = name;  
  29.     }  
  30.  
  31.     public String getData() {  
  32.         return data;  
  33.     }  
  34.  
  35.     public void setData(String data) {  
  36.         this.data = data;  
  37.     }  

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

  1. package com.googlecode.garbagecan.cxfstudy.compress;  
  2.  
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebResult;  
  6. import javax.jws.WebService;  
  7.  
  8. @WebService 
  9. public interface BigDataService {  
  10.       
  11.     @WebMethod 
  12.     @WebResult BigData getBigData(@WebParam String name, @WebParam int size);  

3. Web Service實現(xiàn)類

  1. package com.googlecode.garbagecan.cxfstudy.compress;  
  2.  
  3. public class BigDataServiceImpl implements BigDataService {  
  4.     public BigData getBigData(String name, int size) {  
  5.         BigData bigData = new BigData(name, size);  
  6.         return bigData;  
  7.     }  

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ù)包。

  1. package com.googlecode.garbagecan.cxfstudy.compress;  
  2.  
  3. import org.apache.cxf.endpoint.Client;  
  4. import org.apache.cxf.endpoint.Endpoint;  
  5. import org.apache.cxf.frontend.ClientProxy;  
  6. import org.apache.cxf.interceptor.LoggingInInterceptor;  
  7. import org.apache.cxf.interceptor.LoggingOutInterceptor;  
  8. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  9. import org.apache.cxf.jaxws.JaxWsServerFactoryBean;  
  10. import org.apache.cxf.transport.http.gzip.GZIPInInterceptor;  
  11. import org.apache.cxf.transport.http.gzip.GZIPOutInterceptor;  
  12. import org.junit.Assert;  
  13. import org.junit.BeforeClass;  
  14. import org.junit.Test;  
  15.  
  16. public class BigDataServiceTest {  
  17.  
  18.     private static final String address = "http://localhost:9000/ws/compress/bigDataService";  
  19.       
  20.     @BeforeClass 
  21.     public static void setUpBeforeClass() throws Exception {  
  22.         JaxWsServerFactoryBean factoryBean = new JaxWsServerFactoryBean();  
  23.         factoryBean.getInInterceptors().add(new LoggingInInterceptor());  
  24.         factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());  
  25.         factoryBean.getInInterceptors().add(new GZIPInInterceptor());  
  26.         factoryBean.getOutInterceptors().add(new GZIPOutInterceptor());  
  27.           
  28.         factoryBean.setServiceClass(BigDataServiceImpl.class);  
  29.         factoryBean.setAddress(address);  
  30.         factoryBean.create();  
  31.     }  
  32.  
  33.     @Test 
  34.     public void testGetBigData() {  
  35.         JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();  
  36.         factoryBean.setAddress(address);  
  37.         factoryBean.setServiceClass(BigDataService.class);  
  38.         Object obj = factoryBean.create();  
  39.           
  40.         Client client = ClientProxy.getClient(obj);  
  41.         Endpoint endpoint = client.getEndpoint();  
  42.         endpoint.getInInterceptors().add(new GZIPInInterceptor());  
  43.         endpoint.getOutInterceptors().add(new GZIPOutInterceptor());  
  44.           
  45.         BigDataService service = (BigDataService) obj;  
  46.         Assert.assertNotNull(service);  
  47.           
  48.         String name = "my big data";  
  49.         int size = 1024 * 1024 * 10;  
  50.           
  51.         long start = System.currentTimeMillis();  
  52.         BigData bigData = service.getBigData(name, size);  
  53.         long stop = System.currentTimeMillis();  
  54.         System.out.println("Time: " + (stop - start));  
  55.           
  56.         Assert.assertNotNull(bigData);  
  57.         Assert.assertEquals(name, bigData.getName());  
  58.         Assert.assertEquals(size, bigData.getData().length());  
  59.     }  

5. 運行此unit test,可以在日志中看到數(shù)據(jù)包前后大小和內(nèi)容。

原文鏈接:http://blog.csdn.net/kongxx/article/details/7530216

【系列文章】

  1. Apache CXF實戰(zhàn)之五:壓縮Web Service數(shù)據(jù)
  2. Apache CXF實戰(zhàn)之四:構(gòu)建RESTful Web Service
  3. Apache CXF實戰(zhàn)之三:傳輸Java對象
  4. Apache CXF實戰(zhàn)之二:集成Sping與Web容器
  5. Apache CXF實戰(zhàn)之一:Hello World Web Service
責任編輯:林師授 來源: kongxx的博客
相關(guān)推薦

2012-05-03 11:43:32

ApacheCXFRESTful

2012-05-07 14:15:41

ApacheCXFJava

2012-05-07 14:08:20

ApacheCXFJava

2012-05-03 11:21:58

ApacheCXFJava

2012-05-03 11:30:04

ApacheCXFJava

2012-05-03 11:35:56

ApacheCXFJava

2012-02-22 22:56:19

開源Apache

2022-10-24 00:26:51

大數(shù)據(jù)Hadoop存儲層

2012-05-03 10:55:51

ApacheMINAJava

2012-05-03 10:24:02

ApacheMINAJava

2012-02-15 10:40:37

JavaJava Socket

2009-06-22 10:14:00

J2EE web se

2009-10-21 13:43:04

Linux壓縮方法

2011-04-27 10:57:29

高性能web開發(fā)

2023-02-26 00:12:10

Hadoop數(shù)據(jù)湖存儲

2019-05-21 14:33:01

2011-04-22 09:54:37

CSSjavascript

2009-06-22 13:33:00

Apache CXF2JAX-WS2.0

2013-06-25 11:46:36

虛擬化實戰(zhàn)IP Storage

2015-03-26 11:25:10

對稱加密加密壓縮加密解密解壓
點贊
收藏

51CTO技術(shù)棧公眾號