From 53c93b536753cd912ca1ef8bb666365237268709 Mon Sep 17 00:00:00 2001 From: Costin Leau Date: Thu, 25 Aug 2011 17:49:26 +0300 Subject: [PATCH] SGF-35 + possible fix breaking Pool/ClientCacheFactory cycle + initialization --- .../data/gemfire/CacheFactoryBean.java | 10 +++++++-- .../client/ClientCacheFactoryBean.java | 22 +++++++++++++++++-- .../data/gemfire/client/PoolFactoryBean.java | 5 ----- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java index 81bd13e0..86d971c3 100644 --- a/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/CacheFactoryBean.java @@ -28,6 +28,7 @@ import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.FactoryBean; import org.springframework.beans.factory.InitializingBean; +import org.springframework.core.Ordered; import org.springframework.core.io.Resource; import org.springframework.dao.DataAccessException; import org.springframework.dao.support.PersistenceExceptionTranslator; @@ -58,7 +59,7 @@ import com.gemstone.gemfire.pdx.PdxSerializer; * @author Costin Leau */ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanClassLoaderAware, DisposableBean, - InitializingBean, FactoryBean, PersistenceExceptionTranslator { + InitializingBean, FactoryBean, PersistenceExceptionTranslator, Ordered { /** * Inner class to avoid a hard dependency on the GemFire 6.6 API. @@ -158,6 +159,7 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl th.setContextClassLoader(oldTCCL); } } + /** * Sets the PDX properties for the given object. Note this is implementation specific as it depends on the type * of the factory passed in. @@ -182,7 +184,7 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl return ((CacheFactory) factory).create(); } - private Properties mergeProperties() { + protected Properties mergeProperties() { Properties cfgProps = (properties != null ? (Properties) properties.clone() : new Properties()); return cfgProps; } @@ -320,4 +322,8 @@ public class CacheFactoryBean implements BeanNameAware, BeanFactoryAware, BeanCl protected BeanFactory getBeanFactory() { return beanFactory; } + + public int getOrder() { + return 0; + } } \ No newline at end of file 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 fc664c50..7c5d8a14 100644 --- a/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/client/ClientCacheFactoryBean.java @@ -22,11 +22,13 @@ import java.util.Properties; import org.springframework.data.gemfire.CacheFactoryBean; import org.springframework.util.Assert; +import org.springframework.util.StringUtils; import com.gemstone.gemfire.cache.GemFireCache; import com.gemstone.gemfire.cache.client.ClientCacheFactory; import com.gemstone.gemfire.cache.client.Pool; import com.gemstone.gemfire.cache.client.PoolManager; +import com.gemstone.gemfire.distributed.DistributedSystem; import com.gemstone.gemfire.pdx.PdxSerializer; /** @@ -84,9 +86,25 @@ public class ClientCacheFactoryBean extends CacheFactoryBean { private void initializePool(ClientCacheFactory ccf) { Pool p = pool; - p = PoolManager.find(poolName); + if (p == null && StringUtils.hasText(poolName)) { + p = PoolManager.find(poolName); + + // initialize a client-like Distributed System before initializing the pool + if (p == null) { + Properties prop = mergeProperties(); + prop.setProperty("mcast-port", "0"); + prop.setProperty("locators", ""); + + DistributedSystem system = DistributedSystem.connect(prop); + } + + // trigger pool initialization + Assert.isTrue(getBeanFactory().isTypeMatch(poolName, Pool.class), "No bean named " + poolName + " of type " + + Pool.class.getName() + " found"); + + p = getBeanFactory().getBean(poolName, Pool.class); + } - System.out.println("*** Pool found " + p); if (p != null) { // copy the pool settings - this way if the pool is not found, at least the cache will have a similar config ccf.setPoolFreeConnectionTimeout(p.getFreeConnectionTimeout()); diff --git a/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java b/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java index 65bfa6ea..1f68a380 100644 --- a/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java +++ b/src/main/java/org/springframework/data/gemfire/client/PoolFactoryBean.java @@ -31,7 +31,6 @@ import org.springframework.util.Assert; import org.springframework.util.CollectionUtils; import org.springframework.util.StringUtils; -import com.gemstone.gemfire.cache.GemFireCache; import com.gemstone.gemfire.cache.client.Pool; import com.gemstone.gemfire.cache.client.PoolFactory; import com.gemstone.gemfire.cache.client.PoolManager; @@ -105,10 +104,6 @@ public class PoolFactoryBean implements FactoryBean, InitializingBean, Dis name = beanName; } - if (beanFactory != null) { - beanFactory.getBean(GemFireCache.class); - } - // first check the configured pools Pool existingPool = PoolManager.find(name); if (existingPool != null) {