Spring integration spring-集成TCP网关,连接并从套接字接收数据
我正在尝试在Spring集成中实现TCP客户端。我有一个远程TCP服务器,它将数据泵入套接字。我的基于Spring的TCP客户机必须从该套接字接收数据 作为一个客户端,我不会将任何数据从我这边发送到服务器,只是连接并接收数据。看着这个,我明白这是不可能的。但是,收到的答案很旧,现在有可用的配置吗 如果您还有其他问题,请告诉我 @已更新配置Spring integration spring-集成TCP网关,连接并从套接字接收数据,spring-integration,Spring Integration,我正在尝试在Spring集成中实现TCP客户端。我有一个远程TCP服务器,它将数据泵入套接字。我的基于Spring的TCP客户机必须从该套接字接收数据 作为一个客户端,我不会将任何数据从我这边发送到服务器,只是连接并接收数据。看着这个,我明白这是不可能的。但是,收到的答案很旧,现在有可用的配置吗 如果您还有其他问题,请告诉我 @已更新配置 <bean id="javaSerializer" class="org.springframework.core.serializer.Default
<bean id="javaSerializer" class="org.springframework.core.serializer.DefaultSerializer" />
<bean id="javaDeserializer" class="org.springframework.core.serializer.DefaultDeserializer" />
<context:property-placeholder />
<!-- Client side -->
<int:gateway id="gw"
service-interface="com.my.client.SimpleGateway"
default-request-channel="input" default-reply-channel="replies" />
<int-ip:tcp-connection-factory id="client"
type="client" host="localhost" port="5678"
single-use="false" so-timeout="10000" serializer="javaSerializer"
deserializer="javaDeserializer" so-keep-alive="true"/>
<int:channel id="input" />
<int:channel id="replies">
<int:queue />
</int:channel>
<!-- <int-ip:tcp-outbound-gateway id="outGateway" request-channel="input"
reply-channel="reply" connection-factory="client" request-timeout="10000"
reply-timeout="10000" /> -->
<int-ip:tcp-outbound-channel-adapter
id="outboundClient" channel="input" connection-factory="client" />
<int-ip:tcp-inbound-channel-adapter
id="inboundClient" channel="replies" connection-factory="client"
client-mode="true" retry-interval="10000" auto-startup="true" />
我的网关类:
public interface SimpleGateway {
public String receive();
}
tcprevivingChannelAdapter
(
)通常在服务器模式下运行-它在套接字上侦听到客户端的传入连接
然而,clientMode
(clientMode
)布尔值正是为这个用例添加的。它将连接到服务器并接收来自服务器的传入数据。如果连接丢失,它将重试连接(按照配置计划)
见:
通常,入站适配器使用type=“server”连接工厂,它侦听传入的连接请求。在某些情况下,需要反向建立连接,即入站适配器连接到外部服务器,然后在该连接上等待入站消息
在入站适配器上使用client mode=“true”
支持此拓扑。在这种情况下,连接工厂的类型必须为client
,并且必须将一次性使用设置为false
另外两个属性用于支持此机制:重试间隔
指定连接失败后框架尝试重新连接的频率(以毫秒为单位)<代码>计划程序
用于提供任务计划程序,用于计划连接尝试,并测试连接是否仍处于活动状态
如果未提供调度程序,则使用默认的
taskScheduler
bean。感谢您的快速回复。我在GIT上以你为例。我将执行上述配置,并返回结果。希望我能得到它!:)我尝试了解释过的配置。但是我没有看到服务器的响应。我不确定我是否错过了任何东西或可能是不正确的。我用方法定义了一个接口网关:publicstringreceive(stringtext)代码>。它只是从TCP服务器接收数据。xml配置如上述文档所述。请查找以上配置。在我的客户机TCPClient
中,我调用了receive()
,但我没有收到任何数据。更新上述配置后,我尝试使用自定义desializer,而不是java反序列化器。好消息是,我看到数据进入反序列化()
方法,但没有进入我的TCPClient.receive()
。在run方法中,它被困在org.springframework.integration.ip.tcp.connection.TcpNetConnection
,因为okToRun是真的。我写这篇详细的文章是因为,我知道,你是这门课的作者;您的配置与您的问题不一致作为客户端,我不会将任何数据从我这边发送到服务器,只需连接并接收数据。
您的客户端确实在发送数据。早些时候,我只是尝试连接并接收数据,但不起作用。所以我想,让我试着发送一些数据并接收回复。现在我可以得到响应了,我看到数据出现在我的反序列化()方法中。这不会传播到它的调用者。这是现在的问题阶段。
while(true) {
try {
System.out.println("Waiting for client on port " +
serverSocket.getLocalPort() + "...");
Socket server = serverSocket.accept();
System.out.println("Just connected to "
+ server.getRemoteSocketAddress());
DataOutputStream out =
new DataOutputStream(server.getOutputStream());
out.write("ACK\r\n".getBytes());
out.flush();
//server.close();
} catch(SocketTimeoutException s) {
System.out.println("Socket timed out!");
break;
} catch(IOException e) {
e.printStackTrace();
break;
}
}
public interface SimpleGateway {
public String receive();
}