2016-05-12 102 views
2

我試圖在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(); 
} 

回答

1

TcpReceivingChannelAdapter<ip:tcp-inbound-channel-adapter/>)通常在服務器模式下運行 - 它偵聽套接字以傳入到客戶端的連接。

然而,一個clientModeclient-mode)布爾已被添加爲精確此用例。它將連接到服務器並接收來自它的傳入數據。如果連接丟失,它將重試連接(按配置時間表)。

參見the documentation

通常情況下,入站適配器使用類型=「服務器」連接工廠,它監聽傳入連接請求。在某些情況下,需要建立反向連接,從而入站適配器連接到外部服務器,然後等待該連接上的入站消息。

通過在入站適配器上使用client-mode="true"來支持此拓撲。在這種情況下,連接工廠必須是client,並且必須將single-use設置爲false。

另外兩個屬性用於支持此機制:retry-interval指定(毫秒)框架在連接失敗後嘗試重新連接的頻率。 scheduler用於提供用於調度連接嘗試的TaskScheduler,並測試連接是否仍處於活動狀態。

如果未提供調度程序,則使用默認的taskScheduler bean。

+0

謝謝您的快速回復。我在GIT上跟隨你的例子。我會在上述配置上做,並且會返回結果。希望我能得到它! :) –

+0

我嘗試瞭解釋的配置。但是我看不到服務器的迴應。我不確定我是否錯過了任何東西或者可能不正確。我有一個用方法定義的接口網關:'public String receive(String text);'。它只是從TCP服務器接收數據。 xml配置如文檔中所述。請在配置上方找到。在我的客戶端'TCPClient'中,我調用'receive()',但沒有收到數據。 –

+0

更新上面的配置後,我嘗試使用自定義desializer,而不是java deserialilzer。好消息是,我看到數據進入'deserialize()'方法,而不是我的'TCPClient.receive()'。這是陷入在'中的run方法org.springframework.integration.ip.tcp.connection.TcpNetConnection ',因爲okToRun是真實的。我正在詳細寫這篇文章,因爲我知道你是這個班的作者。 –