Just another blog.

Spring remoting with gzip compression

Java

GZip compression is well known standard, used almost everywhere. Modern web servers are able to communicate with modern browsers and this communication can be automatically gzipped. But if you are unsure, if your communication is compressed, you can use your own.

With the Spring HTTP remoting it’s so easy as to implement extensions for existing classes.

package cz.xpj.expee.spring.remoting.httpinvoker;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import org.springframework.remoting.httpinvoker.SimpleHttpInvokerRequestExecutor;

/**
 * Extension of {@link SimpleHttpInvokerRequestExecutor} for gzip compression
 * @author xpj
 */
public class GZIPSimpleHttpInvokerRequestExecutor extends SimpleHttpInvokerRequestExecutor {

 /**
 * @see org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor#decorateInputStream(java.io.InputStream)
 */
 @Override
 protected InputStream decorateInputStream(InputStream is) throws IOException {
 return new GZIPInputStream(is);
 }

 /**
 * @see org.springframework.remoting.httpinvoker.AbstractHttpInvokerRequestExecutor#decorateOutputStream(java.io.OutputStream)
 */
 @Override
 protected OutputStream decorateOutputStream(OutputStream os) throws IOException {
 return new GZIPOutputStream(os);
 }

}

And the other side of your communication…

package cz.xpj.expee.spring.remoting.httpinvoker;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter;

/**
 * Extension of {@link HttpInvokerServiceExporter} for gzip compression
 * @author xpj
 */
public class GZIPHttpInvokerServiceExporter extends HttpInvokerServiceExporter {

 /**
 * @see org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter#decorateInputStream(javax.servlet.http.HttpServletRequest, java.io.InputStream)
 */
 @Override
 protected InputStream decorateInputStream(HttpServletRequest request, InputStream inputStream) throws IOException {
new GZIPInputStream(inputStream);
}

/**
* @see org.springframework.remoting.httpinvoker.HttpInvokerServiceExporter#decorateOutputStream(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, java.io.OutputStream)
*/
@Override
protected OutputStream decorateOutputStream(HttpServletRequest request, HttpServletResponse response, OutputStream outputStream) throws IOException {
return new GZIPOutputStream(outputStream);
}

}

I tested this communication on some artificial data, and the results were really nice :-)

Sended (List with # of longs) Without GZIP [bytes] With GZIP [bytes]
1000 20328 2741
2000 40328 5235
3000 60328 7852

I know, this is not so good example, but with different data (some entities etc.) we are still better than without compression :-)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>