2017-03-08 100 views
3

我想設置Spring TCP Server-Client應用程序。我需要一個服務器監聽端口上的傳入消息,例如6666,並且客戶端在不同的端口上發送消息,例如7777.我遵循documentation,但我堅持客戶期望的問題收到回覆,但實際上,另一端只會收到來自客戶端的消息,不會發送任何迴應。所以,基本上,我不斷收到此錯誤:Spring Integration TCP

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

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

我發現this答案類似的問題,所以我想答案在我的代碼整合。這是我的配置類:

@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) {} 
} 

}

這是我如何使用客戶端發送消息:

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

我怎樣才能安裝客戶端,因此它不等待爲了迴應?

回答

4

查看reference manual

網關用於請求/回覆交互,通道適配器用於單向交互。

使用TcpSendingMessageHandlerTcpReceivingChannelAdapter而不是入站和出站網關。

相關問題