使用Spring Integration TCP IP套接字发送和接收数据

时间:2018-12-05 13:36:43

标签: spring spring-boot spring-integration tcp-ip

我正在创建一个简单的Spring Boot应用程序(PoC),以使用Spring集成TCP通过套接字将产品ID(字符串)从客户端发送到服务器。如果服务器上输入了正确的产品数据,则服务器将以我需要打印的产品详细信息作为响应。只需建立连接并通过发送适当的数据来获取响应。

请告诉我应该实现哪些类?出站/入站网关,消息通道,tcplisteners?我应该使用xml配置还是注释?我是SI的新手,如果您能给我一个有关如何实现它的想法,将会对我有很大的帮助。


这是我更新的集成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"/>

但是这仍然会打印出回显的结果。另外,当我从主类对abstractClientConnectionfactory调用getHost时,其显示为“ localhost”。如何确认连接是否处于活动状态?


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

****更新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"/> 

1 个答案:

答案 0 :(得分:2)

我建议您先走文档路线:https://docs.spring.io/spring-integration/docs/current/reference/html/ip.html,以研究Spring Integration在TCP / IP方面为您提供的功能。然后,跳到示例项目中,看看我们对配置和使用选项的建议:https://github.com/spring-projects/spring-integration-samples