TcpInboundGateway 服务器配置 - 超时要发送的自定义消息

TcpInboundGateway Server Configuration- on timeout a custom message to be sent

我正在使用 Spring 与 Spring Boot 的集成。我有一个带有 TcpInboundGateway 设置的 TCP 服务器和一个包含业务逻辑的转换器,它在处理完成时 returns 字符串。当我通过套接字接收消息时,此设置工作正常。

@Bean
public TcpInboundGateway paWebserviceInGate() {
    TcpInboundGateway inGateway = new TcpInboundGateway();
    inGateway.setConnectionFactory(connectionFactory.serverConnectionFactory(port));
    inGateway.setRequestChannelName("paWebserviceInputChannel");
    inGateway.setReplyTimeout(5000);//To configure the timeout - but it does not work 
    return inGateway;
}

@Transformer(inputChannel = "paWebserviceInputChannel")
public String consume(byte[] bytes) {
    String message = new String(bytes);
    return paMessageHandler.processPAIndiators(message);
}

ConnectionFactory.java:

public AbstractServerConnectionFactory serverConnectionFactory(int port) {
    final AbstractServerConnectionFactory connectionFactory = new TcpNetServerConnectionFactory(port);
    connectionFactory.setSerializer(customDeserializer);
    connectionFactory.setDeserializer(customDeserializer);
    connectionFactory.setSoKeepAlive(false);
    // connectionFactory.setSoTimeout(timeout);
    return connectionFactory;
}

现在,我有一个要求,即在 5 秒超时时,我需要向客户端发送一条自定义消息。假设服务需要 10 秒,我需要在 5 秒后发送自定义消息以通知客户端超时。 (请注意,我无法控制客户端;因此我无法在客户端添加超时)。

我在 TcpInboundGateway 上使用了 setReplyTimeout;但它不会在 5 秒后超时。

我提到了这个 post

此 post 解释了 Artem Bilan 建议的 xml 配置。 谁能帮忙 Java 配置?我在配置上面的代码以包含超时时感到困惑。我应该使用 IntegrationFlows 吗?

提前致谢。

框架中没有内置任何东西可以做到这一点;我开了一个New Feature issue.

回复计时器仅在线程 return 到达网关时启动;它仅适用于异步处理;您将不得不在不同的线程上处理您的请求。但这仍然无济于事,因为网关只是在回复超时后释放线程。

在当前状态下,您将不得不在服务中自行完成。

  • 排队工作给任务执行者并等待回复
  • 如果没有收到回复return您的错误情况

注意一旦你回复了网关,你就不能再发送真正的回复了。