【发布时间】:2017-04-14 15:52:24
【问题描述】:
由于客户端-服务器应用程序中的序列化,我正在尝试通过 TCP 发送对象。 TCP 客户端是在 android 系统上编写的,我使用 ObjectOutputStream 发送一个对象。 TCP 服务器是用 spring 集成编写的,我尝试使用反序列化器读取这个对象。像这样的:
<int-ip:tcp-connection-factory id="hosServer"
serializer="connectionSerializeDeserialize"
deserializer="connectionSerializeDeserialize" ..... >
在实现序列化器和反序列化器接口的类中,我从 InputSream 创建 ObjectInputStream,这是反序列化器方法中的参数。在我尝试再次连接到服务器的那一刻,它工作正常。然后我在通过 readObject() 方法从 ObjectInputStream 读取对象时收到 EOFException。
public class CommandConverter implements Serializer<Command>, Deserializer<Command>{
private ObjectInputStream ois = null;
private ObjectOutputStream oos = null;
private CommandBuilder commandBuilder = new CommandBuilder();
public Command deserialize(InputStream inputStream) throws IOException {
if (ois == null)
ois = new ObjectInputStream(inputStream);
Command cmd = null;
try {
cmd = (Command) ois.readObject();
} catch (ClassNotFoundException e) {
commandBuilder.setCommandBuilder(new ImproperCommandBuilder());
commandBuilder.createCommand();
cmd = commandBuilder.getCommand();
} catch (InvalidClassException e) {
commandBuilder.setCommandBuilder(new ImproperCommandBuilder());
commandBuilder.createCommand();
cmd = commandBuilder.getCommand();
}
return cmd;
}
在 Spring 集成中通过 TCP 发送对象的最佳方式是什么?
【问题讨论】:
标签: java tcp deserialization spring-integration tcpserver