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 00954ebc05b..83a06f969af 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 @@ -40,6 +40,7 @@ * @author Artem Bilan * @author Christian Tzolov * @author Ngoc Nhan + * @author Jooyoung Pyoung * * @since 2.0 * @@ -85,8 +86,9 @@ protected void checkActive() { @Override protected TcpConnectionSupport buildNewConnection() { + SocketChannel socketChannel = null; try { - SocketChannel socketChannel = SocketChannel.open(); + socketChannel = SocketChannel.open(); setSocketAttributes(socketChannel.socket()); connect(socketChannel); TcpNioConnection connection = @@ -113,6 +115,14 @@ protected TcpConnectionSupport buildNewConnection() { 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) {