diff --git a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java index e23a4a4c45..221d789474 100644 --- a/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java +++ b/spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java @@ -39,6 +39,7 @@ import org.springframework.util.Assert; * @author Gary Russell * @author Artem Bilan * @author Christian Tzolov + * @author Jooyoung Pyoung * * @since 2.0 * @@ -84,8 +85,9 @@ public class TcpNioClientConnectionFactory extends @Override protected TcpConnectionSupport buildNewConnection() { + SocketChannel socketChannel = null; try { - SocketChannel socketChannel = SocketChannel.open(); + socketChannel = SocketChannel.open(); setSocketAttributes(socketChannel.socket()); connect(socketChannel); TcpNioConnection connection = @@ -112,6 +114,14 @@ public class TcpNioClientConnectionFactory extends return wrappedConnection; } catch (IOException e) { + try { + if (socketChannel != null) { + socketChannel.close(); + } + } + catch (IOException e2) { + logger.error(e2, "Error closing socket channel"); + } throw new UncheckedIOException(e); } catch (InterruptedException e) {