@@ -163,7 +163,8 @@ private static NioEventLoopGroup initEventLoopGroup() {
163
163
public ListenableFuture <Void > connect (final TcpConnectionHandler <P > connectionHandler ) {
164
164
Assert .notNull (connectionHandler , "TcpConnectionHandler must not be null" );
165
165
166
- TcpClient <Message <P >, Message <P >> tcpClient ;
166
+ final TcpClient <Message <P >, Message <P >> tcpClient ;
167
+ Runnable cleanupTask ;
167
168
synchronized (this .tcpClients ) {
168
169
if (this .stopping ) {
169
170
IllegalStateException ex = new IllegalStateException ("Shutting down." );
@@ -172,9 +173,18 @@ public ListenableFuture<Void> connect(final TcpConnectionHandler<P> connectionHa
172
173
}
173
174
tcpClient = NetStreams .tcpClient (REACTOR_TCP_CLIENT_TYPE , this .tcpClientSpecFactory );
174
175
this .tcpClients .add (tcpClient );
176
+ cleanupTask = new Runnable () {
177
+ @ Override
178
+ public void run () {
179
+ synchronized (tcpClients ) {
180
+ tcpClients .remove (tcpClient );
181
+ }
182
+ }
183
+ };
175
184
}
176
185
177
- Promise <Void > promise = tcpClient .start (new MessageChannelStreamHandler <P >(connectionHandler ));
186
+ Promise <Void > promise = tcpClient .start (
187
+ new MessageChannelStreamHandler <P >(connectionHandler , cleanupTask ));
178
188
179
189
return new PassThroughPromiseToListenableFutureAdapter <Void >(
180
190
promise .onError (new Consumer <Throwable >() {
@@ -191,7 +201,8 @@ public ListenableFuture<Void> connect(TcpConnectionHandler<P> connectionHandler,
191
201
Assert .notNull (connectionHandler , "TcpConnectionHandler must not be null" );
192
202
Assert .notNull (strategy , "ReconnectStrategy must not be null" );
193
203
194
- TcpClient <Message <P >, Message <P >> tcpClient ;
204
+ final TcpClient <Message <P >, Message <P >> tcpClient ;
205
+ Runnable cleanupTask ;
195
206
synchronized (this .tcpClients ) {
196
207
if (this .stopping ) {
197
208
IllegalStateException ex = new IllegalStateException ("Shutting down." );
@@ -200,10 +211,18 @@ public ListenableFuture<Void> connect(TcpConnectionHandler<P> connectionHandler,
200
211
}
201
212
tcpClient = NetStreams .tcpClient (REACTOR_TCP_CLIENT_TYPE , this .tcpClientSpecFactory );
202
213
this .tcpClients .add (tcpClient );
214
+ cleanupTask = new Runnable () {
215
+ @ Override
216
+ public void run () {
217
+ synchronized (tcpClients ) {
218
+ tcpClients .remove (tcpClient );
219
+ }
220
+ }
221
+ };
203
222
}
204
223
205
224
Stream <Tuple2 <InetSocketAddress , Integer >> stream = tcpClient .start (
206
- new MessageChannelStreamHandler <P >(connectionHandler ),
225
+ new MessageChannelStreamHandler <P >(connectionHandler , cleanupTask ),
207
226
new ReactorReconnectAdapter (strategy ));
208
227
209
228
return new PassThroughPromiseToListenableFutureAdapter <Void >(stream .next ().after ());
@@ -249,6 +268,7 @@ public void operationComplete(Future<Object> future) throws Exception {
249
268
});
250
269
promise = eventLoopPromise ;
251
270
}
271
+
252
272
return new PassThroughPromiseToListenableFutureAdapter <Void >(promise );
253
273
}
254
274
@@ -278,8 +298,11 @@ private static class MessageChannelStreamHandler<P>
278
298
279
299
private final TcpConnectionHandler <P > connectionHandler ;
280
300
281
- public MessageChannelStreamHandler (TcpConnectionHandler <P > connectionHandler ) {
301
+ private final Runnable cleanupTask ;
302
+
303
+ public MessageChannelStreamHandler (TcpConnectionHandler <P > connectionHandler , Runnable cleanupTask ) {
282
304
this .connectionHandler = connectionHandler ;
305
+ this .cleanupTask = cleanupTask ;
283
306
}
284
307
285
308
@ Override
@@ -290,6 +313,7 @@ public Publisher<Void> apply(ChannelStream<Message<P>, Message<P>> channelStream
290
313
.finallyDo (new Consumer <Signal <Message <P >>>() {
291
314
@ Override
292
315
public void accept (Signal <Message <P >> signal ) {
316
+ cleanupTask .run ();
293
317
if (signal .isOnError ()) {
294
318
connectionHandler .handleFailure (signal .getThrowable ());
295
319
}
0 commit comments