2017-03-08 11 views
3

Spring TCP Server-Clientアプリケーションを設定したいと思います。たとえば、6666のようなポート上の着信メッセージを受信するサーバーが必要です。クライアントが別のポート(7777など)でメッセージを送信する必要があります。documentationに従っていますが、クライアントが期待している問題応答を受信しますが、実際には、もう一方のエンドはクライアントからのメッセージを受信し、応答を送信しません。だから、基本的に、私は常にこのエラーを取得しています:Spring統合TCP

o.s.i.ip.tcp.TcpOutboundGateway   : Tcp Gateway exception 

org.springframework.integration.MessageTimeoutException: Timed out waiting for response 

私は同様の質問へthis答えを見つけたので、私は私のコードで答えを統合しようとしました。

@Autowired 
private Gateway gateway; 
gateway.viaTcp("Some message"); 

私はセットアップクライアントが待機しないことができるようにする方法:私は、メッセージを送信するためにクライアントを使用する方法これは

@EnableIntegration 
@IntegrationComponentScan 
@Configuration 
public class Config { 

private int port = 6666; 

@MessagingGateway(defaultRequestChannel = "toTcp") 
public interface Gateway { 
    String viaTcp(String in); 
} 

@Bean 
@ServiceActivator(inputChannel = "toTcp") 
public TcpOutboundGateway tcpOutGate(AbstractClientConnectionFactory connectionFactory) { 
    TcpOutboundGateway gate = new TcpOutboundGateway(); 
    gate.setConnectionFactory(connectionFactory); 
    gate.setOutputChannelName("resultToString"); 
    gate.setRequiresReply(false); 

    return gate; 
} 

@Bean 
public TcpInboundGateway tcpInGate(AbstractServerConnectionFactory connectionFactory) { 
    TcpInboundGateway inGate = new TcpInboundGateway(); 
    inGate.setConnectionFactory(connectionFactory); 
    inGate.setRequestChannel(fromTcp()); 

    return inGate; 
} 

@Bean 
public ByteArrayRawSerializer serializer() { 
    return new ByteArrayRawSerializer(); 
} 

@Bean 
public MessageChannel fromTcp() { 
    return new DirectChannel(); 
} 

@MessageEndpoint 
public static class Echo { 

    @Transformer(inputChannel = "fromTcp", outputChannel = "toEcho") 
    public String convert(byte[] bytes) { 
     return new String(bytes); 
    } 

    @ServiceActivator(inputChannel = "toEcho") 
    public String upCase(String in) { 
     System.out.println("Server received: " + in); 
     return in.toUpperCase(); 
    } 

    @Transformer(inputChannel = "resultToString") 
    public String convertResult(byte[] bytes) { 
     return new String(bytes); 
    } 

} 

@Bean 
public AbstractClientConnectionFactory clientCF() { 
    TcpNetClientConnectionFactory tcpNet = new TcpNetClientConnectionFactory("localhost", 7777); 
    tcpNet.setDeserializer(serializer()); 
    tcpNet.setSerializer(serializer()); 
    tcpNet.setSingleUse(true); 
    tcpNet.setTaskExecutor(new NullExecutor()); 
    return tcpNet; 
} 

@Bean 
public AbstractServerConnectionFactory serverCF() { 
    TcpNetServerConnectionFactory tcp = new TcpNetServerConnectionFactory(this.port); 
    tcp.setSerializer(serializer()); 
    tcp.setDeserializer(serializer()); 
    return tcp; 
} 


public class NullExecutor implements Executor { 

    public void execute(Runnable command) {} 
} 

}

:これは私のConfigクラスであります応答のために?

答えて

4

reference manualを参照してください。

ゲートウェイ要求/応答相互作用をするためのもので、チャネルアダプタは、一方向の相互作用のためです。代わりに、インバウンドとアウトバウンドのゲートウェイの

使用TcpSendingMessageHandlerTcpReceivingChannelAdapter