2016-05-12 24 views
2

私はSpring統合でTCPクライアントを実装しようとしています。私はソケットにデータをポンピングするリモート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を見ると、これは不可能であると私は理解していました。しかし、受け取った回答はかなり古いですが、現在利用可能な設定はありますか?

ご質問がある場合はお知らせください。

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(); 
} 

は、コンフィギュレーションここで

<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クライアントと@updated

答えて

1

TcpReceivingChannelAdapter<ip:tcp-inbound-channel-adapter/>)は通常、サーバーモードで実行されます。クライアントへの着信接続をソケットで待機します。

しかし、正確にこのユースケースのためにclientModeclient-mode)ブール値が追加されました。サーバーに接続し、サーバーから受信データを受信します。接続が失われた場合は、(構成スケジュールで)接続を再試行します。

the documentationを参照してください:

通常、インバウンドアダプタは、着信接続要求をリスニングタイプ=「サーバー」接続ファクトリを使用しています。場合によっては、逆方向の接続を確立することが望ましく、それによってインバウンド・アダプターは外部サーバーに接続し、その接続上のインバウンド・メッセージを待機します。

このトポロジは、インバウンドアダプタでclient-mode="true"を使用することでサポートされています。この場合、接続ファクトリはclientで、single-useをfalseに設定する必要があります。

このメカニズムをサポートするために、2つの追加属性が使用されます。retry-intervalは、接続が失敗した後にフレームワークが再接続を試みる頻度をミリ秒単位で指定します。 schedulerは、接続の試行をスケジュールし、接続がまだアクティブであることをテストするために使用されるTaskSchedulerを提供するために使用されます。

スケジューラが提供されない場合は、デフォルトのtaskScheduler Beanが使用されます。

+0

ご連絡ありがとうございます。私はこれをGITであなたの例にしています。私は上記の設定を行い、結果を返します。うまくいけば私はそれを手に入れよう! :) –

+0

説明した設定で試しました。しかし、私はサーバーからの応答を参照してください。私は何かを見逃しているのか間違っているのかわからない。メソッドとして定義されたインターフェイスゲートウェイが 'public String receive(String text);'です。 TCPサーバーからデータを受信するだけです。 xmlの設定については、前述のドキュメントを参照してください。上記の設定を確認してください。 My Clientの 'TCPClient'では' receive() 'を呼び出しますが、データは受信されません。 –

+0

上記の設定を更新した後、私はカスタムdesializer、むしろjava deserialilzerを使ってみました。良いことは、私はデータが 'deserialize()'メソッドに入ってくるのを見ていますが、私の 'TCPClient.receive()'にはデータが入っていないことです。 okToRunがtrueであるので、実行メソッドで 'org.springframework.integration.ip.tcp.connection.TcpNetConnection 'に固執しています。私はあなたがこのクラスの著者であるので、これを詳しく書いています。 –

関連する問題