Question

I'm trying to implement custom socket factory for compression in our Spring-rmi Application.

below is the rmi-server configuration :

<bean id="parentRmiServiceExporter" class="org.springframework.remoting.rmi.RmiServiceExporter" abstract="true">
    <property name="registryPort" value="${rmi.port}"/>
    <property name="registerTraceInterceptor" value="false"/>
    <!-- gZip Server and Client Socket Factory -->    
    <property name="clientSocketFactory" ref=egsZipClientSocketFactory/>   
    <property name="serverSocketFactory" ref="egsZipServerSocketFactory" />
</bean>

Compression Stream is based on below implementation :

http://jmvidal.cse.sc.edu/csce790/handouts/sockets/CompressionInputStream.java.html

however i am getting below error on rmi client start up:

 Caused by: java.net.SocketException: Software caused connection abort: socket write error
    at java.net.SocketOutputStream.socketWrite0(Native Method) [rt.jar:1.7.0_15]
    at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:109) [rt.jar:1.7.0_15]
    at java.net.SocketOutputStream.write(SocketOutputStream.java:132) [rt.jar:1.7.0_15]

how to achieve compression with spring-rmi app ?

Was it helpful?

Solution

We can compress the Object using DeflaterOutputStream and uncompress the same using InflaterInputStream.

Compression :

ByteArrayOutputStream buf = new ByteArrayOutputStream();
ObjectOutputStream out = new ObjectOutputStream(new DeflaterOutputStream(buf,       new        Deflater(Deflater.BEST_COMPRESSION)));
out.writeObject(object);

egsZippedObject = new EGSZippedObject(buf.toByteArray());

public class EGSZippedObject implements Serializable{       
private byte[] byteArray = null;
public EGSZippedObject(byte[] byteArray) throws Exception { 
    setByteArray(byteArray);
}
public void setByteArray(byte[] byteArray) {
    this.byteArray = byteArray;
}
public byte[] getByteArray() {
    return byteArray;
}
}

Uncompression :

ByteArrayInputStream buf = null;
ObjectInputStream in = null;
R retVal = null;

buf = new ByteArrayInputStream(egsZippedObject.getByteArray());
in = new ObjectInputStream(new InflaterInputStream(buf));
retVal = (R) in.readObject();           
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top