[英]Spring Integration Tcp connection single-use VS setSoKeepAlive
I was using我正在使用
TcpNetClientConnectionFactory cf = new TcpNetClientConnectionFactory(host, port);
for TcpOutboundGateway, normally TcpOutboundGateway is working req/reply order but in my case I extend TcpOutboundGateway to receive arbitrary messages with MessageChannel.对于 TcpOutboundGateway,通常 TcpOutboundGateway 正在工作请求/回复顺序,但在我的情况下,我扩展 TcpOutboundGateway 以使用 MessageChannel 接收任意消息。 This is why ı thought that i should use
这就是为什么我认为我应该使用
cf.setLeaveOpen(true)
to keep connection open.保持连接打开。
Although i started to use that option, after long time when i called tcp server again i have received Exception like虽然我开始使用该选项,但很长一段时间后,当我再次调用 tcp 服务器时,我收到了类似的异常
org.springframework.integration.MessageTimeoutException: Timed out waiting for response
org.springframework.integration.MessageTimeoutException:等待响应超时
but i did not understand why i am taking this error because i set "true" to keep connection open in my connection factory.但我不明白为什么我会出现此错误,因为我设置“true”以在我的连接工厂中保持连接打开。
THEN然后
I did some google and it was supposed to use CachingClientConnectionFactory, I understand that it is by default single-use=true and not supposed to change it false, but then i assume that connection will be open and close in my each request response transaction so is it obstacle to receive arbitrary data from server without any request from client?我做了一些谷歌,它应该使用 CachingClientConnectionFactory,我知道默认情况下它是 single-use=true 并且不应该将其更改为 false,但是我假设连接将在我的每个请求响应事务中打开和关闭所以在没有来自客户端的任何请求的情况下从服务器接收任意数据是否有障碍?
OR或者
How should i keep open connection between client and server?我应该如何保持客户端和服务器之间的开放连接? should i use
我应该使用
cf.setSoKeepAlive(true)?
cf.setSoKeepAlive(true)?
to keep connection open?保持连接打开? Are
是
cf.setSoKeepAlive(true) and cf.setLeaveOpen(true)
cf.setSoKeepAlive(true) 和 cf.setLeaveOpen(true)
same with each other?彼此一样吗?
EDIT
编辑
Also when i use cf.setSoKeepAlive(true), after 1 hour i got same exception too.此外,当我使用 cf.setSoKeepAlive(true) 时,1 小时后我也遇到了同样的异常。
Full code:完整代码:
private MessageChannel createNewSubflow(Message<?> message) {
String host = (String) message.getHeaders().get("host");
Integer port = (Integer) message.getHeaders().get("port");
boolean hasThisConnectionIrregularChannel = message.getHeaders().containsKey("irregularMessageChannelName");
Assert.state(host != null && port != null, "host and/or port header missing");
String flowRegisterKey;
if (hasThisConnectionIrregularChannel) {
flowRegisterKey = host + port + ".extended";
} else {
flowRegisterKey = host + port;
}
TcpNetClientConnectionFactory cf = new TcpNetClientConnectionFactory(host, port);
CachingClientConnectionFactory ccf = new CachingClientConnectionFactory(cf, 20);
ccf.setSoKeepAlive(true);
ByteArrayCrLfSerializer byteArrayCrLfSerializer = new ByteArrayCrLfSerializer();
byteArrayCrLfSerializer.setMaxMessageSize(1048576);
ccf.setSerializer(byteArrayCrLfSerializer);
ccf.setDeserializer(byteArrayCrLfSerializer);
TcpOutboundGateway tcpOutboundGateway;
if (hasThisConnectionIrregularChannel) {
String unsolicitedMessageChannelName = (String) message.getHeaders().get("irregularMessageChannelName");
DirectChannel directChannel = getBeanFactory().getBean(unsolicitedMessageChannelName, DirectChannel.class);
tcpOutboundGateway = new ExtendedTcpOutboundGateway(directChannel);
} else {
tcpOutboundGateway = new TcpOutboundGateway();
}
tcpOutboundGateway.setRemoteTimeout(20000);
tcpOutboundGateway.setConnectionFactory(ccf);
IntegrationFlow flow = f -> f.handle(tcpOutboundGateway);
IntegrationFlowContext.IntegrationFlowRegistration flowRegistration =
this.flowContext.registration(flow)
.addBean(ccf)
.id(flowRegisterKey + ".flow")
.register();
MessageChannel inputChannel = flowRegistration.getInputChannel();
this.subFlows.put(flowRegisterKey, inputChannel);
return inputChannel;
}
Why are you using the CachingClientConnectionFactory
?你为什么使用
CachingClientConnectionFactory
? It is not needed when you keep the connection open;保持连接打开时不需要它; it is intended to be used when you want to maintain multiple open connections.
当您想要维护多个打开的连接时使用它。
Timed out waiting for response
等待响应超时
Means the socket was open just fine (from the client's perspective) when you sent the request;意味着当您发送请求时套接字打开得很好(从客户端的角度来看); we just didn't get a reply.
我们只是没有得到答复。 This could mean that some network component (router) silently closed the socket due to inactivity.
这可能意味着某些网络组件(路由器)由于不活动而默默地关闭了套接字。 Keep-alives should help with that but it depends on your operating system and how often the TCP stack is configured to send keep alives.
Keep-alives 应该对此有所帮助,但这取决于您的操作系统以及 TCP 堆栈配置为发送 keep-alives 的频率。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.