3
votes

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

1
You need to explain the problem more clearly; is the server sending java serialized objects? if so, this log implies to me that the messages are incomplete and the deserializer is waiting for more data. Take a thread dump (with jstack or VisualVM) to see what the threads are doing.Gary Russell
the server sends text strings like this /*00FF99*/. What I need is to capture those strings. They really are coming but do not know how to convert it be stored in database, for example.earandap

1 Answers

8
votes

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.