membersound
membersound

Reputation: 86747

Which timeouts to set on Socket created with spring-integration?

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

The server offers a timeout of 1 minute.

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?

The following properties are available:

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

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

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?

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).

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

Upvotes: 2

Views: 1913

Answers (1)

Gary Russell
Gary Russell

Reputation: 174564

factory.setSoTimeout();

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

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

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

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

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

Upvotes: 3

Related Questions