简体   繁体   中英

Send and receive data using Spring Integration TCP IP socket

I am creating a simple spring boot application(PoC) to send product id's (string) from client to server over socket using Spring integration TCP. If the server is hit with correct product data, the server will respond back with the product details which I need to print. Just need to establish a connection and get the response by sending proper data.

Please tell me what are the classes I am supposed to implement? outbound/inboud gateways,messsage channels, tcplisteners? Should I go with xml configuration or annotations? I am new to SI and would be of great help if you could give me an idea on how to implement it.


Here is my updated integration xml.

<int-ip:tcp-connection-factory id="client" type="client" host="XX.XX.XX.99" port="9XXX" single-use="true" so-timeout="10000" />

<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"/>

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

<int:service-activator input-channel="clientBytes2StringChannel" ref="echoService" method="test"/> 

<bean id="echoService" class="org.springframework.integration.samples.tcpclientserver.EchoService"/>

<int:channel id="toSA"/>

But this still prints the echoed result. Also, when I call getHost on abstractClientConnectionfactory from main class, its showing "localhost". How can I confirm if the connection is active?


<int:gateway id="gw"
             service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
             default-request-channel="input"/>

<int-ip:tcp-connection-factory id="client" type="client" host="xx.xx.xx.99"
                         port="9xxx"
                         single-use="false" so-timeout="300000" using-nio="false"
                         so-keep-alive="true" serializer="byteArrayRawSerializer"
                             deserializer="byteArrayRawSerializer"/>


<bean id="byteArrayRawSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayRawSerializer" />


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

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


<int:service-activator input-channel="toSA" ref="echoService" method="test"/> 

<bean id="echoService" class="org.springframework.integration.samples.tcpclientserver.EchoService"/>

<int:channel id="toSA"/> 

<int:transformer id="errorHandler" input-channel="errorChannel" expression="payload.failedMessage.payload + ':' + payload.cause.message"/>
<int:channel id="responseBytes2StringChannel"></int:channel>

**** Update SI xml ****

<int:gateway id="gw"
             service-interface="org.springframework.integration.samples.tcpclientserver.SimpleGateway"
             default-request-channel="objectIn"/>

<int:channel id="objectIn" />

<int-ip:tcp-connection-factory id="client"
                               type="client"
                               host="xx.xx.xx.99"
                               port="9xxx"
                               single-use="true"
                               so-timeout="50000"
                               using-nio="false"
                               so-keep-alive="true"/>
                               <!-- 
                               serializer="byteArrayLengthSerializer"
                               deserializer="byteArrayLengthSerializer"

<bean id="byteArrayLengthSerializer" class="org.springframework.integration.ip.tcp.serializer.ByteArrayLengthHeaderSerializer " />
-->
<int:payload-serializing-transformer input-channel="objectIn" output-channel="objectOut"/>

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

<int:payload-deserializing-transformer input-channel="bytesIn" output-channel="objectOut" />

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



<int:service-activator input-channel="toSA" ref="echoService" method="test"/> 

<bean id="echoService" class="org.springframework.integration.samples.tcpclientserver.EchoService"/>

<int:channel id="objectOut"/> 
<int:channel id="toSA"/> 
<int:channel id="bytesIn"/> 

I suggest you to go the Documentation route first: https://docs.spring.io/spring-integration/docs/current/reference/html/ip.html to investigate what Spring Integration provides for you in regards of TCP/IP. Then it would be great to jump into samples project to see what we suggest for configuration and usage options: https://github.com/spring-projects/spring-integration-samples

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM