In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to compress Web Service data in Apache CXF. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
In practical applications, there are sometimes large data objects that need to be transmitted, or web service is issued and called in a slow network environment. At this time, the size of data packets can be reduced by compressing the data stream, so as to improve the performance of web service. Let's take a look at how to do this.
1. First simulate a pojo object that can hold big data, which can simulate a string of size size by constructing a size given 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) Sb.toString +) {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 Service, and the testGetBigData method is used to test web service.
Notice in the setUpBeforeClass method
FactoryBean.getInInterceptors () .add (new GZIPInInterceptor ()
FactoryBean.getOutInterceptors () .add (new GZIPOutInterceptor ()
And the testGetBigData method
Endpoint.getInInterceptors () .add (new GZIPInInterceptor ()
Endpoint.getOutInterceptors () .add (new GZIPOutInterceptor ()
The above two pieces of code tell CXF to use compressed Interceptor to compress and decompress 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. When you run this unit test, you can see the size and contents of the packet before and after the log.
This is how to compress Web Service data in Apache CXF shared by Xiaobian. If you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.