Announcement Announcement Module
No announcement yet.
How to close Server sockets after a reply is sent Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to close Server sockets after a reply is sent

    Using spring-integration 2.0.0-RELEASE + Spring 3.0.5

    I have a straight TCP socket server receiving requests from external clients via a hand-made codec set up as follows:

    <ip:tcp-connection-factory id="tcpConnectionFactory"
    		type="server" port="10000"
    		deserializer="myCodec" serializer="myCodec"
    		single-use="true" using-nio="false" />
    <ip:tcp-inbound-gateway id="inGateway"
    		request-channel="protocol.stringInput" reply-channel="protocol.stringOutput"
    		connection-factory="tcpConnectionFactory" error-channel=""
    		reply-timeout="10000" />
    In the app, requests/replies are one-shot by design and the server needs to close the socket once a reply has been sent. As it stands, I can't see a sensible way of doing this by configuration in the inbound gateway .

    At present we are closing the outputstream when the codec serialises a response which is less than ideal as it leads to lots of spurious exceptions when the TcpNetConnection tries to loop to the next message in :

    Code: Socket is closed
    	at org.springframework.integration.ip.tcp.connection.TcpNetConnection.getPayload(
    	at org.springframework.integration.ip.tcp.connection.TcpMessageMapper.toMessage(
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(
    	at java.util.concurrent.ThreadPoolExecutor$
    The intention appears to be (maybe?) that the codec should throw a SoftEndOfStreamException when it tried to read from a closed socket as per ByteArrayClRfSerializer but it seems that the SocketException is thrown in TcpNetConnection.getPayload.

    Looking at the final condition in, the worker seems to be able to cope with closing the connection after receiving a single message but not after a response is sent (e.g. when this.sender is not null).

    Any suggestions?

    I recall Camel having a magic "close the connection when this message is sent" header somewhere, which dealt with this.

  • #2
    Please try upgrading to 2.0.2.RELEASE; I believe this problem is fixed. See...

    If soLinger is anything other than 0, imediately close the socket after a send when appropriate:

    A. Single use socket, and
    1. Server side (reply)
    2. Send-only socket on the client side (outbound channel adapter)


    • #3
      I think Gary meant 2.0.3, since 2.0.2 had a major issue (although unrelated to this problem).


      • #4
        Yep, that seems to fix it, thanks.