Skip to content

Commit 4ddc525

Browse files
kazuki43zooartembilan
authored andcommitted
GH-8609: Fix TcpConnectorInterceptor chain propagation
Fixes #8609 * Changed to passed the self instance to `addNewConnection()` instead of argument's connection in a `TcpConnectionInterceptorSupport` to compose a chain of interceptors from top to down. This way the target `Sender` get the last interceptor in a chain as its connection. **Cherry-pick to `6.0.x` & `5.5.x`**
1 parent d89badf commit 4ddc525

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
* to the underlying {@link TcpConnection}.
3434
*
3535
* @author Gary Russell
36+
* @author Kazuki Shimizu
3637
*
3738
* @since 2.0
3839
*/
@@ -232,7 +233,7 @@ public TcpListener getListener() {
232233
@Override
233234
public void addNewConnection(TcpConnection connection) {
234235
if (this.interceptedSenders != null) {
235-
this.interceptedSenders.forEach(sender -> sender.addNewConnection(connection));
236+
this.interceptedSenders.forEach(sender -> sender.addNewConnection(this));
236237
}
237238
}
238239

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import java.util.ArrayList;
2020
import java.util.Collections;
21+
import java.util.HashMap;
2122
import java.util.List;
23+
import java.util.Map;
2224
import java.util.concurrent.CountDownLatch;
2325
import java.util.concurrent.TimeUnit;
2426
import java.util.concurrent.atomic.AtomicInteger;
@@ -30,6 +32,7 @@
3032

3133
/**
3234
* @author Gary Russell
35+
* @author Kazuki Shimizu
3336
* @since 5.3.10
3437
*
3538
*/
@@ -81,11 +84,14 @@ private void senderCalledForDeadConnectionClient(AbstractClientConnectionFactory
8184
List<Integer> addOrder = Collections.synchronizedList(new ArrayList<>());
8285
List<Integer> remOrder = Collections.synchronizedList(new ArrayList<>());
8386
AtomicReference<Thread> thread = new AtomicReference<>();
87+
Map<Integer, TcpConnection> interceptorsPerInstance = new HashMap<>();
88+
List<TcpConnection> passedConnectionsToSenderViaAddNewConnection = new ArrayList<>();
8489
class InterceptorFactory extends HelloWorldInterceptorFactory {
8590

8691
@Override
8792
public TcpConnectionInterceptorSupport getInterceptor() {
88-
return new TcpConnectionInterceptorSupport() {
93+
94+
TcpConnectionInterceptorSupport interceptor = new TcpConnectionInterceptorSupport() {
8995

9096
private final int instance = instances.incrementAndGet();
9197

@@ -107,6 +113,8 @@ public synchronized void removeDeadConnection(TcpConnection connection) {
107113
}
108114

109115
};
116+
interceptorsPerInstance.put(instances.get(), interceptor);
117+
return interceptor;
110118
}
111119

112120
}
@@ -118,6 +126,7 @@ public synchronized void removeDeadConnection(TcpConnection connection) {
118126
@Override
119127
public void addNewConnection(TcpConnection connection) {
120128
addOrder.add(99);
129+
passedConnectionsToSenderViaAddNewConnection.add(connection);
121130
adds.countDown();
122131
}
123132

@@ -146,6 +155,9 @@ public synchronized void removeDeadConnection(TcpConnection connection) {
146155
assertThat(remOrder).containsExactly(1, 2, 99, 3, 4, 5, 99, 6);
147156
assertThat(interceptorAddCalled.await(10, TimeUnit.SECONDS)).isTrue();
148157
assertThat(interceptorRemCalled.await(10, TimeUnit.SECONDS)).isTrue();
158+
// should be passed the last interceptor to the real sender via addNewConnection method
159+
assertThat(passedConnectionsToSenderViaAddNewConnection.get(0)).isSameAs(interceptorsPerInstance.get(3));
160+
assertThat(passedConnectionsToSenderViaAddNewConnection.get(1)).isSameAs(interceptorsPerInstance.get(6));
149161
}
150162

151163
}

0 commit comments

Comments
 (0)