Announcement Announcement Module
Collapse
No announcement yet.
How to close Server sockets after a reply is sent Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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:

    Code:
    <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="errors.global"
    		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:
    java.net.SocketException: Socket is closed
    	at java.net.Socket.getInputStream(Socket.java:788)
    	at org.springframework.integration.ip.tcp.connection.TcpNetConnection.getPayload(TcpNetConnection.java:81)
    	at org.springframework.integration.ip.tcp.connection.TcpMessageMapper.toMessage(TcpMessageMapper.java:49)
    	at org.springframework.integration.ip.tcp.connection.TcpNetConnection.run(TcpNetConnection.java:109)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
    	at java.lang.Thread.run(Thread.java:662
    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 TcpNetConnection.run, 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...

    https://jira.springsource.org/browse/INT-1707

    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)

    Comment


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

      Comment


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

        Comment

        Working...
        X