xpj's knowledge base » Spring http://www.xpj.cz/expee Just another blog. Wed, 03 Nov 2010 10:24:22 +0000 en hourly 1 Spring remoting with gzip compression http://www.xpj.cz/expee/2009/06/spring-remoting-with-gzip-compression/ http://www.xpj.cz/expee/2009/06/spring-remoting-with-gzip-compression/#comments Mon, 29 Jun 2009 14:48:49 +0000 xpj http://www.xpj.cz/expee/?p=50 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 :-)

]]>
http://www.xpj.cz/expee/2009/06/spring-remoting-with-gzip-compression/feed/ 0
How stop embedded ActiveMQ in Spring http://www.xpj.cz/expee/2009/06/how-stop-embedded-activemq-in-spring/ http://www.xpj.cz/expee/2009/06/how-stop-embedded-activemq-in-spring/#comments Tue, 09 Jun 2009 14:34:17 +0000 xpj http://www.xpj.cz/expee/?p=38 Java

Today I ran into problems with embedded ActiveMQ broker in Spring Framework. The goal is to run local application, which uses Spring context to start AMQ and after this application is finished, AMQ should stop.

My setup of spring context xml is following (I’m using the amq schema and this is only the needed part).

<amq:broker id="broker" useJmx="false" persistent="false" start="true"  >
  <amq:transportConnectors>
    <amq:transportConnector uri="tcp://localhost:0" />
  </amq:transportConnectors>
</amq:broker>
<amq:connectionFactory id="connectionFactory" brokerURL="vm://localhost"/>
<amq:queue id="replyQueue" physicalName="replyQueue" />
<amq:queue id="requestQueue" physicalName="requestQueue" />

And the app looks like this:

context = new ClassPathXmlApplicationContext("client-local-context.xml");

 ComputingStarterService computingStarterService = (ComputingService)context.getBean("computingStarterService");
 computingStarterService.start("Test");

And the problem is, that the main part of application stops, but the AMQ is still running. I tried a lot of things and the only working thing is to use

context.close();

I don’t know if there is another way to stop embedded AMQ. How it works eg. in JUnit tests?

]]>
http://www.xpj.cz/expee/2009/06/how-stop-embedded-activemq-in-spring/feed/ 0