【发布时间】:2017-06-28 04:33:07
【问题描述】:
我正在尝试在 Spring 集成中实现 TCP 客户端。我有一个远程 TCP 服务器,它将数据泵入一个套接字。我的基于 Spring 的 TCP 客户端必须从该套接字接收数据。
作为客户端,我不会从我这边向服务器发送任何数据,只是连接和接收数据。看着这个http://forum.spring.io/forum/spring-projects/integration/94696-want-to-configure-simple-tcp-client-to-receive-data-from-java-based-tcp-server?view=thread,我明白这是不可能的。但是,收到的答案已经很老了,现在有什么可用的配置吗?
如果您还有其他问题,请告诉我。
@更新配置
<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" />
这是我的远程 TCP 客户端:
final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
context.load("classpath:config.xml");
context.registerShutdownHook();
context.refresh();
final SimpleGateway gateway = context.getBean(SimpleGateway.class);
int i=0;
while(i++<10){
String h = gateway.receive();
System.out.println(System.currentTimeMillis()+h);
我的 TCP 模拟服务器:
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();
}
【问题讨论】: