我试图在Spring集成中实现TCP Client。我有一个远程TCP服务器,将数据泵入套接字。基于Spring的TCP客户端必须从该套接字接收数据。spring -integration 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,我明白这是不可能的。但是,收到的答案相当陈旧,现在有没有可用的配置?
如果您还有其他问题,请让我知道。
@updated与配置
<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();
}
谢谢您的快速回复。我在GIT上跟随你的例子。我会在上述配置上做,并且会返回结果。希望我能得到它! :) –
我尝试了解释的配置。但是我看不到服务器的回应。我不确定我是否错过了任何东西或者可能不正确。我有一个用方法定义的接口网关:'public String receive(String text);'。它只是从TCP服务器接收数据。 xml配置如文档中所述。请在配置上方找到。在我的客户端'TCPClient'中,我调用'receive()',但没有收到数据。 –
更新上面的配置后,我尝试使用自定义desializer,而不是java deserialilzer。好消息是,我看到数据进入'deserialize()'方法,而不是我的'TCPClient.receive()'。这是陷入在'中的run方法org.springframework.integration.ip.tcp.connection.TcpNetConnection ',因为okToRun是真实的。我正在详细写这篇文章,因为我知道你是这个班的作者。 –