Announcement Announcement Module
Collapse
No announcement yet.
Spring Integration - Simple TCP Client Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring Integration - Simple TCP Client

    Hello

    I have just started using Spring Integration for sharing data between two heterogenous systems.

    Question 1: To begin with, I took the tcp-client-server sample available in the forum and tried to get a better understanding. The tcp-client-server sample as a standalone unit produces expected result when ran using Maven. However, while I run TelnetServer class as java application from eclipse as server and Telnet as client, I do not see communication between the two. I see a Connect Failed in the telnet window. What could be wrong?

    Problem I am trying to solve:
    Wantd to share data between two systems. The source reads a delimited flat file, converts it into a xml string and writes it to a TCP port. The target listens to the port, retrieves the data, processes it and sends a response back. To implement source system, I referred to the tcp-client-server sample and used only the client portion of code.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans:beans xmlns:beans="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://www.springframework.org/schema/integration"
    	xmlns:ip="http://www.springframework.org/schema/integration/ip"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    		http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd
    		http://www.springframework.org/schema/integration/ip http://www.springframework.org/schema/integration/ip/spring-integration-ip.xsd">
    
    	<!-- Client side -->
    	
    <gateway id="gw" service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
            default-request-channel="input"/>
    
    	<ip:tcp-connection-factory id="client"
    		type="client"
    		host="localhost"
    		port="9001"
    		single-use="true"
    		so-timeout="10000"
            />
    	
    	<!--
    	<channel id="input" />
    	-->
    	<ip:tcp-outbound-gateway id="outGateway"
    		request-channel="input"
    		reply-channel="clientBytes2StringChannel"
    		connection-factory="client"
    		request-timeout="10000"
    		reply-timeout="10000"
    		/>
    
    	<transformer id="clientBytes2String"
    		input-channel="clientBytes2StringChannel"
    		expression="new String(payload)"
    		/>
    </beans:beans>
    Question 2: The program runs fine on Windows XP. However, when I move it to a Mac, I do not get the response. The exception stack shows Exception in sendAndReceive() and timesout. What could be the problem?

    Question 3: I am using Spring Batch 2.1.8 to read and convert flat file to xml structure. Spring Integration 2.0.5 and Spring 3.0.5. Would there be any changes to the schema references based on the version of the Spring modules used?

    Question 4: When a transformer is defined, do we need to explicitly define the channel? I observe the clientBytes2StringChannel is not defined in the sample. Does the container create one automatically?

    Appreciate your assistance.
    Chandra

  • #2
    #1 works fine for me - are you connecting to the right port?

    In eclipse console...

    Code:
    Press Enter/Return in the console to exit
    In terminal...

    Code:
    $ telnet localhost 11111
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    hello 
    echo:hello
    ^]
    
    telnet> quit
    Connection closed.
    You can verify the server is listening (netstat -lntp | grep 11111 on linux or mac, netstat -ano | find "11111" on Windows).

    #2 Turn on debug logging in the log4j.xml

    Code:
    	<logger name="org.springframework.integration">
    		<level value="debug" />
    	</logger>
    #3 No

    #4 Yes, the framework will generate a default DirectChannel when parsing the transformer if it's not explicitly defined. This is true for any consumer.

    I am going to be traveling for the next few hours so this will be my last response for a while.

    Comment


    • #3
      Gary

      Thanks for the answers.

      Earlier, I was using netcat. I tried with telnet and it produces the expected result.
      I used the tcp-client-server sample to create a client component. Wrote a standalone TCP Server simulator and tried to pass message between them. The programs worked fine in Windows environment; however, when I moved them to Mac, I observed a time out. On further reading and analysis, I found that the TCP Server program appends a OS specific newline character at the end of response string.
      Code:
      public static final String NEWLINE= System.getProperty("line.separator");
      .........
      response.append(NEWLINE);
      Instead of appending NEWLINE. I hardcodes \r\n and it worked fine.

      Question 1: Irrespective of the operating system newline character, the ByteArraySerializer always expect a /r/n at the end. Is this understanding correct?
      Question 2: I am using Spring Batch to read data from a delimited flat file and convert it into XML string. I am trying to write the XML string into the TCP port in a class that overrides the ItemWriter and waiting for response.

      Code:
      TCPWriter implements ItemWriter{
      
          // gateway injected declaratively
          private SimpleGateway gateway;
      
          public void write(List payloadList){
               StringWriter writer;
               // use JAXB Marshaller to create xml string and store in writer
               // send data to TCP port using gateway
               String result = gateway.send(writer);
               log.info(result);
          }
      }
      Is this a clean and correct approach? Please advice.

      Thanks
      Chandra

      Comment


      • #4
        #1 Yes, the Telnet protocol defines the EOL as \r\n, regardless of platform. The ByteArrayCrLfSerializer always uses \r\n; there are other serializers StxEtx, etc.

        #2 Yes, your approach is reasonable.

        Comment


        • #5
          Gary

          Thank you very much for your help.

          Wanted to share this piece of information. As mentioned earlier, when we used netcat and keyed in data and pressed enter, we did not see desired results. Later we observed that to generate a CRLF in netcat, we need to hit ctrl+v and Enter as Enter in netcat generates only a LF. To generate a CR, we need to press ctrl+v. The program works fine in Telnet as well as netcat.

          Thanks again.
          Chandra

          Comment

          Working...
          X