SGF-133: Enabled pool autowiring

This commit is contained in:
David Turanski
2012-10-09 12:05:40 -04:00
parent 208c57f5c8
commit bff9f3ce7c
21 changed files with 203 additions and 86 deletions

View File

@@ -20,7 +20,9 @@ import java.net.InetSocketAddress;
import java.util.List;
import java.util.Properties;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.data.gemfire.CacheFactoryBean;
import org.springframework.data.gemfire.config.GemfireConstants;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@@ -33,7 +35,8 @@ import com.gemstone.gemfire.pdx.PdxSerializer;
/**
* FactoryBean dedicated to creating client caches (caches for client JVMs).
* Acts an utility class (as client caches are a subset with a particular configuration of the generic cache).
* Acts an utility class (as client caches are a subset with a particular
* configuration of the generic cache).
*
* @author Costin Leau
*/
@@ -54,7 +57,8 @@ public class ClientCacheFactoryBean extends CacheFactoryBean {
public void run() {
if (pdxSerializer != null) {
Assert.isAssignable(PdxSerializer.class, pdxSerializer.getClass(), "Invalid pdx serializer used");
Assert.isAssignable(PdxSerializer.class,
pdxSerializer.getClass(), "Invalid pdx serializer used");
factory.setPdxSerializer((PdxSerializer) pdxSerializer);
}
if (pdxDiskStoreName != null) {
@@ -95,10 +99,12 @@ public class ClientCacheFactoryBean extends CacheFactoryBean {
private void initializePool(ClientCacheFactory ccf) {
Pool p = pool;
if (p == null && StringUtils.hasText(poolName)) {
p = PoolManager.find(poolName);
// initialize a client-like Distributed System before initializing the pool
if (p == null) {
if (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");
@@ -107,16 +113,32 @@ public class ClientCacheFactoryBean extends CacheFactoryBean {
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");
if (StringUtils.hasText(poolName)) {
try {
p = getBeanFactory().getBean(poolName, Pool.class);
Assert.notNull(p, "No pool named [" + poolName + "] found");
getBeanFactory().isTypeMatch(poolName, Pool.class);
} catch (Exception e) {
String msg = "No bean found with name " + poolName
+ " of type " + Pool.class.getName();
if (poolName
.equals(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME)) {
msg += ". A client cache requires a pool";
}
throw new BeanInitializationException(msg);
}
p = getBeanFactory().getBean(poolName, Pool.class);
} else {
if (log.isDebugEnabled()) {
log.debug("Checking for a unique pool");
}
p = getBeanFactory().getBean(Pool.class);
this.poolName = p.getName();
}
}
if (p != null) {
// copy the pool settings - this way if the pool is not found, at least the cache will have a similar config
// 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());
ccf.setPoolIdleTimeout(p.getIdleTimeout());
ccf.setPoolLoadConditioningInterval(p.getLoadConditioningInterval());
@@ -132,7 +154,8 @@ public class ClientCacheFactoryBean extends CacheFactoryBean {
ccf.setPoolStatisticInterval(p.getStatisticInterval());
ccf.setPoolSubscriptionAckInterval(p.getSubscriptionAckInterval());
ccf.setPoolSubscriptionEnabled(p.getSubscriptionEnabled());
ccf.setPoolSubscriptionMessageTrackingTimeout(p.getSubscriptionMessageTrackingTimeout());
ccf.setPoolSubscriptionMessageTrackingTimeout(p
.getSubscriptionMessageTrackingTimeout());
ccf.setPoolSubscriptionRedundancy(p.getSubscriptionRedundancy());
ccf.setPoolThreadLocalConnections(p.getThreadLocalConnections());
@@ -143,7 +166,6 @@ public class ClientCacheFactoryBean extends CacheFactoryBean {
}
}
List<InetSocketAddress> servers = p.getServers();
if (locators != null) {
for (InetSocketAddress inet : servers) {

View File

@@ -173,6 +173,9 @@ public class ClientRegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V>
}
factory.setPoolName(poolName);
} else {
Pool pool = beanFactory.getBean(Pool.class);
factory.setPoolName(pool.getName());
}
if (diskStoreName != null) {

View File

@@ -20,7 +20,6 @@ import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanNameAware;
@@ -38,10 +37,11 @@ import com.gemstone.gemfire.cache.client.PoolManager;
import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
/**
* Factory bean for easy declaration and configuration of a GemFire pool.
* If a new pool is created, its life-cycle is bound to that of the declaring container.
* Factory bean for easy declaration and configuration of a GemFire pool. If a
* new pool is created, its life-cycle is bound to that of the declaring
* container.
*
* Note that if the pool already exists, it will be returned as is, without any
* Note that if the pool already exists, it will be returned as is, without any
* modifications and its life cycle untouched by this factory.
*
* @see PoolManager
@@ -50,8 +50,8 @@ import com.gemstone.gemfire.distributed.internal.InternalDistributedSystem;
*
* @author Costin Leau
*/
public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, DisposableBean, BeanNameAware,
BeanClassLoaderAware, BeanFactoryAware {
public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean,
DisposableBean, BeanNameAware, BeanFactoryAware {
private static final Log log = LogFactory.getLog(PoolFactoryBean.class);
@@ -66,7 +66,6 @@ public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, Dis
private Collection<PoolConnection> locators;
private Collection<PoolConnection> servers;
private ClassLoader classLoader = getClass().getClassLoader();
private BeanFactory beanFactory;
private boolean keepAlive = false;
@@ -120,14 +119,17 @@ public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, Dis
pool = existingPool;
internalPool = false;
if (log.isDebugEnabled())
log.debug("Pool '" + name + " already exists; using found instance...");
}
else {
log.debug("Pool '" + name
+ " already exists; using found instance...");
} else {
if (log.isDebugEnabled())
log.debug("No pool named '" + name + "' found. Creating a new once...");
log.debug("No pool named '" + name
+ "' found. Creating a new once...");
if (CollectionUtils.isEmpty(locators) && CollectionUtils.isEmpty(servers)) {
throw new IllegalArgumentException("at least one locator or server is required");
if (CollectionUtils.isEmpty(locators)
&& CollectionUtils.isEmpty(servers)) {
throw new IllegalArgumentException(
"at least one locator or server is required");
}
internalPool = true;
@@ -136,13 +138,15 @@ public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, Dis
if (!CollectionUtils.isEmpty(locators)) {
for (PoolConnection connection : locators) {
poolFactory.addLocator(connection.getHost(), connection.getPort());
poolFactory.addLocator(connection.getHost(),
connection.getPort());
}
}
if (!CollectionUtils.isEmpty(servers)) {
for (PoolConnection connection : servers) {
poolFactory.addServer(connection.getHost(), connection.getPort());
poolFactory.addServer(connection.getHost(),
connection.getPort());
}
}
@@ -161,7 +165,8 @@ public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, Dis
poolFactory.setStatisticInterval(statisticInterval);
poolFactory.setSubscriptionEnabled(subscriptionEnabled);
poolFactory.setSubscriptionAckInterval(subscriptionAckInterval);
poolFactory.setSubscriptionMessageTrackingTimeout(subscriptionMessageTrackingTimeout);
poolFactory
.setSubscriptionMessageTrackingTimeout(subscriptionMessageTrackingTimeout);
poolFactory.setSubscriptionRedundancy(subscriptionRedundancy);
poolFactory.setThreadLocalConnections(threadLocalConnections);
@@ -185,169 +190,189 @@ public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, Dis
}
/**
* @param pool the pool to set
* @param pool
* the pool to set
*/
public void setPool(Pool pool) {
this.pool = pool;
}
/**
* @param name the name to set
* @param name
* the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @param locators the locators to set
* @param locators
* the locators to set
*/
public void setLocators(Collection<PoolConnection> locators) {
this.locators = locators;
}
/**
* @param servers the servers to set
* @param servers
* the servers to set
*/
public void setServers(Collection<PoolConnection> servers) {
this.servers = servers;
}
/**
* @param keepAlive the keepAlive to set
* @param keepAlive
* the keepAlive to set
*/
public void setKeepAlive(boolean keepAlive) {
this.keepAlive = keepAlive;
}
/**
* @param freeConnectionTimeout the freeConnectionTimeout to set
* @param freeConnectionTimeout
* the freeConnectionTimeout to set
*/
public void setFreeConnectionTimeout(int freeConnectionTimeout) {
this.freeConnectionTimeout = freeConnectionTimeout;
}
/**
* @param idleTimeout the idleTimeout to set
* @param idleTimeout
* the idleTimeout to set
*/
public void setIdleTimeout(long idleTimeout) {
this.idleTimeout = idleTimeout;
}
/**
* @param loadConditioningInterval the loadConditioningInterval to set
* @param loadConditioningInterval
* the loadConditioningInterval to set
*/
public void setLoadConditioningInterval(int loadConditioningInterval) {
this.loadConditioningInterval = loadConditioningInterval;
}
/**
* @param maxConnections the maxConnections to set
* @param maxConnections
* the maxConnections to set
*/
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
/**
* @param minConnections the minConnections to set
* @param minConnections
* the minConnections to set
*/
public void setMinConnections(int minConnections) {
this.minConnections = minConnections;
}
/**
* @param pingInterval the pingInterval to set
* @param pingInterval
* the pingInterval to set
*/
public void setPingInterval(long pingInterval) {
this.pingInterval = pingInterval;
}
/**
* @param readTimeout the readTimeout to set
* @param readTimeout
* the readTimeout to set
*/
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
/**
* @param retryAttempts the retryAttempts to set
* @param retryAttempts
* the retryAttempts to set
*/
public void setRetryAttempts(int retryAttempts) {
this.retryAttempts = retryAttempts;
}
/**
* @param serverGroup the serverGroup to set
* @param serverGroup
* the serverGroup to set
*/
public void setServerGroup(String serverGroup) {
this.serverGroup = serverGroup;
}
/**
* @param socketBufferSize the socketBufferSize to set
* @param socketBufferSize
* the socketBufferSize to set
*/
public void setSocketBufferSize(int socketBufferSize) {
this.socketBufferSize = socketBufferSize;
}
/**
* @param statisticInterval the statisticInterval to set
* @param statisticInterval
* the statisticInterval to set
*/
public void setStatisticInterval(int statisticInterval) {
this.statisticInterval = statisticInterval;
}
/**
* @param subscriptionAckInterval the subscriptionAckInterval to set
* @param subscriptionAckInterval
* the subscriptionAckInterval to set
*/
public void setSubscriptionAckInterval(int subscriptionAckInterval) {
this.subscriptionAckInterval = subscriptionAckInterval;
}
/**
* @param subscriptionEnabled the subscriptionEnabled to set
* @param subscriptionEnabled
* the subscriptionEnabled to set
*/
public void setSubscriptionEnabled(boolean subscriptionEnabled) {
this.subscriptionEnabled = subscriptionEnabled;
}
/**
* @param subscriptionMessageTrackingTimeout the subscriptionMessageTrackingTimeout to set
* @param subscriptionMessageTrackingTimeout
* the subscriptionMessageTrackingTimeout to set
*/
public void setSubscriptionMessageTrackingTimeout(int subscriptionMessageTrackingTimeout) {
public void setSubscriptionMessageTrackingTimeout(
int subscriptionMessageTrackingTimeout) {
this.subscriptionMessageTrackingTimeout = subscriptionMessageTrackingTimeout;
}
/**
* @param subscriptionRedundancy the subscriptionRedundancy to set
* @param subscriptionRedundancy
* the subscriptionRedundancy to set
*/
public void setSubscriptionRedundancy(int subscriptionRedundancy) {
this.subscriptionRedundancy = subscriptionRedundancy;
}
/**
* @param threadLocalConnections the threadLocalConnections to set
* @param threadLocalConnections
* the threadLocalConnections to set
*/
public void setThreadLocalConnections(boolean threadLocalConnections) {
this.threadLocalConnections = threadLocalConnections;
}
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
public void setBeanFactory(BeanFactory beanFactory) {
this.beanFactory = beanFactory;
}
/**
* @param multiUserAuthentication the multiUserAuthentication to set
* @param multiUserAuthentication
* the multiUserAuthentication to set
*/
public void setMultiUserAuthentication(boolean multiUserAuthentication) {
this.multiUserAuthentication = multiUserAuthentication;
}
/**
* @param prSingleHopEnabled the prSingleHopEnabled to set
* @param prSingleHopEnabled
* the prSingleHopEnabled to set
*/
public void setPrSingleHopEnabled(boolean prSingleHopEnabled) {
this.prSingleHopEnabled = prSingleHopEnabled;

View File

@@ -97,7 +97,7 @@ abstract class AbstractRegionParser extends AliasReplacingBeanDefinitionParser {
if (!subRegion) {
String cacheRef = element.getAttribute("cache-ref");
// add cache reference (fallback to default if nothing is specified)
builder.addPropertyReference("cache", (StringUtils.hasText(cacheRef) ? cacheRef : "gemfireCache"));
builder.addPropertyReference("cache", (StringUtils.hasText(cacheRef) ? cacheRef : GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
}
// add attributes
ParsingUtils.setPropertyValue(element, builder, "name");

View File

@@ -180,9 +180,9 @@ class CacheParser extends AbstractSimpleBeanDefinitionParser {
throws BeanDefinitionStoreException {
String name = super.resolveId(element, definition, parserContext);
if (!StringUtils.hasText(name)) {
name = "gemfireCache";
name = GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME;
//For backward compatibility
parserContext.getRegistry().registerAlias("gemfireCache", "gemfire-cache");
parserContext.getRegistry().registerAlias(GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME, "gemfire-cache");
}
return name;
}

View File

@@ -60,7 +60,7 @@ class CacheServerParser extends AbstractSimpleBeanDefinitionParser {
String attr = element.getAttribute("cache-ref");
// add cache reference (fallback to default if nothing is specified)
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : "gemfireCache"));
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
attr = element.getAttribute("groups");
if (StringUtils.hasText(attr)) {

View File

@@ -19,7 +19,6 @@ package org.springframework.data.gemfire.config;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.gemfire.client.ClientCacheFactoryBean;
import org.springframework.util.StringUtils;
import org.w3c.dom.Element;
/**
@@ -38,15 +37,12 @@ class ClientCacheParser extends CacheParser {
@Override
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
super.doParse(element, parserContext, builder);
ParsingUtils.setPropertyValue(element, builder, "pool-name", "poolName");
ParsingUtils.setPropertyValue(element, builder, "pool-name");
}
@Override
protected void postProcessDynamicRegionSupport(Element element, BeanDefinitionBuilder dynamicRegionSupport) {
String poolName = element.getAttribute("pool-name");
if (StringUtils.hasText(poolName)) {
dynamicRegionSupport.addPropertyValue("poolName", poolName);
}
ParsingUtils.setPropertyValue(element, dynamicRegionSupport, "pool-name");
}
}

View File

@@ -73,7 +73,7 @@ class ClientRegionParser extends AliasReplacingBeanDefinitionParser {
attr = element.getAttribute("cache-ref");
// add cache reference (fallback to default if nothing is specified)
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : "gemfireCache"));
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
// eviction + overflow attributes
// client attributes

View File

@@ -53,7 +53,7 @@ class FunctionServiceParser extends AbstractSimpleBeanDefinitionParser {
throws BeanDefinitionStoreException {
String name = super.resolveId(element, definition, parserContext);
if (!StringUtils.hasText(name)) {
name = "gemfireFunctionService";
name = GemfireConstants.DEFAULT_GEMFIRE_FUNCTION_SERVICE_NAME;
}
return name;
}

View File

@@ -0,0 +1,26 @@
/* Copyright 2010-2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on 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.config;
/**
*
* @author David Turanski
*
*/
public interface GemfireConstants {
static final String DEFAULT_GEMFIRE_POOL_NAME = "gemfirePool";
static final String DEFAULT_GEMFIRE_CACHE_NAME = "gemfireCache";
static final String DEFAULT_GEMFIRE_TXMANAGER_NAME = "gemfireTransactionManager";
static final String DEFAULT_GEMFIRE_FUNCTION_SERVICE_NAME = "gemfireFunctionService";
}

View File

@@ -43,7 +43,7 @@ class LookupRegionParser extends AbstractRegionParser {
if (!subRegion) {
String attr = element.getAttribute("cache-ref");
// add cache reference (fallback to default if nothing is specified)
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : "gemfireCache"));
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
}
else {
builder.addPropertyValue("lookupOnly", true);

View File

@@ -53,13 +53,25 @@ abstract class ParsingUtils {
private static final String ALIASES_KEY = ParsingUtils.class.getName() + ":aliases";
static void setPropertyValue(Element element, BeanDefinitionBuilder builder, String attributeName,
String propertyName) {
String propertyName, Object defaultValue) {
String attr = element.getAttribute(attributeName);
if (StringUtils.hasText(attr)) {
builder.addPropertyValue(propertyName, attr);
} else {
if (defaultValue != null) {
builder.addPropertyValue(propertyName, defaultValue);
}
}
}
static void setPropertyValue(Element element, BeanDefinitionBuilder builder, String attributeName,
String propertyName) {
setPropertyValue(element, builder, attributeName, propertyName,null);
}
static void setPropertyValue(Element element, BeanDefinitionBuilder builder, String attributeName) {
setPropertyValue(element, builder, attributeName, Conventions.attributeNameToPropertyName(attributeName));

View File

@@ -88,9 +88,9 @@ class PoolParser extends AbstractSimpleBeanDefinitionParser {
throws BeanDefinitionStoreException {
String name = super.resolveId(element, definition, parserContext);
if (!StringUtils.hasText(name)) {
name = "gemfirePool";
name = GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME;
//For backward compatibility
parserContext.getRegistry().registerAlias("gemfirePool", "gemfire-pool");
parserContext.getRegistry().registerAlias(GemfireConstants.DEFAULT_GEMFIRE_POOL_NAME, "gemfire-pool");
}
return name;
}

View File

@@ -45,7 +45,7 @@ class TransactionManagerParser extends AbstractSingleBeanDefinitionParser {
String attr = element.getAttribute("cache-ref");
// add cache reference (fallback to default if nothing is specified)
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : "gemfireCache"));
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
}
@@ -54,9 +54,9 @@ class TransactionManagerParser extends AbstractSingleBeanDefinitionParser {
throws BeanDefinitionStoreException {
String name = super.resolveId(element, definition, parserContext);
if (!StringUtils.hasText(name)) {
name = "gemfireTransactionManager";
name = GemfireConstants.DEFAULT_GEMFIRE_TXMANAGER_NAME;
//For backward compatibility
parserContext.getRegistry().registerAlias("gemfireTransactionManager", "gemfire-transaction-manager");
parserContext.getRegistry().registerAlias(GemfireConstants.DEFAULT_GEMFIRE_TXMANAGER_NAME, "gemfire-transaction-manager");
}
return name;
}