Skip to content

Commit

Permalink
GH-3509 Register TcpSenders on wrapped connection
Browse files Browse the repository at this point in the history
Fixes #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`**
  • Loading branch information
gigermocas authored and artembilan committed Mar 12, 2021
1 parent 51f2188 commit 1e67a94
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 61 deletions.
@@ -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.
Expand All @@ -22,6 +22,8 @@

/**
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*
*/
Expand All @@ -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);
}

}
@@ -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.
Expand All @@ -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;
Expand All @@ -29,6 +31,7 @@
* to the underlying {@link TcpConnection}.
*
* @author Gary Russell
* @author Mário Dias
*
* @since 2.0
*/
Expand Down Expand Up @@ -96,6 +99,11 @@ public void registerSender(TcpSender sender) {
this.theConnection.registerSender(this);
}

@Override
public void registerSenders(List<TcpSender> sendersToRegister) {
this.theConnection.registerSenders(sendersToRegister);
}

@Override
public String getConnectionId() {
return this.theConnection.getConnectionId();
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-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.
Expand All @@ -16,40 +16,34 @@

package org.springframework.integration.ip.tcp;

import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.integration.ip.tcp.connection.AbstractConnectionFactory;
import org.springframework.integration.ip.tcp.connection.HelloWorldInterceptorFactory;

/**
* @author Gary Russell
* @author Mário Dias
* @author Artem Bilan
*
* @since 3.0
*
*/
public class AbstractTcpChannelAdapterTests {

private static final ApplicationEventPublisher NOOP_PUBLISHER = new ApplicationEventPublisher() {

@Override
public void publishEvent(ApplicationEvent event) {
}

@Override
public void publishEvent(Object event) {

}

};
private static final ApplicationEventPublisher NOOP_PUBLISHER = event -> { };

protected HelloWorldInterceptorFactory newInterceptorFactory() {
return newInterceptorFactory(NOOP_PUBLISHER);
}

protected HelloWorldInterceptorFactory newInterceptorFactory(ApplicationEventPublisher applicationEventPublisher) {
HelloWorldInterceptorFactory factory = new HelloWorldInterceptorFactory();
factory.setApplicationEventPublisher(NOOP_PUBLISHER);
factory.setApplicationEventPublisher(applicationEventPublisher);
return factory;
}

protected void noopPublisher(AbstractConnectionFactory connectionFactory) {
connectionFactory.setApplicationEventPublisher(NOOP_PUBLISHER);
}


}

0 comments on commit 1e67a94

Please sign in to comment.