gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 09:17:29 25 4
gpt4 key购买 nike

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

@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 秒后不会超时。

我引用了这篇文章reply-timeout meaning in tcp-inbound-gateway in spring integration

这篇文章解释了 Artem Bilan 建议的 xml 配置。任何人都可以帮忙进行Java 配置吗?在配置上面的代码以包含超时时,我感到很困惑。我应该使用 IntegrationFlows 吗?

提前致谢。

最佳答案

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

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

在当前状态下,您必须在服务中自己完成此操作。

  • 排队等待任务执行者回复
  • 如果没有收到回复,请返回您的错误情况

注意一旦您回复网关,您就无法发送真正的回复。

关于java - TcpInboundGateway 服务器配置 - 超时时发送自定义消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58796500/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com