SGF-15
added pool factory and connection
This commit is contained in:
costin
2010-08-31 19:30:20 +03:00
parent 69ce99b3bc
commit faef286cd5
2 changed files with 524 additions and 0 deletions

View File

@@ -0,0 +1,466 @@
/*
* Copyright 2010 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;
import java.util.Collection;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.data.gemfire.client.PoolConnection;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;
import com.gemstone.gemfire.cache.client.Pool;
import com.gemstone.gemfire.cache.client.PoolFactory;
import com.gemstone.gemfire.cache.client.PoolManager;
/**
* 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
* modifications and its life cycle untouched by this factory.
*
* @see PoolManager
* @see PoolFactory
* @see Pool
*
* @author Costin Leau
*/
public class PoolFactoryBean implements FactoryBean<Pool>, InitializingBean, DisposableBean {
private static final Log log = LogFactory.getLog(PoolFactoryBean.class);
// whether the pool has been created internaly or not
private boolean internalPool = true;
private Pool pool;
// pool settings
private String name;
private Collection<PoolConnection> locators;
private Collection<PoolConnection> servers;
private boolean keepAlive = false;
private int freeConnectionTimeout = PoolFactory.DEFAULT_FREE_CONNECTION_TIMEOUT;
private long idleTimeout = PoolFactory.DEFAULT_IDLE_TIMEOUT;
private int loadConditioningInterval = PoolFactory.DEFAULT_LOAD_CONDITIONING_INTERVAL;
private int maxConnections = PoolFactory.DEFAULT_MAX_CONNECTIONS;
private int minConnections = PoolFactory.DEFAULT_MIN_CONNECTIONS;
private long pingInterval = PoolFactory.DEFAULT_PING_INTERVAL;
private int readTimeout = PoolFactory.DEFAULT_READ_TIMEOUT;
private int retryAttempts = PoolFactory.DEFAULT_RETRY_ATTEMPTS;
private String serverGroup = PoolFactory.DEFAULT_SERVER_GROUP;
private int socketBufferSize = PoolFactory.DEFAULT_SOCKET_BUFFER_SIZE;
private int statisticInterval = PoolFactory.DEFAULT_STATISTIC_INTERVAL;
private int subscriptionAckInterval = PoolFactory.DEFAULT_SUBSCRIPTION_ACK_INTERVAL;
private boolean enableSubscription = PoolFactory.DEFAULT_SUBSCRIPTION_ENABLED;
private int subscriptionMessageTrackingTimeout = PoolFactory.DEFAULT_SUBSCRIPTION_MESSAGE_TRACKING_TIMEOUT;
private int subscriptionRedundancy = PoolFactory.DEFAULT_SUBSCRIPTION_REDUNDANCY;
private boolean threadLocalConnections = PoolFactory.DEFAULT_THREAD_LOCAL_CONNECTIONS;
public Class<?> getObjectType() {
return Pool.class;
}
public boolean isSingleton() {
return true;
}
public Pool getObject() throws Exception {
return pool;
}
public void afterPropertiesSet() throws Exception {
Assert.hasText(name, "the pool name is required");
// first check the configured pools
Pool existingPool = PoolManager.find(name);
if (existingPool != null) {
pool = existingPool;
internalPool = false;
if (log.isDebugEnabled())
log.debug("Pool '" + name + " already exists; using found instance...");
}
else {
if (log.isDebugEnabled())
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");
}
internalPool = true;
PoolFactory poolFactory = PoolManager.createFactory();
if (!CollectionUtils.isEmpty(locators)) {
for (PoolConnection connection : locators) {
poolFactory.addLocator(connection.getHost(), connection.getPort());
}
}
if (!CollectionUtils.isEmpty(servers)) {
for (PoolConnection connection : servers) {
poolFactory.addServer(connection.getHost(), connection.getPort());
}
}
poolFactory.setFreeConnectionTimeout(freeConnectionTimeout);
poolFactory.setIdleTimeout(idleTimeout);
poolFactory.setLoadConditioningInterval(loadConditioningInterval);
poolFactory.setMaxConnections(maxConnections);
poolFactory.setMinConnections(minConnections);
poolFactory.setPingInterval(pingInterval);
poolFactory.setReadTimeout(readTimeout);
poolFactory.setRetryAttempts(retryAttempts);
poolFactory.setServerGroup(serverGroup);
poolFactory.setSocketBufferSize(socketBufferSize);
poolFactory.setStatisticInterval(statisticInterval);
poolFactory.setSubscriptionEnabled(enableSubscription);
poolFactory.setSubscriptionAckInterval(subscriptionAckInterval);
poolFactory.setSubscriptionMessageTrackingTimeout(subscriptionMessageTrackingTimeout);
poolFactory.setSubscriptionRedundancy(subscriptionRedundancy);
poolFactory.setThreadLocalConnections(threadLocalConnections);
pool = poolFactory.create(name);
}
}
public void destroy() throws Exception {
if (internalPool && pool != null) {
if (!pool.isDestroyed()) {
pool.releaseThreadLocalConnection();
pool.destroy(keepAlive);
if (log.isDebugEnabled())
log.debug("Destroyed pool '" + name + "'...");
}
}
}
/**
* @return the internalPool
*/
public boolean isInternalPool() {
return internalPool;
}
/**
* @param internalPool the internalPool to set
*/
public void setInternalPool(boolean internalPool) {
this.internalPool = internalPool;
}
/**
* @return the pool
*/
public Pool getPool() {
return pool;
}
/**
* @param pool the pool to set
*/
public void setPool(Pool pool) {
this.pool = pool;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the locators
*/
public Collection<PoolConnection> getLocators() {
return locators;
}
/**
* @param locators the locators to set
*/
public void setLocators(Collection<PoolConnection> locators) {
this.locators = locators;
}
/**
* @return the servers
*/
public Collection<PoolConnection> getServers() {
return servers;
}
/**
* @param servers the servers to set
*/
public void setServers(Collection<PoolConnection> servers) {
this.servers = servers;
}
/**
* @return the keepAlive
*/
public boolean isKeepAlive() {
return keepAlive;
}
/**
* @param keepAlive the keepAlive to set
*/
public void setKeepAlive(boolean keepAlive) {
this.keepAlive = keepAlive;
}
/**
* @return the freeConnectionTimeout
*/
public int getFreeConnectionTimeout() {
return freeConnectionTimeout;
}
/**
* @param freeConnectionTimeout the freeConnectionTimeout to set
*/
public void setFreeConnectionTimeout(int freeConnectionTimeout) {
this.freeConnectionTimeout = freeConnectionTimeout;
}
/**
* @return the idleTimeout
*/
public long getIdleTimeout() {
return idleTimeout;
}
/**
* @param idleTimeout the idleTimeout to set
*/
public void setIdleTimeout(long idleTimeout) {
this.idleTimeout = idleTimeout;
}
/**
* @return the loadConditioningInterval
*/
public int getLoadConditioningInterval() {
return loadConditioningInterval;
}
/**
* @param loadConditioningInterval the loadConditioningInterval to set
*/
public void setLoadConditioningInterval(int loadConditioningInterval) {
this.loadConditioningInterval = loadConditioningInterval;
}
/**
* @return the maxConnections
*/
public int getMaxConnections() {
return maxConnections;
}
/**
* @param maxConnections the maxConnections to set
*/
public void setMaxConnections(int maxConnections) {
this.maxConnections = maxConnections;
}
/**
* @return the minConnections
*/
public int getMinConnections() {
return minConnections;
}
/**
* @param minConnections the minConnections to set
*/
public void setMinConnections(int minConnections) {
this.minConnections = minConnections;
}
/**
* @return the pingInterval
*/
public long getPingInterval() {
return pingInterval;
}
/**
* @param pingInterval the pingInterval to set
*/
public void setPingInterval(long pingInterval) {
this.pingInterval = pingInterval;
}
/**
* @return the readTimeout
*/
public int getReadTimeout() {
return readTimeout;
}
/**
* @param readTimeout the readTimeout to set
*/
public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
}
/**
* @return the retryAttempts
*/
public int getRetryAttempts() {
return retryAttempts;
}
/**
* @param retryAttempts the retryAttempts to set
*/
public void setRetryAttempts(int retryAttempts) {
this.retryAttempts = retryAttempts;
}
/**
* @return the serverGroup
*/
public String getServerGroup() {
return serverGroup;
}
/**
* @param serverGroup the serverGroup to set
*/
public void setServerGroup(String serverGroup) {
this.serverGroup = serverGroup;
}
/**
* @return the socketBufferSize
*/
public int getSocketBufferSize() {
return socketBufferSize;
}
/**
* @param socketBufferSize the socketBufferSize to set
*/
public void setSocketBufferSize(int socketBufferSize) {
this.socketBufferSize = socketBufferSize;
}
/**
* @return the statisticInterval
*/
public int getStatisticInterval() {
return statisticInterval;
}
/**
* @param statisticInterval the statisticInterval to set
*/
public void setStatisticInterval(int statisticInterval) {
this.statisticInterval = statisticInterval;
}
/**
* @return the subscriptionAckInterval
*/
public int getSubscriptionAckInterval() {
return subscriptionAckInterval;
}
/**
* @param subscriptionAckInterval the subscriptionAckInterval to set
*/
public void setSubscriptionAckInterval(int subscriptionAckInterval) {
this.subscriptionAckInterval = subscriptionAckInterval;
}
/**
* @return the enableSubscription
*/
public boolean isEnableSubscription() {
return enableSubscription;
}
/**
* @param enableSubscription the enableSubscription to set
*/
public void setEnableSubscription(boolean enableSubscription) {
this.enableSubscription = enableSubscription;
}
/**
* @return the subscriptionMessageTrackingTimeout
*/
public int getSubscriptionMessageTrackingTimeout() {
return subscriptionMessageTrackingTimeout;
}
/**
* @param subscriptionMessageTrackingTimeout the subscriptionMessageTrackingTimeout to set
*/
public void setSubscriptionMessageTrackingTimeout(int subscriptionMessageTrackingTimeout) {
this.subscriptionMessageTrackingTimeout = subscriptionMessageTrackingTimeout;
}
/**
* @return the subscriptionRedundancy
*/
public int getSubscriptionRedundancy() {
return subscriptionRedundancy;
}
/**
* @param subscriptionRedundancy the subscriptionRedundancy to set
*/
public void setSubscriptionRedundancy(int subscriptionRedundancy) {
this.subscriptionRedundancy = subscriptionRedundancy;
}
/**
* @return the threadLocalConnections
*/
public boolean isThreadLocalConnections() {
return threadLocalConnections;
}
/**
* @param threadLocalConnections the threadLocalConnections to set
*/
public void setThreadLocalConnections(boolean threadLocalConnections) {
this.threadLocalConnections = threadLocalConnections;
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright 2010 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.client;
/**
* Simple holder class used for configuring servers or locators for GemFire pools.
*
* @see com.gemstone.gemfire.cache.client.PoolFactory
*
* @author Costin Leau
*/
public class PoolConnection {
private String host;
private int port;
/**
* @return the host
*/
public String getHost() {
return host;
}
/**
* @param host the host to set
*/
public void setHost(String host) {
this.host = host;
}
/**
* @return the port
*/
public int getPort() {
return port;
}
/**
* @param port the port to set
*/
public void setPort(int port) {
this.port = port;
}
}