Fix CachedSessionFactory Race
Close the pool so that any sessions returned after the factory is `destroy()`ed are closed. * Call `removeAllIdleItems()` in `close()`. * Close sessions in `SftpStreamingMessageSourceTests`. **cherry-pick to all supported branches**
This commit is contained in:
@@ -72,4 +72,11 @@ public interface Pool<T> {
|
||||
*/
|
||||
int getAllocatedCount();
|
||||
|
||||
/**
|
||||
* Close the pool; returned items will be destroyed.
|
||||
* @since 4.3.23
|
||||
*/
|
||||
default void close() {
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -62,6 +62,8 @@ public class SimplePool<T> implements Pool<T> {
|
||||
|
||||
private final PoolItemCallback<T> callback;
|
||||
|
||||
private volatile boolean closed;
|
||||
|
||||
/**
|
||||
* Creates a SimplePool with a specific limit.
|
||||
* @param poolSize The maximum number of items the pool supports.
|
||||
@@ -161,6 +163,7 @@ public class SimplePool<T> implements Pool<T> {
|
||||
*/
|
||||
@Override
|
||||
public T getItem() {
|
||||
Assert.state(!this.closed, "Pool has been closed");
|
||||
boolean permitted = false;
|
||||
try {
|
||||
try {
|
||||
@@ -218,7 +221,7 @@ public class SimplePool<T> implements Pool<T> {
|
||||
Assert.isTrue(this.allocated.contains(item),
|
||||
"You can only release items that were obtained from the pool");
|
||||
if (this.inUse.contains(item)) {
|
||||
if (this.poolSize.get() > this.targetPoolSize.get()) {
|
||||
if (this.poolSize.get() > this.targetPoolSize.get() || this.closed) {
|
||||
this.poolSize.decrementAndGet();
|
||||
doRemoveItem(item);
|
||||
}
|
||||
@@ -255,6 +258,12 @@ public class SimplePool<T> implements Pool<T> {
|
||||
this.callback.removedFromPool(item);
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void close() {
|
||||
this.closed = true;
|
||||
removeAllIdleItems();
|
||||
}
|
||||
|
||||
/**
|
||||
* User of the pool provide an implementation of this interface; called during
|
||||
* various pool operations.
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2019 the original author or authors.
|
||||
* Copyright 2002-2020 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.
|
||||
@@ -18,6 +18,8 @@ package org.springframework.integration.util;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
|
||||
import static org.assertj.core.api.Assertions.fail;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@@ -27,7 +29,7 @@ import java.util.Set;
|
||||
import java.util.concurrent.Semaphore;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.integration.test.util.TestUtils;
|
||||
|
||||
@@ -127,13 +129,13 @@ public class SimplePoolTests {
|
||||
assertThat(pool.getAllocatedCount()).isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testForeignObject() {
|
||||
final Set<String> strings = new HashSet<String>();
|
||||
final AtomicBoolean stale = new AtomicBoolean();
|
||||
SimplePool<String> pool = stringPool(2, strings, stale);
|
||||
pool.getItem();
|
||||
pool.releaseItem("Hello, world!");
|
||||
assertThatIllegalArgumentException().isThrownBy(() -> pool.releaseItem("Hello, world!"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -251,16 +253,32 @@ public class SimplePoolTests {
|
||||
assertThat(pool.getActiveCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testClose() {
|
||||
SimplePool<String> pool = stringPool(10, new HashSet<>(), new AtomicBoolean());
|
||||
String item1 = pool.getItem();
|
||||
String item2 = pool.getItem();
|
||||
pool.releaseItem(item2);
|
||||
assertThat(pool.getAllocatedCount()).isEqualTo(2);
|
||||
pool.close();
|
||||
pool.releaseItem(item1);
|
||||
assertThat(pool.getAllocatedCount()).isEqualTo(0);
|
||||
assertThatIllegalStateException().isThrownBy(pool::getItem);
|
||||
}
|
||||
|
||||
private SimplePool<String> stringPool(int size, final Set<String> strings,
|
||||
final AtomicBoolean stale) {
|
||||
|
||||
SimplePool<String> pool = new SimplePool<String>(size, new SimplePool.PoolItemCallback<String>() {
|
||||
private int i;
|
||||
|
||||
@Override
|
||||
public String createForPool() {
|
||||
String string = "String" + i++;
|
||||
strings.add(string);
|
||||
return string;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isStale(String item) {
|
||||
if (stale.get()) {
|
||||
@@ -268,10 +286,12 @@ public class SimplePoolTests {
|
||||
}
|
||||
return stale.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removedFromPool(String item) {
|
||||
strings.remove(item);
|
||||
}
|
||||
|
||||
});
|
||||
return pool;
|
||||
}
|
||||
|
||||
@@ -137,7 +137,7 @@ public class CachingSessionFactory<F> implements SessionFactory<F>, DisposableBe
|
||||
*/
|
||||
@Override
|
||||
public void destroy() {
|
||||
this.pool.removeAllIdleItems();
|
||||
this.pool.close();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,6 +21,7 @@ import java.util.Map;
|
||||
import java.util.concurrent.Executor;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
import org.springframework.beans.factory.DisposableBean;
|
||||
import org.springframework.core.serializer.Deserializer;
|
||||
import org.springframework.core.serializer.Serializer;
|
||||
import org.springframework.integration.ip.IpHeaders;
|
||||
@@ -41,7 +42,7 @@ import org.springframework.messaging.support.ErrorMessage;
|
||||
* @since 2.2
|
||||
*
|
||||
*/
|
||||
public class CachingClientConnectionFactory extends AbstractClientConnectionFactory {
|
||||
public class CachingClientConnectionFactory extends AbstractClientConnectionFactory implements DisposableBean {
|
||||
|
||||
private final AbstractClientConnectionFactory targetConnectionFactory;
|
||||
|
||||
@@ -389,6 +390,11 @@ public class CachingClientConnectionFactory extends AbstractClientConnectionFact
|
||||
this.pool.removeAllIdleItems();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destroy() throws Exception {
|
||||
this.pool.close();
|
||||
}
|
||||
|
||||
private final class CachedConnection extends TcpConnectionInterceptorSupport {
|
||||
|
||||
private final AtomicBoolean released = new AtomicBoolean();
|
||||
|
||||
@@ -30,6 +30,7 @@ import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.integration.StaticMessageHeaderAccessor;
|
||||
import org.springframework.integration.annotation.InboundChannelAdapter;
|
||||
import org.springframework.integration.annotation.Transformer;
|
||||
import org.springframework.integration.channel.QueueChannel;
|
||||
@@ -134,6 +135,7 @@ public class SftpStreamingMessageSourceTests extends SftpTestSupport {
|
||||
.isIn(" sftpSource1.txt", "sftpSource2.txt");
|
||||
|
||||
received.getPayload().close();
|
||||
StaticMessageHeaderAccessor.getCloseableResource(received).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -148,6 +150,7 @@ public class SftpStreamingMessageSourceTests extends SftpTestSupport {
|
||||
.isIn(" sftpSource1.txt", "sftpSource2.txt");
|
||||
|
||||
received.getPayload().close();
|
||||
StaticMessageHeaderAccessor.getCloseableResource(received).close();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -162,6 +165,7 @@ public class SftpStreamingMessageSourceTests extends SftpTestSupport {
|
||||
.isIn(" sftpSource1.txt", "sftpSource2.txt");
|
||||
|
||||
received.getPayload().close();
|
||||
StaticMessageHeaderAccessor.getCloseableResource(received).close();
|
||||
}
|
||||
|
||||
private SftpStreamingMessageSource buildSource() {
|
||||
|
||||
Reference in New Issue
Block a user