codesmith
codesmith

Reputation: 1431

How to implement this TCP stream reader in Spring Integration?

I would like a Spring Integration implementation of a stream reader. Another application (outside of java) sends streams of data (delimited by dollar-signs) to port 9999. This server listens.

First I made sure the stream was streaming by connecting to it with telnet 127.0.0.1 9999.

Then I created a simple java application with the following method. This is working currently.

public void readStream() throws IOException{
    Scanner s = null;
    try {
        Socket skt = new Socket("localhost", 9999);
        BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(
                        skt.getInputStream()));
        s = new Scanner(bufferedReader);
        s.useDelimiter("[$]");
        System.out.println(s);
        while (s.hasNext()) {
            System.out.println("----------------------");
            System.out.println(s.next());
        }
    } finally {
        if (s != null) {
            s.close();
        }
    }
}

Now, I would like to implement this in Spring Integration framework. I looked at https://github.com/spring-projects/spring-integration-samples/tree/master/basic/tcp-client-server and http://docs.spring.io/autorepo/docs/spring-integration/2.0.0.M3/spring-integration-reference/html/stream.html. However I get confused where to start? What is needed to connect to the sending application? (I'm really new to the Spring Framework.)

The difficulty for me lies in the terminology. Should I create a TCP Inbound gateway? or a receiving channel adapter? or is it outbound because I'm requesting the stream??

EDIT after comments of Gary:

<bean class="org.springframework.integration.ip.tcp.serializer.ByteArraySingleTerminatorSerializer" id="deserializer1">
    <constructor-arg type="byte" value="$"/>
</bean>
<int-ip:tcp-connection-factory id="server" type="server" port="9999"
    deserializer="deserializer1"
/>
<int-ip:tcp-inbound-channel-adapter id="adapter" connection-factory="server" request-channel="channel1"/>
<int:channel id="channel1" />

Upvotes: 5

Views: 1741

Answers (1)

Gary Russell
Gary Russell

Reputation: 174554

An inbound gateway is used when the server sends a reply to an inbound request. An inbound channel adapter (<int-ip:tcp-inbound-channel-adapter) is for one-way integration only - the client sends data only and does not receive replies.

You would need a server connection factory, configured to use a ByteArraySingleTerminatorSerializer configured with your $ delimiter, in the deserializer property.

Please use the latest documentation not the old version that was in your question.

Upvotes: 4

Related Questions