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.
- 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. The Web Service Interface Class is no different from the ordinary interface definition.
- 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 implementation class
- 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. 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.
- 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. Run this unit test to view the size and content of the data packet in the log.
Series of articles]