Skip to content

Commit 1df9186

Browse files
garyrussellartembilan
authored andcommitted
GH-3299: Fix client connectionId for TCP/NIO
Resolves #3299 Connect before creating the `TcpNioConnection` object and publishing the `TcpConnectionOpenEvent`. This was a regression caused by supporting connect timout; which moved the connect to after the object was created and event published, causing the `connectionId` to start with `unknown`. **cherry-pick to 5.3.x, 5.2.x**
1 parent f90473f commit 1df9186

File tree

2 files changed

+23
-13
lines changed

2 files changed

+23
-13
lines changed

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioClientConnectionFactory.java

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ protected TcpConnectionSupport buildNewConnection() {
8787
try {
8888
SocketChannel socketChannel = SocketChannel.open();
8989
setSocketAttributes(socketChannel.socket());
90+
connect(socketChannel);
9091
TcpNioConnection connection =
9192
this.tcpNioConnectionSupport.createNewConnection(socketChannel, false, isLookupHost(),
9293
getApplicationEventPublisher(), getComponentName());
@@ -98,18 +99,6 @@ protected TcpConnectionSupport buildNewConnection() {
9899
}
99100
TcpConnectionSupport wrappedConnection = wrapConnection(connection);
100101
initializeConnection(wrappedConnection, socketChannel.socket());
101-
socketChannel.configureBlocking(false);
102-
socketChannel.connect(new InetSocketAddress(getHost(), getPort()));
103-
boolean connected = socketChannel.finishConnect();
104-
long timeLeft = getConnectTimeout().toMillis();
105-
while (!connected && timeLeft > 0) {
106-
Thread.sleep(50); // NOSONAR Magic #
107-
connected = socketChannel.finishConnect();
108-
timeLeft -= 50; // NOSONAR Magic #
109-
}
110-
if (!connected) {
111-
throw new IOException("Not connected after connectTimeout");
112-
}
113102
if (getSoTimeout() > 0) {
114103
connection.setLastRead(System.currentTimeMillis());
115104
}
@@ -127,6 +116,21 @@ protected TcpConnectionSupport buildNewConnection() {
127116
}
128117
}
129118

119+
private void connect(SocketChannel socketChannel) throws IOException, InterruptedException {
120+
socketChannel.configureBlocking(false);
121+
socketChannel.connect(new InetSocketAddress(getHost(), getPort()));
122+
boolean connected = socketChannel.finishConnect();
123+
long timeLeft = getConnectTimeout().toMillis();
124+
while (!connected && timeLeft > 0) {
125+
Thread.sleep(50); // NOSONAR Magic #
126+
connected = socketChannel.finishConnect();
127+
timeLeft -= 50; // NOSONAR Magic #
128+
}
129+
if (!connected) {
130+
throw new IOException("Not connected after connectTimeout");
131+
}
132+
}
133+
130134
/**
131135
* When set to true, connections created by this factory attempt
132136
* to use direct buffers where possible.

spring-integration-ip/src/test/java/org/springframework/integration/ip/tcp/connection/TcpNioConnectionTests.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,12 @@ public void testWriteTimeout() throws Exception {
139139
assertThat(latch.await(10000, TimeUnit.MILLISECONDS)).isTrue();
140140
TcpNioClientConnectionFactory factory = new TcpNioClientConnectionFactory("localhost",
141141
serverSocket.get().getLocalPort());
142-
factory.setApplicationEventPublisher(nullPublisher);
142+
AtomicReference<String> connectionId = new AtomicReference<>();
143+
factory.setApplicationEventPublisher(event -> {
144+
if (event instanceof TcpConnectionOpenEvent) {
145+
connectionId.set(((TcpConnectionOpenEvent) event).getConnectionId());
146+
}
147+
});
143148
factory.setSoTimeout(100);
144149
factory.start();
145150
try {
@@ -154,6 +159,7 @@ public void testWriteTimeout() throws Exception {
154159
done.countDown();
155160
factory.stop();
156161
serverSocket.get().close();
162+
assertThat(connectionId.get()).startsWith("localhost");
157163
}
158164

159165
@Test

0 commit comments

Comments
 (0)