title: Outbound adapter attempting to send message after client is already disconnected
body: <p>I am using Spring Integration to process data and then return a response. I have inbound adapter and outbound adapters setup with a few channels to handle the initial ACK and process the data and return a response to the client.</p>
  <p>When the client disconnects we trigger an additional process that runs over all the data they had since in our database.</p>
  <p>The issue is when the client sends data and we start processing it and then the client disconnects, the outbound adapter(?) attempts to send the response of that process back to the client. This causes an exception and breaks the flow of that additional process we kicked off after they disconnected.</p>
  <p>Running this on my local machine it runs fine. When this is deployed to <strong>AWS Elastic Beanstalk</strong> on a <strong>Amazon Linux 2</strong> instance running <strong>Corretto 11</strong> it fails with the error below.</p>
  <p>I am not sure if there is a way to check that the client is still connected before attempting to send. I tried using the error-channel but it does not catch it. I am quite new with Spring Integration so any help is greatly appreciated.</p>
  <p>Thanks.</p>
  <p>Error log (replaced the client IP with XXXX):</p>
  <pre><code>Nov 17 16:19:38 ip-172-31-40-195 web: 2020-11-17 16:19:38.985  INFO 31923 --- [pool-4-thread-2] c.o.v.service.DefaultVoiceDataProcessor  : Client XXXXXXX disconnected.
  Nov 17 16:19:38 ip-172-31-40-195 web: 2020-11-17 16:19:38.986  WARN 31923 --- [pool-4-thread-2] o.s.i.i.tcp.connection.TcpNetConnection  : Failed to publish TcpConnectionCloseEvent [source=TcpNetConnection:XXXXXX], [factory=serverConnectionFactory, connectionId=XXXXXX] **CLOSED**:null
  Nov 17 16:19:39 ip-172-31-40-195 web: 2020-11-17 16:19:39.001 ERROR 31923 --- [pool-4-thread-2] o.s.i.ip.tcp.TcpSendingMessageHandler    : Error sending message
  Nov 17 16:19:39 ip-172-31-40-195 web: org.springframework.messaging.MessagingException: Send Failed; nested exception is java.net.SocketException: Connection reset by peer (Write failed)
  Nov 17 16:19:39 ip-172-31-40-195 web: at org.springframework.integration.ip.tcp.connection.TcpNetConnection.send(TcpNetConnection.java:118) ~[spring-integration-ip-5.3.2.RELEASE.jar!/:5.3.2.RELEASE]
  Nov 17 16:19:39 ip-172-31-40-195 web: at org.springframework.integration.ip.tcp.TcpSendingMessageHandler.handleMessageAsServer(TcpSendingMessageHandler.java:119) ~[spring-integration-ip-5.3.2.RELEASE.jar!/:5.3.2.RELEASE]
  Nov 17 16:19:39 ip-172-31-40-195 web: at org.springframework.integration.ip.tcp.TcpSendingMessageHandler.handleMessageInternal(TcpSendingMessageHandler.java:103) ~[spring-integration-ip-5.3.2.RELEASE.jar!/:5.3.2.RELEASE]
  Nov 17 16:19:39 ip-172-31-40-195 web: at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:62) ~[spring-integration-core-5.3.2.RELEASE.jar!/:5.3.2.RELEASE]
  Nov 17 16:19:39 ip-172-31-40-195 web: at org.springframework.integration.handler.ReplyProducingMessageHandlerWrapper.handleRequestMessage(ReplyProducingMessageHandlerWrapper.java:58) ~[spring-integration-core-5.3.2.RELEASE.jar!/:5.3.2.RELEASE]
  </code></pre>
  <p>Adapter Config setup:</p>
  <pre><code>    @Bean
      fun serverConnectionFactory(byteArrayCrlfSerializer: ByteArrayCrLfSerializer): AbstractServerConnectionFactory =
              TcpNetServerConnectionFactory(port).apply {
                  deserializer = byteArrayCrlfSerializer
                  soSendBufferSize = MAX_MESSAGE_SIZE + CLRF_SIZE
                  soReceiveBufferSize = MAX_MESSAGE_SIZE + CLRF_SIZE
  
              }
  
      @Bean
      fun inboundAdapter(serverConnectionFactory: AbstractServerConnectionFactory) =
              TcpReceivingChannelAdapter().apply {
                  setConnectionFactory(serverConnectionFactory)
                  setOutputChannelName(&quot;sendAcknowledgement&quot;)
              }
  
      @Bean
      @ServiceActivator(inputChannel = &quot;sendResponse&quot;)
      fun outboundAdapter(serverConnectionFactory: AbstractServerConnectionFactory) =
              TcpSendingMessageHandler().apply {
                  setConnectionFactory(serverConnectionFactory)
              }
  
  
      @MessagingGateway(defaultRequestChannel = &quot;sendResponse&quot;, errorChannel = &quot;outboundErrorChannel&quot;)
      interface OutboundMessageGateway {
          @Throws(MessagingException::class)
          fun send(@Payload message: String, @Header(IpHeaders.CONNECTION_ID) connectionId: String)
      }
  </code></pre>
  <p>TCP Message Endpoint:</p>
  <pre><code>    @ServiceActivator(inputChannel = &quot;sendAcknowledgement&quot;, outputChannel = &quot;doProcessing&quot;)
      fun initialAck(message: Message&lt;ByteArray&gt;): Message&lt;ByteArray&gt; {
          gateway.send(&quot;ACK&quot;, message.headers[IpHeaders.CONNECTION_ID].toString())
          return message
      }
  
      @ServiceActivator(inputChannel = &quot;doProcessing&quot;, outputChannel = &quot;sendResponse&quot;)
      fun process(message: Message&lt;ByteArray&gt;, @Header(IpHeaders.CONNECTION_ID) connectionId: String): String? {
          /*** our process logic here ***/
      }
  
      @ServiceActivator(inputChannel = &quot;outboundErrorChannel&quot;)
      fun handleError(message: MessagingException) {
          log.error(&quot;error: %s&quot;, message)
      }
  
      override fun onApplicationEvent(event: TcpConnectionEvent) {
          when (event) {
              is TcpConnectionOpenEvent -&gt; {
                  log.debug { &quot;${event.connectionId} is attempting to connect&quot; }
              }
              is TcpConnectionCloseEvent -&gt; {
                  /*** kick off additional process here ***/
              }
          }
      }
  </code></pre>