GH-3509 Register TcpSenders on wrapped connection

Fixes https://github.com/spring-projects/spring-integration/issues/3509

Dead connections are not being removed on `TcpSender` when using a `TcpConnectionInterceptor`.
May cause a memory leak

* Fix `TcpConnectionInterceptorSupport` to override `registerSenders()`
 and delegate to the `this.theConnection`
* Introduce vararg-based `setInterceptor()` into `TcpConnectionInterceptorFactoryChain`
* Remove redundant `//NOSONAR` and properly return an `Arrays.copyOf()` in the `getInterceptorFactories()`

**Cherry-pick to `5.4.x`**
This commit is contained in:
Mário Dias
2021-03-12 15:33:11 +00:00
committed by Artem Bilan
parent 42fa248753
commit 78a0ae8a04
4 changed files with 98 additions and 61 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@ import org.springframework.lang.Nullable;
/**
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*
*/
@@ -31,11 +33,17 @@ public class TcpConnectionInterceptorFactoryChain {
@Nullable
public TcpConnectionInterceptorFactory[] getInterceptorFactories() {
return this.interceptorFactories; //NOSONAR
return this.interceptorFactories != null
? Arrays.copyOf(this.interceptorFactories, this.interceptorFactories.length)
: null;
}
public void setInterceptors(TcpConnectionInterceptorFactory[] interceptorFactories) {
this.interceptorFactories = Arrays.copyOf(interceptorFactories, interceptorFactories.length);
}
public void setInterceptor(TcpConnectionInterceptorFactory... interceptorFactories) {
setInterceptors(interceptorFactories);
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,6 +16,8 @@
package org.springframework.integration.ip.tcp.connection;
import java.util.List;
import javax.net.ssl.SSLSession;
import org.springframework.context.ApplicationEventPublisher;
@@ -29,6 +31,7 @@ import org.springframework.messaging.support.ErrorMessage;
* to the underlying {@link TcpConnection}.
*
* @author Gary Russell
* @author Mário Dias
*
* @since 2.0
*/
@@ -96,6 +99,11 @@ public abstract class TcpConnectionInterceptorSupport extends TcpConnectionSuppo
this.theConnection.registerSender(this);
}
@Override
public void registerSenders(List<TcpSender> sendersToRegister) {
this.theConnection.registerSenders(sendersToRegister);
}
@Override
public String getConnectionId() {
return this.theConnection.getConnectionId();