Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Integration receive and send messages over a tcp connection

I'm trying to implement the following scenario using Spring Integration:

  • I need to connect to a server via TCP IP and process messages received.
  • I need to send messages to the same server from which receive and process responses.

This is my config:

 <channel id="input">

        <interceptors>
            <wire-tap channel="logger"/>
        </interceptors>
    </channel>


    <logging-channel-adapter id="logger" level="DEBUG" log-full-message="true"/>

    <ip:tcp-connection-factory id="factory" type="client" host="localhost" port="9004" single-use="false"
                               using-nio="true" deserializer="javaDeserializer"/>

    <ip:tcp-inbound-channel-adapter id="inbound" channel="input" connection-factory="factory" client-mode="true"
                                    retry-interval="5000"/>

I can receive messages sent from the server, but I have no idea how to convert a string. javaDeserializer is not invoked when messages arrive.

2014-01-19 05:47:20 DEBUG TcpNioConnection:380 - localhost:9004:74154fb2-f77c-4036-9142-e756e53a6ac6 Reading...
2014-01-19 05:47:20 DEBUG TcpNioConnection:324 - Read 26 into raw buffer
2014-01-19 05:47:25 DEBUG TcpNioConnection:380 - localhost:9004:74154fb2-f77c-4036-9142-e756e53a6ac6 Reading...
2014-01-19 05:47:25 DEBUG TcpNioConnection:324 - Read 26 into raw buffer
2014-01-19 05:47:30 DEBUG TcpNioConnection:380 - localhost:9004:74154fb2-f77c-4036-9142-e756e53a6ac6 Reading...
2014-01-19 05:47:30 DEBUG TcpNioConnection:324 - Read 26 into raw buffer
2014-01-19 05:47:35 DEBUG TcpNioConnection:380 - localhost:9004:74154fb2-f77c-4036-9142-e756e53a6ac6 Reading...
2014-01-19 05:47:35 DEBUG TcpNioConnection:324 - Read 26 into raw buffer

With this example, you can send messages and process the reply from the server, but the connection will close and not kept listening to the messages from the server. Any idea how to solve this?

Thanks in advance.

Eduardo

like image 479
earandap Avatar asked Dec 28 '25 15:12

earandap


1 Answers

TCP is a streaming protocol and has no concept of "messages". The framework uses a serializers (for output) and deserializers (for input) to encode/decode messages to/from the stream.

The framework has a number of standard deserializers (e.g. messages are delimited by \n or \r\n etc).

If your data doesn't fit into one of these standard formats, you need to provide a custom deserializer. A good place to start would be to look at the standard deserializers and subclass the AbstractByteArraySerializer.

like image 122
Gary Russell Avatar answered Dec 30 '25 15:12

Gary Russell