2017-04-14 49 views
0

我想通过TCP发送对象感谢客户端服务器应用程序中的序列化。 TCP客户端写在android系统上,我使用ObjectOutputStream发送一个对象。 TCP服务器是用Spring集成编写的,我尝试使用解串器来读取这个对象。事情是这样的:InputStream对象转换 - 弹簧集成

<int-ip:tcp-connection-factory id="hosServer" 
    serializer="connectionSerializeDeserialize" 
    deserializer="connectionSerializeDeserialize" ..... > 

在它实现了我createing从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; 
    } 

春季集成中通过TCP发送对象的最佳方式是什么?

回答

0

SerializerDeserializer实现必须是线程安全的。达到这个目的最简单的方法是使实现无状态

由于您在课堂中拥有这些属性,因此它们将在不同的调用之间共享。如果有这样的状态,它会使行为变得不可预测。

ObjectInputStreamObjectOutputStream不适用于共享状态。

所以,你必须创建ObjectInputStream所有deserialize()被称为时间:

ObjectInputStream ois = new ObjectInputStream(inputStream); 

等。