From 409cc95607f557be7a4b9dd55eaf9569ef1c3fa7 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Sat, 14 Apr 2012 11:04:27 -0400 Subject: [PATCH] INT-2511 Change Default Socket Timeout Previously, when a client connection factory was used by both an inbound and outbound adapter, the socket timeout defaulted to 10 seconds. This was inappropriate because, often, collaborating channel adapters are used for aysnchronous messaaging and, even when used for request/response, it is generatlly not appropriate to timeout the socket due to a lack of recent send activity. --- .../AbstractClientConnectionFactory.java | 15 ----- .../tcp/connection/DefaultTimeoutTests.java | 66 +++++++++++++++++++ src/reference/docbook/ip.xml | 32 ++++----- 3 files changed, 79 insertions(+), 34 deletions(-) create mode 100644 spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/DefaultTimeoutTests.java diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractClientConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractClientConnectionFactory.java index 7b4252fe8b..0cbe9ecf03 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractClientConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/AbstractClientConnectionFactory.java @@ -17,7 +17,6 @@ package org.springframework.integration.ip.tcp.connection; import java.net.Socket; -import java.net.SocketException; /** * Abstract class for client connection factories; client connection factories @@ -73,20 +72,6 @@ public abstract class AbstractClientConnectionFactory extends AbstractConnection if (listener != null) { connection.registerListener(listener); } - if (listener != null || this.isSingleUse()) { - if (this.getSoTimeout() < 0) { - try { - /* Default so-timeout, when we have a collaborating inbound adapter, - * may go to infinity in a future release; currently it's 10 seconds. - * While it makes sense in a request/reply scenario, it doesn't - * really for completely asynchronous communication between peers. - */ - socket.setSoTimeout(DEFAULT_REPLY_TIMEOUT); - } catch (SocketException e) { - logger.error("Error setting default reply timeout", e); - } - } - } TcpSender sender = this.getSender(); if (sender != null) { connection.registerSender(sender); diff --git a/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/DefaultTimeoutTests.java b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/DefaultTimeoutTests.java new file mode 100644 index 0000000000..389ac66073 --- /dev/null +++ b/spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/DefaultTimeoutTests.java @@ -0,0 +1,66 @@ +/* + * Copyright 2002-2012 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springframework.integration.ip.tcp.connection; + +import static org.junit.Assert.assertEquals; + +import java.net.Socket; + +import org.junit.Test; +import org.springframework.integration.Message; +import org.springframework.integration.ip.util.SocketTestUtils; +import org.springframework.integration.test.util.TestUtils; + +/** + * @author Gary Russell + * @since 2.2 + * + */ +public class DefaultTimeoutTests { + + @Test + public void test() throws Exception { + int port = SocketTestUtils.findAvailableServerSocket(); + TcpNetServerConnectionFactory server = new TcpNetServerConnectionFactory(port); + server.registerListener(new TcpListener() { + public boolean onMessage(Message message) { + return false; + } + }); + TcpNetClientConnectionFactory client = new TcpNetClientConnectionFactory("localhost", port); + client.registerSender(new TcpSender() { + public void addNewConnection(TcpConnection connection) { + } + public void removeDeadConnection(TcpConnection connection) { + } + }); + client.registerListener(new TcpListener() { + public boolean onMessage(Message message) { + return false; + } + }); + server.start(); + client.start(); + TcpConnection connection = client.getConnection(); + Socket socket = TestUtils.getPropertyValue(connection, "socket", Socket.class); + // should default to 0 (infinite) timeout + assertEquals(0, socket.getSoTimeout()); + connection.close(); + server.stop(); + client.stop(); + } + +} diff --git a/src/reference/docbook/ip.xml b/src/reference/docbook/ip.xml index 8755a69cbe..cb466dbc1a 100644 --- a/src/reference/docbook/ip.xml +++ b/src/reference/docbook/ip.xml @@ -709,23 +709,20 @@ - When a client connection factory is used by + Before the 2.2 release, + when a client connection factory was used by collaborating channel - adapters, the so-timeout attribute defaults - to the default reply timeout (10 seconds). This means that if - no data are received by the inbound adapter for this period of - time, the socket will be closed. + adapters, the so-timeout attribute defaulted + to the default reply timeout (10 seconds). This meant that if + no data were received by the inbound adapter for this period of + time, the socket was closed. - This may not be appropriate in a truly asynch environment, or - if you expect the server to take more than 10 seconds to respond - in a request/reply environment. - The timeout can be increased by setting the - so-timeout attribute on the connection - factory. - - - Setting the attribute to 0 will enable an infinite timeout. + This default behavior was not appropriate in a truly asynchronous + environment, so it now defaults to an infinite timeout. + You can reinstate the previous default behavior by setting the + so-timeout attribute on the client connection + factory to 10000 milliseconds. @@ -887,13 +884,10 @@ Y Y - Defaults to 0 (infinity), except when the connection - factory is used by collaborating adapters, and for + Defaults to 0 (infinity), except for server connection factories with single-use="true". - In those cases, it + In that case, it defaults to the default reply timeout (10 seconds). - See the section about collaborating adapters above - and java.net.Socket. setSoTimeout().