Apache CXF實(shí)戰(zhàn)之五:壓縮Web Service數(shù)據(jù)
在現(xiàn)實(shí)應(yīng)用中有些時(shí)候會(huì)有比較大的數(shù)據(jù)對(duì)象需要傳輸,或者在一個(gè)比較慢的網(wǎng)絡(luò)環(huán)境下發(fā)布調(diào)用web service,此時(shí)可以通過(guò)壓縮數(shù)據(jù)流的方式來(lái)減小數(shù)據(jù)包的大小,從而提高web service的性能。下面來(lái)看看怎樣來(lái)做到這一點(diǎn)。
1. 首先模擬一個(gè)可以存放大數(shù)據(jù)的pojo對(duì)象,這個(gè)對(duì)象可以通過(guò)構(gòu)造參數(shù)給定的size來(lái)模擬一個(gè)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接口類,和普通的接口定義沒(méi)有什么區(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實(shí)現(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. 測(cè)試類,這片文章使用了JUnit測(cè)試類來(lái)做測(cè)試。setUpBeforeClass方法用來(lái)啟動(dòng)Service, testGetBigData方法用來(lái)測(cè)試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來(lái)壓縮和解壓縮數(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. 運(yùn)行此unit test,可以在日志中看到數(shù)據(jù)包前后大小和內(nèi)容。
原文鏈接:http://blog.csdn.net/kongxx/article/details/7530216
【系列文章】





















