Apache CXF practice 5: compressing Web Service Data

Source: Internet
Author: User

In real applications, there are sometimes large data objects that need to be transmitted, or publish and call web Services in a slow network environment, in this case, you can reduce the data packet size by compressing the data stream to improve the web service performance. Let's take a look at how to achieve this.

1. First simulate a pojo object that can store and enlarge data. This object can simulate a size string by constructing the size specified by the parameter.

 
 
  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. The Web Service Interface Class is no different from the ordinary interface definition.

 
 
  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 implementation class

 
 
  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. Test class. This article uses the JUnit test class for testing. The setUpBeforeClass method is used to start the Service, and the testGetBigData method is used to test the web service.

Note: In the setUpBeforeClass method

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

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

And testGetBigData

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

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

The above code tells CXF to use the compressed Interceptor to compress and decompress data packets.

 
 
  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. Run this unit test to view the size and content of the data packet in the log.

Series of articles]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.