首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Spring集成TCP

Spring集成TCP
EN

Stack Overflow用户
提问于 2017-03-08 09:23:56
回答 1查看 3.2K关注 0票数 5

我想设置Spring服务器-客户端应用程序。我需要服务器监听端口上的传入消息,例如6666,以及客户机在另一个端口上发送消息,例如7777。我已经跟踪了文档,但是我遇到了客户端希望接收响应的问题,但实际上,另一端只会接收来自客户端的消息,不会发送任何响应。所以,基本上,我经常会收到这样的错误:

代码语言:javascript
复制
o.s.i.ip.tcp.TcpOutboundGateway          : Tcp Gateway exception

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

我找到了类似问题的答案,所以我尝试将答案集成到我的代码中。这是我的Config课程:

代码语言:javascript
复制
@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) {}
}

}

我是这样使用客户端发送消息的:

代码语言:javascript
复制
@Autowired
private Gateway gateway;
gateway.viaTcp("Some message");

如何设置客户端,使其不等待响应?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-08 13:45:46

参考手册

网关用于请求/应答交互,信道适配器用于单向交互.

使用TcpSendingMessageHandlerTcpReceivingChannelAdapter代替入站和出站网关。

票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/42667403

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档