在使用 spring-integration 创建的 Socket 上设置哪些超时? [英] Which timeouts to set on Socket created with spring-integration?

查看:29
本文介绍了在使用 spring-integration 创建的 Socket 上设置哪些超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 bean 创建到服务器的客户端套接字连接:AbstractClientConnectionFactoryTcpOutboundGateway.

I have two beans creating a client socket connection to a server: AbstractClientConnectionFactory and TcpOutboundGateway.

服务器提供 1 分钟的超时时间.

The server offers a timeout of 1 minute.

问题:我必须在 bean 上设置哪些超时,以便 spring/java 不会在服务器超时之前终止连接?

Question: which timeouts do I have to set on the beans so that spring/java does not terminate the connection before the timeout of the server?

以下属性可用:

factory.setSoTimeout();
gateway.setRequestTimeout();
gateway.setRemoteTimeout();

从客户的角度来看,哪些超时设置是正确的?或者我应该将它们全部设置为等于 60000L?

Which of those timeouts is the correct one to set from a clients perspective? Or should I just set them all to equal 60000L?

我之所以这么问是因为我现在只使用 factory.setSoTimeout(60000L),并且在 10 秒后获得套接字超时.所以也许我必须另外设置网关超时?

I'm asking because I' just using factory.setSoTimeout(60000L) by now, and getting socket timeouts after 10sec. So maybe I have to additionally set the gateway timeouts?

我还发现 gateway.setRemoteTimeout(60000L) 仅在设置时防止超时.所以也设置这个值可能是正确的(虽然我不明白为什么超时必须配置两次).

I also discovered that gateway.setRemoteTimeout(60000L) prevents the timeout only when set. So it's probably correct to also set this value (though I don't understand why timeout has to be configured twice).

问题仍然是 .setRequestTimeout() 的用途.

Still the question remains what .setRequestTimeout() is for.

推荐答案

factory.setSoTimeout();

SO 超时设置在套接字本身上;如果在那段时间内没有收到回复,则读取器线程会出现异常.如果我们最近没有发送消息(意味着我们正在等待回复),则套接字已关闭.如果我们最近确实发送了一条消息,我们将等待另一个套接字超时,然后套接字关闭.

The SO timeout is set on the socket itself; if no reply is received within that time, the reader thread gets an exception. If we haven't sent a message recently (meaning we are expecting a reply), the socket is closed. If we did send a message recently we'll wait for one more socket timeout after which the socket is closed.

gateway.setRequestTimeout();

这仅适用于工厂 singleUse 为 false(意味着共享单个连接)的情况.如果另一个请求正在处理中,这是我们等待访问套接字的时间.由于 TCP 没有用于请求/回复关联的自然机制,我们不能有 2 个(或更多)请求未完成,因此第二个请求必须等到第一个请求完成.如果 singleUse 为真,则每个请求都使用一个新的套接字,因此不需要.CachingClientConnectionFactory 提供了一种使用共享套接字池的机制.同样,此超时不适用(但如果所有套接字都在使用中,则池会超时).

This only applies if the factory singleUse is false (meaning a shared single connection). It is the time we wait to get access to the socket if another request is in process. Since TCP has no natural mechanism for request/reply correlation, we can't have 2 (or more) requests outstanding so the second request has to wait until the first one completes. If singleUse is true a new socket is used for each request so this is not needed. The CachingClientConnectionFactory provides a mechanism to use a pool of shared sockets. Again, this timeout does not apply (but the pool has a timeout if all the sockets are in use).

gateway.setRemoteTimeout();

gateway.setRemoteTimeout();

这是网关本身等待回复的时间;如果超时,则套接字将关闭.

This is how long the gateway itself will wait for a reply; if this expires, the socket is closed.

SO timeout 和 remoteTimeout 有效地做同样的事情;只是有不同的实现.

SO timeout and remoteTimeout effectively do the same thing; just with different implementations.

您可以将两者至少设置为您期望请求花费的时间,或者将 SO 超时设置为默认值(无穷大).

You can set both to at least the time you expect a request to take, or leave the SO timeout to default (infinity).

这篇关于在使用 spring-integration 创建的 Socket 上设置哪些超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆