Search code examples
springtcpspring-integrationtcpclient

TCP Client Spring Integration with Legacy Server


I need some help with a migration to Spring Integration. Actually we have a component based in java.net package, it includes only a few classes with a java classic TCP Client, nothing fancy, that actually works to send some requests to a legacy server by tcp. This is the code for sending and receiving a message:

public String sendMessage(String info) {

    try {
        PrintStream ps = new PrintStream(clientSocket.getOutputStream());
        ps.println(info);

        InputStreamReader ir = new InputStreamReader(clientSocket.getInputStream());
        BufferedReader br = new BufferedReader(ir);

        return br.readLine();

    } catch (IOException e) {
        log.debug(e.getMessage());
    }
    return "";
}

The problem is that I want to start using Spring Integration in the client side, but I cannot change the server because it belongs to another company, but I don't receive the response from the server, I tried a lot of configuration and no luck at all. This is my client configuration right now:

            <int:gateway id="gw"
                 service-interface="pe.financieraoh.core.cointegrador.tcp.SimpleGateway"
                 default-request-channel="input"/>

    <int-ip:tcp-connection-factory id="client"
                                   type="client"
                                   host="localhost"
                                   port="7777"
                                   single-use="false"
                                   serializer="connectionSerializeDeserialize"
                                   deserializer="connectionSerializeDeserialize"
                                    />
    <int:channel id="input" />

    <int-ip:tcp-outbound-gateway id="outGateway"
                                 request-channel="input"
                                 reply-channel="clientBytes2StringChannel"
                                 connection-factory="client"
                                 request-timeout="10000"
                                 reply-timeout="10000" />

<bean id="connectionSerializeDeserialize" 
          class="org.springframework.integration.ip.tcp.serializer.ByteArrayLfSerializer"/>

<int:object-to-string-transformer id="clientBytes2String"
                                      input-channel="clientBytes2StringChannel" />

I tried a lot and always received a TimeOutConnection Exception, I'm pretty sure the server did not receive my request, but I cannot be sure, because I dont have access to the server logs. My only guide is the legacy code in the client works fine. Thanks for your help.

Here is the logs from the server, i tried several kind of de/serializer with the same result, 2020-06-21 23:47:10.377 ERROR 1 --- [nio-8080-exec-7] o.s.i.ip.tcp.TcpOutboundGateway : Tcp Gateway exception org.springframework.integration.MessageTimeoutException: Timed out waiting for response at org.springframework.integration.ip.tcp.TcpOutboundGateway.getReply(TcpOutboundGateway.java:243) ~[spring-integration-ip-5.3.0.RELEASE.jar:5.3.0.RELEASE]


Solution

  • For the equivalent of println and readLine you need a ByteArrayLfSerializer (if running the client on Linux/Unix/Mac) or the default ByteArrayCrLfSerializer (if running the client on Windows).

    If that doesn't work, use WireShark or similar to look at the network traffic.