diff --git a/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java index a96dae97..1abec747 100644 --- a/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java @@ -33,7 +33,6 @@ import org.apache.geode.cache.GemFireCache; import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.ClientCacheFactory; import org.apache.geode.cache.client.Pool; -import org.apache.geode.cache.client.PoolManager; import org.apache.geode.distributed.DistributedSystem; import org.springframework.context.ApplicationContext; @@ -44,13 +43,17 @@ import org.springframework.data.gemfire.CacheFactoryBean; import org.springframework.data.gemfire.GemfireUtils; import org.springframework.data.gemfire.client.support.DefaultableDelegatingPoolAdapter; import org.springframework.data.gemfire.client.support.DelegatingPoolAdapter; +import org.springframework.data.gemfire.client.support.PoolManagerPoolResolver; import org.springframework.data.gemfire.config.annotation.ClientCacheConfigurer; import org.springframework.data.gemfire.config.xml.GemfireConstants; import org.springframework.data.gemfire.support.ConnectionEndpoint; import org.springframework.data.gemfire.support.ConnectionEndpointList; import org.springframework.data.gemfire.util.SpringUtils; +import org.springframework.lang.Nullable; import org.springframework.util.StringUtils; +import lombok.NonNull; + /** * Spring {@link org.springframework.beans.factory.FactoryBean} used to create a Pivotal GemFire/Apache Geode * {@link ClientCache}. @@ -78,6 +81,8 @@ import org.springframework.util.StringUtils; @SuppressWarnings("unused") public class ClientCacheFactoryBean extends CacheFactoryBean implements ApplicationListener { + protected static final PoolResolver DEFAULT_POOL_RESOLVER = new PoolManagerPoolResolver(); + private Boolean keepAlive = false; private Boolean multiUserAuthentication; private Boolean prSingleHopEnabled; @@ -109,6 +114,8 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat private Pool pool; + private PoolResolver poolResolver; + private String durableClientId; private String poolName; private String serverGroup; @@ -327,11 +334,10 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat * Resolves the {@link Pool} used to configure the {@link ClientCache}, {@literal DEFAULT} {@link Pool}. * * @return the resolved {@link Pool} used to configure the {@link ClientCache}, {@literal DEFAULT} {@link Pool}. - * @see org.apache.geode.cache.client.PoolManager#find(String) * @see org.apache.geode.cache.client.Pool - * @see #getPoolName() * @see #getPool() * @see #findPool(String) + * @see #resolvePoolName() * @see #isPoolNameResolvable(String) */ Pool resolvePool() { @@ -366,7 +372,7 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat } Pool findPool(String name) { - return PoolManager.find(name); + return getPoolResolver().resolve(name); } private boolean isPoolNameResolvable(String poolName) { @@ -401,6 +407,7 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat * @see #fetchCache() */ @Override + @SuppressWarnings("all") public void onApplicationEvent(ContextRefreshedEvent event) { if (isReadyForEvents()) { @@ -433,7 +440,7 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat * @see org.springframework.beans.factory.FactoryBean#getObjectType() */ @Override - @SuppressWarnings("unchecked") + @SuppressWarnings({ "rawtypes", "unchecked" }) public Class getObjectType() { return Optional.ofNullable(getCache()).map(Object::getClass).orElse((Class) ClientCache.class); } @@ -633,45 +640,74 @@ public class ClientCacheFactoryBean extends CacheFactoryBean implements Applicat } /** - * Sets the {@link Pool} used by this cache client to obtain connections to the Pivotal GemFire cluster. + * Sets the {@link Pool} used by this {@link ClientCache} to obtain connections to the Apache Geode cluster. * - * @param pool the Pivotal GemFire {@link Pool} used by this {@link ClientCache} to obtain connections - * to the Pivotal GemFire cluster. - * @throws IllegalArgumentException if the {@link Pool} is null. + * @param pool {@link Pool} used by this {@link ClientCache} to obtain connections to the Apache Geode cluster. + * @see org.apache.geode.cache.client.Pool */ public void setPool(Pool pool) { this.pool = pool; } /** - * Gets the {@link Pool} used by this cache client to obtain connections to the Pivotal GemFire cluster. + * Gets the {@link Pool} used by this {@link ClientCache} to obtain connections to the Apache Geode cluster. * - * @return the Pivotal GemFire {@link Pool} used by this {@link ClientCache} to obtain connections - * to the Pivotal GemFire cluster. + * @return {@link Pool} used by this {@link ClientCache} to obtain connections to the Apache Geode cluster. + * @see org.apache.geode.cache.client.Pool */ public Pool getPool() { return this.pool; } /** - * Sets the name of the {@link Pool} used by this cache client to obtain connections to the Pivotal GemFire cluster. + * Sets the {@link String name} of the {@link Pool} used by this {@link ClientCache} to obtain connections to + * the Apache Geode cluster. * - * @param poolName set the name of the Pivotal GemFire {@link Pool} used by this Pivotal GemFire {@link ClientCache}. - * @throws IllegalArgumentException if the {@link Pool} name is unspecified. + * @param poolName {@link String name} of the {@link Pool} used by this {@link ClientCache} to obtain connections to + * the Apache Geode cluster. */ public void setPoolName(String poolName) { this.poolName = poolName; } /** - * Gets the name of the Pivotal GemFire {@link Pool} used by this Pivotal GemFire cache client. + * Gets the {@link String name} of the {@link Pool} used by this {@link ClientCache} to obtain connections to + * the Apache Geode cluster. * - * @return the name of the Pivotal GemFire {@link Pool} used by this Pivotal GemFire cache client. + * @return {@link String name} of the {@link Pool} used by this {@link ClientCache} to obtain connections to + * the Apache Geode cluster. */ public String getPoolName() { return this.poolName; } + /** + * Sets (configures) the {@link PoolResolver} used by this {@link ClientCache} to resolve {@link Pool} objects. + * + * The {@link Pool} objects may be managed or un-managed depending on the {@link PoolResolver} implementation. + * + * @param poolResolver {@link PoolResolver} used to resolve the configured {@link Pool}. + * @see org.springframework.data.gemfire.client.PoolResolver + */ + public void setPoolResolver(@Nullable PoolResolver poolResolver) { + this.poolResolver = poolResolver; + } + + /** + * Gets the configured {@link PoolResolver} used by this {@link ClientCache} to resolve {@link Pool} objects. + * + * @return the configured {@link PoolResolver}. If no {@link PoolResolver} was configured, then return the default, + * {@link PoolManagerPoolResolver}. + * @see org.springframework.data.gemfire.client.PoolResolver + * @see org.springframework.data.gemfire.client.support.PoolManagerPoolResolver + */ + public @NonNull PoolResolver getPoolResolver() { + + PoolResolver poolResolver = this.poolResolver; + + return poolResolver != null ? poolResolver : DEFAULT_POOL_RESOLVER; + } + public void setPingInterval(Long pingInterval) { this.pingInterval = pingInterval; } diff --git a/src/main/java/org/springframework/data/gemfire/client/ClientRegionFactoryBean.java b/src/main/java/org/springframework/data/gemfire/client/ClientRegionFactoryBean.java index 73bdb384..ddddb771 100644 --- a/src/main/java/org/springframework/data/gemfire/client/ClientRegionFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/client/ClientRegionFactoryBean.java @@ -36,22 +36,25 @@ import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.ClientRegionFactory; import org.apache.geode.cache.client.ClientRegionShortcut; import org.apache.geode.cache.client.Pool; -import org.apache.geode.cache.client.PoolManager; import org.apache.geode.compression.Compressor; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; import org.springframework.data.gemfire.ConfigurableRegionFactoryBean; import org.springframework.data.gemfire.GemfireUtils; +import org.springframework.data.gemfire.client.support.PoolManagerPoolResolver; import org.springframework.data.gemfire.config.xml.GemfireConstants; import org.springframework.data.gemfire.eviction.EvictingRegionFactoryBean; import org.springframework.data.gemfire.expiration.ExpiringRegionFactoryBean; import org.springframework.data.gemfire.support.SmartLifecycleSupport; import org.springframework.data.gemfire.util.RegionUtils; import org.springframework.data.gemfire.util.SpringUtils; +import org.springframework.lang.Nullable; import org.springframework.util.Assert; import org.springframework.util.StringUtils; +import lombok.NonNull; + /** * Spring {@link FactoryBean} used to construct, configure and initialize a client {@link Region}. * @@ -81,6 +84,8 @@ public class ClientRegionFactoryBean extends ConfigurableRegionFactoryBean public static final String DEFAULT_POOL_NAME = "DEFAULT"; public static final String GEMFIRE_POOL_NAME = GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME; + protected static final PoolResolver DEFAULT_POOL_RESOLVER = new PoolManagerPoolResolver(); + private boolean close = false; private boolean destroy = false; @@ -122,6 +127,8 @@ public class ClientRegionFactoryBean extends ConfigurableRegionFactoryBean private Float loadFactor; + private PoolResolver poolResolver = DEFAULT_POOL_RESOLVER; + private RegionAttributes attributes; private String diskStoreName; @@ -257,7 +264,7 @@ public class ClientRegionFactoryBean extends ConfigurableRegionFactoryBean @SuppressWarnings("all") private boolean eagerlyInitializePool(String poolName) { - return Optional.ofNullable(PoolManager.find(poolName)) + return Optional.ofNullable(getPoolResolver().resolve(poolName)) .map(it -> true) .orElseGet(() -> SpringUtils.safeGetValue(() -> @@ -678,6 +685,33 @@ public class ClientRegionFactoryBean extends ConfigurableRegionFactoryBean return Optional.ofNullable(this.poolName); } + /** + * Sets (configures) the {@link PoolResolver} used by this {@link ClientCache} to resolve {@link Pool} objects. + * + * The {@link Pool} objects may be managed or un-managed depending on the {@link PoolResolver} implementation. + * + * @param poolResolver {@link PoolResolver} used to resolve the configured {@link Pool}. + * @see org.springframework.data.gemfire.client.PoolResolver + */ + public void setPoolResolver(@Nullable PoolResolver poolResolver) { + this.poolResolver = poolResolver; + } + + /** + * Gets the configured {@link PoolResolver} used by this {@link ClientCache} to resolve {@link Pool} objects. + * + * @return the configured {@link PoolResolver}. If no {@link PoolResolver} was configured, then return the default, + * {@link PoolManagerPoolResolver}. + * @see org.springframework.data.gemfire.client.PoolResolver + * @see org.springframework.data.gemfire.client.support.PoolManagerPoolResolver + */ + public @NonNull PoolResolver getPoolResolver() { + + PoolResolver poolResolver = this.poolResolver; + + return poolResolver != null ? poolResolver : DEFAULT_POOL_RESOLVER; + } + public void setRegionIdleTimeout(ExpirationAttributes regionIdleTimeout) { this.regionIdleTimeout = regionIdleTimeout; } diff --git a/src/main/java/org/springframework/data/gemfire/function/execution/GemfireOnServerFunctionTemplate.java b/src/main/java/org/springframework/data/gemfire/function/execution/GemfireOnServerFunctionTemplate.java index bdf79ecd..cde7b0d2 100644 --- a/src/main/java/org/springframework/data/gemfire/function/execution/GemfireOnServerFunctionTemplate.java +++ b/src/main/java/org/springframework/data/gemfire/function/execution/GemfireOnServerFunctionTemplate.java @@ -19,11 +19,12 @@ import java.util.Optional; import org.apache.geode.cache.RegionService; import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.Pool; -import org.apache.geode.cache.client.PoolManager; import org.apache.geode.cache.execute.Execution; import org.apache.geode.cache.execute.Function; import org.springframework.data.gemfire.GemfireUtils; +import org.springframework.data.gemfire.client.PoolResolver; +import org.springframework.data.gemfire.client.support.PoolManagerPoolResolver; import org.springframework.data.gemfire.util.CacheUtils; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -44,8 +45,12 @@ import org.springframework.util.StringUtils; @SuppressWarnings("unused") public class GemfireOnServerFunctionTemplate extends AbstractFunctionTemplate { + protected static final PoolResolver DEFAULT_POOL_RESOLVER = new PoolManagerPoolResolver(); + private Pool pool; + private PoolResolver poolResolver = DEFAULT_POOL_RESOLVER; + private final RegionService cache; private String poolName; @@ -75,6 +80,17 @@ public class GemfireOnServerFunctionTemplate extends AbstractFunctionTemplate { this.poolName = poolName; } + public void setPoolResolver(PoolResolver poolResolver) { + this.poolResolver = poolResolver; + } + + protected PoolResolver getPoolResolver() { + + PoolResolver poolResolver = this.poolResolver; + + return poolResolver != null ? poolResolver : DEFAULT_POOL_RESOLVER; + } + @Override protected AbstractFunctionExecution getFunctionExecution() { @@ -97,14 +113,14 @@ public class GemfireOnServerFunctionTemplate extends AbstractFunctionTemplate { protected Pool resolveDefaultPool() { - return Optional.ofNullable(PoolManager.find(GemfireUtils.DEFAULT_POOL_NAME)) + return Optional.ofNullable(getPoolResolver().resolve(GemfireUtils.DEFAULT_POOL_NAME)) .orElseThrow(() -> newIllegalStateException("No Pool was configured")); } protected Pool resolveNamedPool() { if (StringUtils.hasText(this.poolName)) { - this.pool = Optional.ofNullable(PoolManager.find(this.poolName)) + this.pool = Optional.ofNullable(getPoolResolver().resolve(this.poolName)) .orElseThrow(() -> newIllegalStateException("No Pool with name [%s] exists", this.poolName)); } diff --git a/src/main/java/org/springframework/data/gemfire/function/execution/GemfireOnServersFunctionTemplate.java b/src/main/java/org/springframework/data/gemfire/function/execution/GemfireOnServersFunctionTemplate.java index ad12962b..e93281cc 100644 --- a/src/main/java/org/springframework/data/gemfire/function/execution/GemfireOnServersFunctionTemplate.java +++ b/src/main/java/org/springframework/data/gemfire/function/execution/GemfireOnServersFunctionTemplate.java @@ -12,7 +12,6 @@ */ package org.springframework.data.gemfire.function.execution; - import static org.springframework.data.gemfire.util.RuntimeExceptionFactory.newIllegalStateException; import java.util.Optional; @@ -20,11 +19,12 @@ import java.util.Optional; import org.apache.geode.cache.RegionService; import org.apache.geode.cache.client.ClientCache; import org.apache.geode.cache.client.Pool; -import org.apache.geode.cache.client.PoolManager; import org.apache.geode.cache.execute.Execution; import org.apache.geode.cache.execute.Function; import org.springframework.data.gemfire.GemfireUtils; +import org.springframework.data.gemfire.client.PoolResolver; +import org.springframework.data.gemfire.client.support.PoolManagerPoolResolver; import org.springframework.data.gemfire.util.CacheUtils; import org.springframework.util.Assert; import org.springframework.util.StringUtils; @@ -45,8 +45,12 @@ import org.springframework.util.StringUtils; @SuppressWarnings("unused") public class GemfireOnServersFunctionTemplate extends AbstractFunctionTemplate { + protected static final PoolResolver DEFAULT_POOL_RESOLVER = new PoolManagerPoolResolver(); + private Pool pool; + private PoolResolver poolResolver = DEFAULT_POOL_RESOLVER; + private final RegionService cache; private String poolName; @@ -76,6 +80,17 @@ public class GemfireOnServersFunctionTemplate extends AbstractFunctionTemplate { this.poolName = poolName; } + public void setPoolResolver(PoolResolver poolResolver) { + this.poolResolver = poolResolver; + } + + protected PoolResolver getPoolResolver() { + + PoolResolver poolResolver = this.poolResolver; + + return poolResolver != null ? poolResolver : DEFAULT_POOL_RESOLVER; + } + @Override protected AbstractFunctionExecution getFunctionExecution() { @@ -98,14 +113,14 @@ public class GemfireOnServersFunctionTemplate extends AbstractFunctionTemplate { protected Pool resolveDefaultPool() { - return Optional.ofNullable(PoolManager.find(GemfireUtils.DEFAULT_POOL_NAME)) + return Optional.ofNullable(getPoolResolver().resolve(GemfireUtils.DEFAULT_POOL_NAME)) .orElseThrow(() -> newIllegalStateException("No Pool was configured")); } protected Pool resolveNamedPool() { if (StringUtils.hasText(this.poolName)) { - this.pool = Optional.ofNullable(PoolManager.find(this.poolName)) + this.pool = Optional.ofNullable(getPoolResolver().resolve(this.poolName)) .orElseThrow(() -> newIllegalStateException("No Pool with name [%s] exists", this.poolName)); } diff --git a/src/test/java/org/springframework/data/gemfire/function/execution/FunctionExecutionIntegrationTests.java b/src/test/java/org/springframework/data/gemfire/function/execution/FunctionExecutionIntegrationTests.java index a335e38f..2e82e592 100644 --- a/src/test/java/org/springframework/data/gemfire/function/execution/FunctionExecutionIntegrationTests.java +++ b/src/test/java/org/springframework/data/gemfire/function/execution/FunctionExecutionIntegrationTests.java @@ -10,25 +10,25 @@ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ - package org.springframework.data.gemfire.function.execution; import static org.assertj.core.api.Assertions.assertThat; -import org.apache.geode.cache.CacheClosedException; -import org.apache.geode.cache.Region; -import org.apache.geode.cache.client.ClientCache; -import org.apache.geode.cache.client.ClientCacheFactory; -import org.apache.geode.cache.client.ClientRegionShortcut; -import org.apache.geode.cache.client.Pool; -import org.apache.geode.cache.client.PoolManager; - import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.apache.geode.cache.CacheClosedException; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.cache.client.ClientCacheFactory; +import org.apache.geode.cache.client.ClientRegionShortcut; +import org.apache.geode.cache.client.Pool; + +import org.springframework.data.gemfire.client.PoolResolver; +import org.springframework.data.gemfire.client.support.PoolManagerPoolResolver; import org.springframework.data.gemfire.fork.FunctionCacheServerProcess; import org.springframework.data.gemfire.process.ProcessWrapper; import org.springframework.data.gemfire.test.support.ClientServerIntegrationTestsSupport; @@ -47,6 +47,8 @@ public class FunctionExecutionIntegrationTests extends ClientServerIntegrationTe private Pool gemfirePool = null; + private PoolResolver poolResolver = new PoolManagerPoolResolver(); + private Region gemfireRegion = null; @BeforeClass @@ -87,7 +89,7 @@ public class FunctionExecutionIntegrationTests extends ClientServerIntegrationTe assertThat(this.gemfireRegion).isNotNull(); assertThat(this.gemfireRegion.getName()).isEqualTo("test-function"); - this.gemfirePool = PoolManager.find("DEFAULT"); + this.gemfirePool = this.poolResolver.resolve("DEFAULT"); assertThat(this.gemfirePool).isNotNull(); assertThat(this.gemfirePool.getName()).isEqualTo("DEFAULT"); diff --git a/src/test/java/org/springframework/data/gemfire/function/execution/GemfireFunctionTemplateIntegrationTests.java b/src/test/java/org/springframework/data/gemfire/function/execution/GemfireFunctionTemplateIntegrationTests.java index 534c4f76..80d08e6e 100644 --- a/src/test/java/org/springframework/data/gemfire/function/execution/GemfireFunctionTemplateIntegrationTests.java +++ b/src/test/java/org/springframework/data/gemfire/function/execution/GemfireFunctionTemplateIntegrationTests.java @@ -10,17 +10,9 @@ * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the * specific language governing permissions and limitations under the License. */ - package org.springframework.data.gemfire.function.execution; -import static org.assertj.core.api.Java6Assertions.assertThat; - -import org.apache.geode.cache.Region; -import org.apache.geode.cache.client.ClientCache; -import org.apache.geode.cache.client.ClientCacheFactory; -import org.apache.geode.cache.client.ClientRegionShortcut; -import org.apache.geode.cache.client.Pool; -import org.apache.geode.cache.client.PoolManager; +import static org.assertj.core.api.Assertions.assertThat; import org.junit.After; import org.junit.AfterClass; @@ -28,7 +20,15 @@ import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; +import org.apache.geode.cache.Region; +import org.apache.geode.cache.client.ClientCache; +import org.apache.geode.cache.client.ClientCacheFactory; +import org.apache.geode.cache.client.ClientRegionShortcut; +import org.apache.geode.cache.client.Pool; + import org.springframework.data.gemfire.GemfireUtils; +import org.springframework.data.gemfire.client.PoolResolver; +import org.springframework.data.gemfire.client.support.PoolManagerPoolResolver; import org.springframework.data.gemfire.fork.FunctionCacheServerProcess; import org.springframework.data.gemfire.process.ProcessWrapper; import org.springframework.data.gemfire.test.support.ClientServerIntegrationTestsSupport; @@ -46,6 +46,8 @@ public class GemfireFunctionTemplateIntegrationTests extends ClientServerIntegra private Pool gemfirePool = null; + private PoolResolver poolResolver = new PoolManagerPoolResolver(); + private Region gemfireRegion = null; @BeforeClass @@ -86,7 +88,7 @@ public class GemfireFunctionTemplateIntegrationTests extends ClientServerIntegra assertThat(this.gemfireRegion).isNotNull(); assertThat(this.gemfireRegion.getName()).isEqualTo("test-function"); - this.gemfirePool = PoolManager.find("DEFAULT"); + this.gemfirePool = this.poolResolver.resolve("DEFAULT"); assertThat(this.gemfirePool).isNotNull(); assertThat(this.gemfirePool.getName()).isEqualTo("DEFAULT");