private void doTestCloseOnSendError(
     TcpConnection conn1, TcpConnection conn2, CachingClientConnectionFactory cccf)
     throws Exception {
   TcpConnection cached1 = cccf.getConnection();
   try {
     cached1.send(new GenericMessage<String>("foo"));
     fail("Expected IOException");
   } catch (IOException e) {
     assertEquals("Foo", e.getMessage());
   }
   // Before INT-3163 this failed with a timeout - connection not returned to pool after failure on
   // send()
   TcpConnection cached2 = cccf.getConnection();
   assertTrue(cached1.getConnectionId().contains(conn1.getConnectionId()));
   assertTrue(cached2.getConnectionId().contains(conn2.getConnectionId()));
 }
 protected final void addStandardHeaders(
     TcpConnection connection, AbstractIntegrationMessageBuilder<?> messageBuilder) {
   String connectionId = connection.getConnectionId();
   messageBuilder
       .setHeader(IpHeaders.HOSTNAME, connection.getHostName())
       .setHeader(IpHeaders.IP_ADDRESS, connection.getHostAddress())
       .setHeader(IpHeaders.REMOTE_PORT, connection.getPort())
       .setHeader(IpHeaders.CONNECTION_ID, connectionId);
   if (this.applySequence) {
     messageBuilder
         .setCorrelationId(connectionId)
         .setSequenceNumber((int) connection.incrementAndGetConnectionSequence());
   }
 }
 @Override
 public Message<?> toMessage(TcpConnection connection) throws Exception {
   Message<Object> message = null;
   Object payload = connection.getPayload();
   if (payload != null) {
     AbstractIntegrationMessageBuilder<Object> messageBuilder =
         this.messageBuilderFactory.withPayload(payload);
     this.addStandardHeaders(connection, messageBuilder);
     this.addCustomHeaders(connection, messageBuilder);
     message = messageBuilder.build();
   } else {
     if (logger.isWarnEnabled()) {
       logger.warn("Null payload from connection " + connection.getConnectionId());
     }
   }
   return message;
 }