title: Spring integration reply message hangs until the 'timing out TcpNioConnection' occurs
body: <p>i want to use spring integration for TCP communication with another server (written in C language).</p>
  
  <p>Exchange data between servers are raw bytes, but messages have no delimiter. </p>
  
  <p>Therefore i've written my own Serializer/Deserializer that extends <strong>AbstractByteArraySerializer</strong>.</p>
  
  <p>But, when i send message over MessagingGateway -> TcpOutboundGateway -> TcpNioClientConnectionFactory, reply message hangs until the timing out TcpNioConnection occurs.</p>
  
  <p>Here is my SI java configuration :</p>
  
  <pre><code>@Bean(name = "loggingChannel")
  public MessageChannel loggingChannel() {
      return new DirectChannel();
  }
  
  @Bean
  @GlobalChannelInterceptor()
  public ChannelInterceptor wiretap() {
      return new WireTap(loggingChannel(), s -&gt; true);
  }
  
  private int port = 10101;
  
  private String host = "10.1.3.119";
  
  @Bean
  public MessageChannel toTcp() {
      return new DirectChannel();
  }
  
  @Bean
  public MessageChannel fromTcp() {
      return new DirectChannel();
  }
  
  @Autowired
  public PerunGateway perunGateway;
  
  @MessagingGateway(defaultRequestChannel = "toTcp", defaultReplyChannel = "fromTcp")
  public interface PerunGateway {
  
      @Gateway
      byte[] sendCommand(byte[] message);
  }
  
  @Bean
  @ServiceActivator(inputChannel = "toTcp")
  public TcpOutboundGateway tcpOutGate() {
      TcpOutboundGateway gate = new TcpOutboundGateway();
      gate.setConnectionFactory(clientConnectionFactory());
      gate.setOutputChannelName("fromTcp");
      gate.setRequestTimeout(5000);
      return gate;
  }
  
  @Bean
  public AbstractClientConnectionFactory clientConnectionFactory() {
      TcpNioClientConnectionFactory tnccf = new TcpNioClientConnectionFactory(host, port);
      tnccf.setSoTimeout(1000);
      tnccf.setSingleUse(true);
      tnccf.setSerializer(new SunByteArrayRawSerializer());
      tnccf.setDeserializer(new SunByteArrayRawSerializer());
      return tnccf;
  }
  </code></pre>
  
  <p>And output i get after sending the message :</p>
  
  <pre><code>2015-11-12 13:49:09.032 DEBUG 10329 --- [nio-8080-exec-1] o.s.integration.channel.DirectChannel    : preSend on channel 'toTcp', message: GenericMessage [payload=byte[150], headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, id=e5425395-e1cb-8cc7-20fd-eb5b9be3a615, timestamp=1447332549031}]
          2015-11-12 13:49:09.032 DEBUG 10329 --- [nio-8080-exec-1] o.s.integration.channel.DirectChannel    : preSend on channel 'loggingChannel', message: GenericMessage [payload=byte[150], headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, id=e5425395-e1cb-8cc7-20fd-eb5b9be3a615, timestamp=1447332549031}]
          2015-11-12 13:49:09.033 DEBUG 10329 --- [nio-8080-exec-1] o.s.integration.handler.LoggingHandler   : org.springframework.integration.handler.LoggingHandler#0 received message: GenericMessage [payload=byte[150], headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, id=e5425395-e1cb-8cc7-20fd-eb5b9be3a615, timestamp=1447332549031}]
          2015-11-12 13:49:09.033  INFO 10329 --- [nio-8080-exec-1] o.s.integration.handler.LoggingHandler   : GenericMessage [payload=byte[150], headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, id=e5425395-e1cb-8cc7-20fd-eb5b9be3a615, timestamp=1447332549031}]
          2015-11-12 13:49:09.034 DEBUG 10329 --- [nio-8080-exec-1] o.s.integration.channel.DirectChannel    : postSend (sent=true) on channel 'loggingChannel', message: GenericMessage [payload=byte[150], headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, id=e5425395-e1cb-8cc7-20fd-eb5b9be3a615, timestamp=1447332549031}]
          2015-11-12 13:49:09.034 DEBUG 10329 --- [nio-8080-exec-1] o.s.i.ip.tcp.TcpOutboundGateway          : tcpOutGate received message: GenericMessage [payload=byte[150], headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, id=e5425395-e1cb-8cc7-20fd-eb5b9be3a615, timestamp=1447332549031}]
          2015-11-12 13:49:09.034 DEBUG 10329 --- [nio-8080-exec-1] .s.i.i.t.c.TcpNioClientConnectionFactory : Opening new socket connection to 10.1.3.119:10101
          2015-11-12 13:49:09.042 DEBUG 10329 --- [nio-8080-exec-1] o.s.i.i.tcp.connection.TcpNioConnection  : New connection 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19
          2015-11-12 13:49:09.043 DEBUG 10329 --- [nio-8080-exec-1] .s.i.i.t.c.TcpNioClientConnectionFactory : clientConnectionFactory: Added new connection: 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19
          2015-11-12 13:49:09.045 TRACE 10329 --- [pool-1-thread-1] .s.i.i.t.c.TcpNioClientConnectionFactory : clientConnectionFactory: Connection is open: 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19
          2015-11-12 13:49:09.045 TRACE 10329 --- [pool-1-thread-1] .s.i.i.t.c.TcpNioClientConnectionFactory : Host 10.1.3.119 port 10101 SelectionCount: 0
          2015-11-12 13:49:09.048 DEBUG 10329 --- [nio-8080-exec-1] o.s.i.ip.tcp.TcpOutboundGateway          : Added pending reply 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19
          2015-11-12 13:49:09.049 DEBUG 10329 --- [nio-8080-exec-1] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 writing 150
          2015-11-12 13:49:09.049 DEBUG 10329 --- [nio-8080-exec-1] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 Message sent GenericMessage [payload=byte[150], headers={replyChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, errorChannel=org.springframework.messaging.core.GenericMessagingTemplate$TemporaryReplyChannel@66e6fc48, id=e5425395-e1cb-8cc7-20fd-eb5b9be3a615, timestamp=1447332549031}]
          2015-11-12 13:49:09.111 TRACE 10329 --- [pool-1-thread-1] .s.i.i.t.c.TcpNioClientConnectionFactory : clientConnectionFactory: Connection is open: 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19
          2015-11-12 13:49:09.111 TRACE 10329 --- [pool-1-thread-1] .s.i.i.t.c.TcpNioClientConnectionFactory : Host 10.1.3.119 port 10101 SelectionCount: 1
          2015-11-12 13:49:09.114 DEBUG 10329 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 Reading...
          2015-11-12 13:49:09.115 DEBUG 10329 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 Running an assembler
          2015-11-12 13:49:09.115 TRACE 10329 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNioConnection  : Before read:0/61440
          2015-11-12 13:49:09.115 TRACE 10329 --- [pool-1-thread-3] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 Nio message assembler running...
          2015-11-12 13:49:09.115 TRACE 10329 --- [pool-1-thread-3] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 checking data avail: 0 pending: true
          2015-11-12 13:49:09.116 TRACE 10329 --- [pool-1-thread-3] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 checking data avail (convert): 0 pending: true
          2015-11-12 13:49:09.116 TRACE 10329 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNioConnection  : After read:102/61440
          2015-11-12 13:49:09.116 TRACE 10329 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNioConnection  : After flip:0/102
          2015-11-12 13:49:09.116 DEBUG 10329 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNioConnection  : Read 102 into raw buffer
          2015-11-12 13:49:09.116 TRACE 10329 --- [pool-1-thread-2] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 Sending 102 to pipe
          2015-11-12 13:49:09.116 TRACE 10329 --- [pool-1-thread-1] .s.i.i.t.c.TcpNioClientConnectionFactory : clientConnectionFactory: Connection is open: 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19
          2015-11-12 13:49:09.117 TRACE 10329 --- [pool-1-thread-1] .s.i.i.t.c.TcpNioClientConnectionFactory : Host 10.1.3.119 port 10101 SelectionCount: 0
          2015-11-12 13:49:10.117  WARN 10329 --- [pool-1-thread-1] .s.i.i.t.c.TcpNioClientConnectionFactory : Timing out TcpNioConnection 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19
          2015-11-12 13:49:10.118 DEBUG 10329 --- [pool-1-thread-1] .s.i.i.t.c.TcpNioClientConnectionFactory : clientConnectionFactory: Removed closed connection: 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19
          2015-11-12 13:49:10.119 TRACE 10329 --- [pool-1-thread-1] .s.i.i.t.c.TcpNioClientConnectionFactory : Host 10.1.3.119 port 10101 SelectionCount: 0
          2015-11-12 13:49:11.117 TRACE 10329 --- [pool-1-thread-3] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 checking data avail: 0 pending: false
          2015-11-12 13:49:11.117 TRACE 10329 --- [pool-1-thread-3] o.s.i.ip.tcp.TcpOutboundGateway          : onMessage: 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19(GenericMessage [payload=byte[108], headers={ip_address=10.1.3.119, id=1006fc9a-8be4-3722-3cbb-9250714b47ec, ip_hostname=10.1.3.119, ip_tcp_remotePort=10101, ip_connectionId=10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19, timestamp=1447332551117}])
          2015-11-12 13:49:11.117 TRACE 10329 --- [pool-1-thread-3] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 checking data avail: 0 pending: false
          2015-11-12 13:49:11.118 TRACE 10329 --- [pool-1-thread-3] o.s.i.i.tcp.connection.TcpNioConnection  : 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19 Nio message assembler exiting... avail: 0
          2015-11-12 13:49:11.118 DEBUG 10329 --- [nio-8080-exec-1] o.s.i.ip.tcp.TcpOutboundGateway          : Response GenericMessage [payload=byte[108], headers={ip_address=10.1.3.119, id=1006fc9a-8be4-3722-3cbb-9250714b47ec, ip_hostname=10.1.3.119, ip_tcp_remotePort=10101, ip_connectionId=10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19, timestamp=1447332551117}]
          2015-11-12 13:49:11.118 DEBUG 10329 --- [nio-8080-exec-1] o.s.i.ip.tcp.TcpOutboundGateway          : Removed pending reply 10.1.3.119:10101:38308:430e8a01-3d8a-462a-85a7-00ff6ce70b19
  </code></pre>
  
  <p>Any ideas whats going on ? </p>
  
  <p>Here is my custom deserialize method : </p>
  
  <pre><code>@Override
  public byte[] deserialize(InputStream inputStream) throws IOException {
      if (logger.isDebugEnabled()) {
          logger.debug("Available to read:" + inputStream.available());
      }
  
      byte[] header = new byte[2];
      header[0] = (byte) inputStream.read();
      if (header[0] &lt; 0) {
          throw new SoftEndOfStreamException("Stream closed between payloads");
      }
  
      header[1] = (byte) inputStream.read();
      if (header[1] &lt; 0) {
          throw new SoftEndOfStreamException("Stream closed between payloads");
      }
  
      int val = getIntFromTwoBytes(header);
  
      byte[] length = new byte[val];
      for (int i = 0; i &lt; val; i++) {
          length[i] = (byte) inputStream.read();
      }
  
      int messageLength;
      if (val == 2) {
          messageLength = getIntFromTwoBytes(length);
      } else if (val == 4) {
          messageLength = getIntFromFourBytes(length);
      } else throw new IOException("Unexpected count of bytes that holds message length");
  
      byte[] answer = new byte[messageLength];
      for (int i = 0; i &lt; messageLength; i++) {
          int bite = inputStream.read();
          if (bite &lt; 0) {
              throw new SoftEndOfStreamException("Stream closed between payloads");
          }
          answer[i] = (byte) bite;
      }
  
      ByteBuffer b = ByteBuffer.allocate(2 + val + messageLength);
      b.put(header);
      b.put(length);
      b.put(answer);
      return b.array();
  }
  </code></pre>
  
  <p>We use own binary protocol, so i know exact length of incoming message.  </p>