我試圖在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是真實的。我正在詳細寫這篇文章,因爲我知道你是這個班的作者。 –