0

我正在尝试在我们的 Spring-rmi 应用程序中实现自定义套接字工厂以进行压缩。

以下是 rmi-server 配置:

<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>

压缩流基于以下实现:

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

但是我在 rmi 客户端启动时遇到以下错误:

 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]

如何使用 spring-rmi app 实现压缩?

4

1 回答 1

0

我们可以使用 DeflaterOutputStream 压缩 Object 并使用 InflaterInputStream 解压缩。

压缩

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;
}
}

解压

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

buf = new ByteArrayInputStream(egsZippedObject.getByteArray());
in = new ObjectInputStream(new InflaterInputStream(buf));
retVal = (R) in.readObject();           
于 2013-09-10T13:05:33.650 回答