java - Spring Integration TCP

标签 java spring tcp spring-integration

我想设置 Spring TCP 服务器-客户端应用程序。我需要一个服务器监听端口上的传入消息,例如 6666,客户端在不同的端口上发送消息,例如 7777。我遵循了 documentation ,但我一直卡在客户端希望收到响应的问题,但实际上,另一端只会收到来自客户端的消息,不会发送任何响应。所以,基本上,我经常收到这个错误:

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");

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

最佳答案

参见 reference manual .

网关用于请求/回复交互, channel 适配器用于单向交互。

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

关于java - Spring Integration TCP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42667403/

相关文章:

ruby-on-rails - 需要从我的基于云的服务器使用 Modbus over TCP/IP 与盒子通信

java - 通过Jtable修改记录到数据库

java - Bean 范围 ("Request")不起作用

java - .Net 可以加载 IL 文件(例如 java 类文件),还是必须加载整个程序集?

java - Spring 使用 JSONObject 字段反序列化对象

node.js - org.springframework.web.multipart.MultipartException : Could not parse multipart servlet request. ..流意外结束

c# - 从实际数据返回中对客户端心跳进行排序?

c# - 在 C# 中使用 Socket api 时 TCP 数据包如何到达

java - Jquery 错误 - 调用 Struts 2 操作时

java - 如何在 DropWizard 中验证单个参数