Merge branch 'sgf217-218' of https://github.com/jxblum/spring-data-gemfire into jxblum-sgf217-218

This commit is contained in:
John Blum
2013-11-07 13:25:17 -08:00
4 changed files with 189 additions and 50 deletions

View File

@@ -29,8 +29,9 @@ import org.w3c.dom.Element;
/**
* Namespace parser for "cache-server" element.
*
* <p/>
* @author Costin Leau
* @author John Blum
*/
class CacheServerParser extends AbstractSimpleBeanDefinitionParser {
@@ -41,7 +42,7 @@ class CacheServerParser extends AbstractSimpleBeanDefinitionParser {
@Override
protected String resolveId(Element element, AbstractBeanDefinition definition, ParserContext parserContext)
throws BeanDefinitionStoreException {
throws BeanDefinitionStoreException {
String name = super.resolveId(element, definition, parserContext);
if (!StringUtils.hasText(name)) {
name = "gemfireServer";
@@ -52,35 +53,38 @@ class CacheServerParser extends AbstractSimpleBeanDefinitionParser {
@Override
protected boolean isEligibleAttribute(Attr attribute, ParserContext parserContext) {
return super.isEligibleAttribute(attribute, parserContext) && !"groups".equals(attribute.getName())
&& !"cache-ref".equals(attribute.getName());
&& !"cache-ref".equals(attribute.getName());
}
@Override
protected void postProcess(BeanDefinitionBuilder builder, Element element) {
String cacheRefAttribute = element.getAttribute("cache-ref");
String attr = element.getAttribute("cache-ref");
// add cache reference (fallback to default if nothing is specified)
builder.addPropertyReference("cache", (StringUtils.hasText(attr) ? attr : GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
builder.addPropertyReference("cache", (StringUtils.hasText(cacheRefAttribute) ? cacheRefAttribute
: GemfireConstants.DEFAULT_GEMFIRE_CACHE_NAME));
attr = element.getAttribute("groups");
if (StringUtils.hasText(attr)) {
builder.addPropertyValue("serverGroups", StringUtils.commaDelimitedListToStringArray(attr));
String groupsAttribute = element.getAttribute("groups");
if (StringUtils.hasText(groupsAttribute)) {
builder.addPropertyValue("serverGroups", StringUtils.commaDelimitedListToStringArray(groupsAttribute));
}
parseSubscription(builder, element);
}
private void parseSubscription(BeanDefinitionBuilder builder, Element element) {
Element subConfig = DomUtils.getChildElementByTagName(element, "subscription-config");
if (subConfig == null) {
return;
}
Element subscriptionConfigElement = DomUtils.getChildElementByTagName(element, "subscription-config");
ParsingUtils.setPropertyValue(subConfig, builder, "capacity", "subscriptionCapacity");
ParsingUtils.setPropertyValue(subConfig, builder, "disk-store", "subscriptionDiskStore");
String attr = element.getAttribute("eviction-type");
if (StringUtils.hasText(attr)) {
builder.addPropertyValue("subscriptionEvictionPolicy", attr.toUpperCase());
if (subscriptionConfigElement != null) {
ParsingUtils.setPropertyValue(subscriptionConfigElement, builder, "capacity", "subscriptionCapacity");
ParsingUtils.setPropertyValue(subscriptionConfigElement, builder, "disk-store", "subscriptionDiskStore");
String evictionTypeAttribute = subscriptionConfigElement.getAttribute("eviction-type");
if (StringUtils.hasText(evictionTypeAttribute)) {
builder.addPropertyValue("subscriptionEvictionPolicy", evictionTypeAttribute.toUpperCase());
}
}
}
}
}

View File

@@ -24,8 +24,8 @@ import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.SmartLifecycle;
import org.springframework.core.io.Resource;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.InterestRegistrationListener;
@@ -35,43 +35,50 @@ import com.gemstone.gemfire.cache.server.ServerLoadProbe;
/**
* FactoryBean for easy creation and configuration of GemFire {@link CacheServer} instances.
*
* <p/>
* @author Costin Leau
* @author John Blum
*/
public class CacheServerFactoryBean implements FactoryBean<CacheServer>, InitializingBean, DisposableBean,
SmartLifecycle {
SmartLifecycle {
private boolean autoStartup = true;
private int port = CacheServer.DEFAULT_PORT;
private int maxConnections = CacheServer.DEFAULT_MAX_CONNECTIONS;
private int maxThreads = CacheServer.DEFAULT_MAX_THREADS;
private boolean notifyBySubscription = CacheServer.DEFAULT_NOTIFY_BY_SUBSCRIPTION;
private int socketBufferSize = CacheServer.DEFAULT_SOCKET_BUFFER_SIZE;
private int maxTimeBetweenPings = CacheServer.DEFAULT_MAXIMUM_TIME_BETWEEN_PINGS;
private int maxMessageCount = CacheServer.DEFAULT_MAXIMUM_MESSAGE_COUNT;
private int messageTimeToLive = CacheServer.DEFAULT_MESSAGE_TIME_TO_LIVE;
private String[] serverGroups = CacheServer.DEFAULT_GROUPS;
private ServerLoadProbe serverLoadProbe = CacheServer.DEFAULT_LOAD_PROBE;
private long loadPollInterval = CacheServer.DEFAULT_LOAD_POLL_INTERVAL;
private String bindAddress = CacheServer.DEFAULT_BIND_ADDRESS;
private String hostNameForClients = CacheServer.DEFAULT_HOSTNAME_FOR_CLIENTS;
private Set<InterestRegistrationListener> listeners = Collections.emptySet();
private SubscriptionEvictionPolicy evictionPolicy = SubscriptionEvictionPolicy.valueOf(ClientSubscriptionConfig.DEFAULT_EVICTION_POLICY.toUpperCase());
private int maxConnections = CacheServer.DEFAULT_MAX_CONNECTIONS;
private int maxMessageCount = CacheServer.DEFAULT_MAXIMUM_MESSAGE_COUNT;
private int maxThreads = CacheServer.DEFAULT_MAX_THREADS;
private int maxTimeBetweenPings = CacheServer.DEFAULT_MAXIMUM_TIME_BETWEEN_PINGS;
private int messageTimeToLive = CacheServer.DEFAULT_MESSAGE_TIME_TO_LIVE;
private int port = CacheServer.DEFAULT_PORT;
private int socketBufferSize = CacheServer.DEFAULT_SOCKET_BUFFER_SIZE;
private int subscriptionCapacity = ClientSubscriptionConfig.DEFAULT_CAPACITY;
private Resource subscriptionDiskStore;
private long loadPollInterval = CacheServer.DEFAULT_LOAD_POLL_INTERVAL;
private Cache cache;
private CacheServer cacheServer;
private ServerLoadProbe serverLoadProbe = CacheServer.DEFAULT_LOAD_PROBE;
private Set<InterestRegistrationListener> listeners = Collections.emptySet();
private String bindAddress = CacheServer.DEFAULT_BIND_ADDRESS;
private String hostNameForClients = CacheServer.DEFAULT_HOSTNAME_FOR_CLIENTS;
private String subscriptionDiskStore;
private String[] serverGroups = CacheServer.DEFAULT_GROUPS;
private SubscriptionEvictionPolicy subscriptionEvictionPolicy = SubscriptionEvictionPolicy.valueOf(
ClientSubscriptionConfig.DEFAULT_EVICTION_POLICY.toUpperCase());
public CacheServer getObject() {
return cacheServer;
}
public Class<?> getObjectType() {
return ((this.cacheServer != null) ? cacheServer.getClass() : CacheServer.class);
return (this.cacheServer != null ? cacheServer.getClass() : CacheServer.class);
}
public boolean isSingleton() {
@@ -79,7 +86,7 @@ public class CacheServerFactoryBean implements FactoryBean<CacheServer>, Initial
}
public void afterPropertiesSet() throws IOException {
Assert.notNull(cache, "cache is required");
Assert.notNull(cache, "A GemFire Cache is required.");
cacheServer = cache.addCacheServer();
cacheServer.setBindAddress(bindAddress);
@@ -100,12 +107,14 @@ public class CacheServerFactoryBean implements FactoryBean<CacheServer>, Initial
cacheServer.registerInterestRegistrationListener(listener);
}
// client config
ClientSubscriptionConfig config = cacheServer.getClientSubscriptionConfig();
config.setCapacity(subscriptionCapacity);
config.setEvictionPolicy(evictionPolicy.name().toLowerCase());
if (subscriptionDiskStore != null)
config.setDiskStoreName(subscriptionDiskStore.getFile().getCanonicalPath());
config.setEvictionPolicy(subscriptionEvictionPolicy.name().toLowerCase());
if (StringUtils.hasText(subscriptionDiskStore)) {
config.setDiskStoreName(subscriptionDiskStore);
}
}
public void destroy() {
@@ -128,7 +137,7 @@ public class CacheServerFactoryBean implements FactoryBean<CacheServer>, Initial
}
public boolean isRunning() {
return (cacheServer != null ? cacheServer.isRunning() : false);
return (cacheServer != null && cacheServer.isRunning());
}
public void start() {
@@ -210,10 +219,10 @@ public class CacheServerFactoryBean implements FactoryBean<CacheServer>, Initial
}
/**
* @param evictionPolicy the evictionPolicy to set
* @param evictionPolicy the subscriptionEvictionPolicy to set
*/
public void setSubscriptionEvictionPolicy(SubscriptionEvictionPolicy evictionPolicy) {
this.evictionPolicy = evictionPolicy;
this.subscriptionEvictionPolicy = evictionPolicy;
}
/**
@@ -223,7 +232,8 @@ public class CacheServerFactoryBean implements FactoryBean<CacheServer>, Initial
this.subscriptionCapacity = subscriptionCapacity;
}
public void setSubscriptionDiskStore(Resource resource) {
this.subscriptionDiskStore = resource;
public void setSubscriptionDiskStore(String diskStoreName) {
this.subscriptionDiskStore = diskStoreName;
}
}
}

View File

@@ -0,0 +1,96 @@
/*
* Copyright 2010-2013 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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import javax.annotation.Resource;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.server.CacheServer;
/**
* The CacheServerIntegrationTest class is a test suite of test cases testing the functionality of GemFire Cache Servers
* configured using the Spring Data GemFire XML namespace.
* <p/>
* @author John Blum
* @see org.junit.Test
* @see org.springframework.data.gemfire.server.CacheServerFactoryBean
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @since 1.3.3
*/
@ContextConfiguration("cache-server-with-subscription-disk-store.xml")
@RunWith(SpringJUnit4ClassRunner.class)
public class CacheServerIntegrationTest {
@Resource(name = "testCacheServer")
private CacheServer cacheServer;
protected static boolean createDirectory(final File path) {
return (path != null && (path.isDirectory() || path.mkdirs()));
}
protected static File createFile(final String pathname) {
return new File(pathname);
}
protected static void deleteRecursive(final File path) {
if (path.isDirectory()) {
for (File file : path.listFiles()) {
deleteRecursive(file);
}
}
path.delete();
}
@BeforeClass
public static void setupBeforeClass() {
assertTrue(createDirectory(createFile("./gemfire/subscription-disk-store")));
}
@AfterClass
public static void tearDownAfterClass() {
deleteRecursive(createFile("./gemfire"));
}
@Test
public void testCacheServerRunningWithSubscription() {
assertNotNull(cacheServer);
assertTrue(cacheServer.isRunning());
assertEquals("localhost", cacheServer.getBindAddress());
assertNotNull(cacheServer.getGroups());
assertEquals(1, cacheServer.getGroups().length);
assertEquals("test-server", cacheServer.getGroups()[0]);
assertEquals(1, cacheServer.getMaxConnections());
assertNotNull(cacheServer.getClientSubscriptionConfig());
assertEquals(512, cacheServer.getClientSubscriptionConfig().getCapacity());
assertEquals("testSubscriptionDiskStore", cacheServer.getClientSubscriptionConfig().getDiskStoreName());
assertTrue("ENTRY".equalsIgnoreCase(cacheServer.getClientSubscriptionConfig().getEvictionPolicy()));
}
}

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="utf-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
">
<util:properties id="cacheServerConfigurationSettings">
<prop key="name">springGemFireCacheServerWithClientSubscriptionTest</prop>
<prop key="log-level">config</prop>
</util:properties>
<gfe:cache properties-ref="cacheServerConfigurationSettings"/>
<gfe:disk-store id="testSubscriptionDiskStore" auto-compact="true" compaction-threshold="75" queue-size="50" max-oplog-size="10" time-interval="600000">
<gfe:disk-dir location="./gemfire/subscription-disk-store" max-size="50"/>
</gfe:disk-store>
<!-- let the GemFire Cache Server port default to 40404 -->
<gfe:cache-server id="testCacheServer" auto-startup="true" bind-address="localhost" groups="test-server"
max-connections="1">
<gfe:subscription-config eviction-type="ENTRY" disk-store="testSubscriptionDiskStore" capacity="512"/>
</gfe:cache-server>
</beans>