SGF-47: add PartitionListener to partitioned region

This commit is contained in:
David Turanski
2012-07-24 16:24:32 -04:00
parent fdf1983081
commit 6b8a45acf8
5 changed files with 99 additions and 19 deletions

View File

@@ -16,71 +16,88 @@
package org.springframework.data.gemfire;
import java.util.List;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import com.gemstone.gemfire.cache.PartitionAttributes;
import com.gemstone.gemfire.cache.PartitionResolver;
import com.gemstone.gemfire.cache.partition.PartitionListener;
/**
* Spring-friendly bean for creating {@link PartitionAttributes}. Eliminates the need of using
* a XML 'factory-method' tag and allows the attributes properties to be set directly.
* Spring-friendly bean for creating {@link PartitionAttributes}. Eliminates the
* need of using a XML 'factory-method' tag and allows the attributes properties
* to be set directly.
*
* @author Costin Leau
* @author David Turanski
*/
@SuppressWarnings("unchecked")
public class PartitionAttributesFactoryBean implements FactoryBean<PartitionAttributes> {
@SuppressWarnings({ "unchecked", "rawtypes" })
public class PartitionAttributesFactoryBean implements FactoryBean<PartitionAttributes>, InitializingBean {
private com.gemstone.gemfire.cache.PartitionAttributesFactory paf = new com.gemstone.gemfire.cache.PartitionAttributesFactory();
private final com.gemstone.gemfire.cache.PartitionAttributesFactory paf = new com.gemstone.gemfire.cache.PartitionAttributesFactory();
private List<PartitionListener> listeners;
@Override
public PartitionAttributes getObject() throws Exception {
return paf.create();
}
@Override
public Class<?> getObjectType() {
return PartitionAttributes.class;
}
@Override
public boolean isSingleton() {
return false;
}
public void setColocatedWith(String colocatedRegionFullPath) {
paf.setColocatedWith(colocatedRegionFullPath);
}
public void setLocalMaxMemory(int mb) {
paf.setLocalMaxMemory(mb);
}
public void setPartitionResolver(PartitionResolver resolver) {
paf.setPartitionResolver(resolver);
}
public void setPartitionListeners(List<PartitionListener> listeners) {
this.listeners = listeners;
}
public void setRecoveryDelay(long recoveryDelay) {
paf.setRecoveryDelay(recoveryDelay);
}
public void setRedundantCopies(int redundantCopies) {
paf.setRedundantCopies(redundantCopies);
}
public void setStartupRecoveryDelay(long startupRecoveryDelay) {
paf.setStartupRecoveryDelay(startupRecoveryDelay);
}
public void setTotalMaxMemory(long mb) {
paf.setTotalMaxMemory(mb);
}
public void setTotalNumBuckets(int numBuckets) {
paf.setTotalNumBuckets(numBuckets);
}
@Override
public void afterPropertiesSet() throws Exception {
if (listeners != null) {
for (PartitionListener listener : listeners) {
paf.addPartitionListener(listener);
}
}
}
}

View File

@@ -16,8 +16,6 @@
package org.springframework.data.gemfire.config;
import java.util.List;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.xml.ParserContext;
import org.springframework.data.gemfire.PartitionAttributesFactoryBean;
@@ -93,13 +91,20 @@ class PartitionedRegionParser extends AbstractRegionParser {
parAttrBuilder.addPropertyValue("totalNumBuckets", Integer.valueOf(attr));
}
//
List<Element> subElements = DomUtils.getChildElementsByTagName(element, "partition-resolver");
//
// // parse nested cache-listener elements
for (Element subElement : subElements) {
Element subElement = DomUtils.getChildElementByTagName(element, "partition-resolver");
// parse nested partition resolver element
if (subElement != null) {
parAttrBuilder.addPropertyValue("partitionResolver",
parsePartitionResolver(parserContext, subElement, builder));
}
subElement = DomUtils.getChildElementByTagName(element, "partition-listener");
// parse nested partition resolver element
if (subElement != null) {
parAttrBuilder.addPropertyValue("partitionListeners",
parsePartitionListeners(parserContext, subElement, builder));
}
//
// // add partition attributes attributes
attrBuilder.addPropertyValue("partitionAttributes", parAttrBuilder.getBeanDefinition());
@@ -110,6 +115,11 @@ class PartitionedRegionParser extends AbstractRegionParser {
}
private Object parsePartitionResolver(ParserContext parserContext, Element subElement, BeanDefinitionBuilder builder) {
return ParsingUtils.parseRefOrSingleNestedBeanDeclaration(parserContext, subElement, builder);
}
private Object parsePartitionListeners(ParserContext parserContext, Element subElement,
BeanDefinitionBuilder builder) {
return ParsingUtils.parseRefOrNestedBeanDeclaration(parserContext, subElement, builder);
}
}

View File

@@ -1106,6 +1106,22 @@ colocate data based on custom criterias (such as colocating trades by month and
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="partition-listener"
type="beanDeclarationType" minOccurs="0"
maxOccurs="1">
<xsd:annotation>
<xsd:documentation
source="com.gemstone.gemfire.cache.partition.PartitionListener"><![CDATA[
The partition listener definition for this region. Defines a callback for partitioned regions, invoked when a partition region is created or any bucket in a partitioned region becomes primary
]]></xsd:documentation>
<xsd:appinfo>
<tool:annotation>
<tool:exports
type="com.gemstone.gemfire.cache.partition.PartitionListener" />
</tool:annotation>
</xsd:appinfo>
</xsd:annotation>
</xsd:element>
<xsd:element name="eviction" minOccurs="0"
maxOccurs="1">
<xsd:annotation>

View File

@@ -18,6 +18,7 @@ package org.springframework.data.gemfire.config;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
@@ -35,7 +36,9 @@ import org.springframework.util.ObjectUtils;
import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.PartitionAttributes;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.RegionAttributes;
import com.gemstone.gemfire.cache.partition.PartitionListener;
/**
* @author Costin Leau
@@ -90,7 +93,38 @@ public class PartitionedRegionNamespaceTest {
RegionAttributes attrs = TestUtils.readField("attributes", fb);
PartitionAttributes pAttr = attrs.getPartitionAttributes();
assertEquals(20, pAttr.getLocalMaxMemory());
assertNotNull(pAttr.getPartitionListeners());
assertEquals(1, pAttr.getPartitionListeners().length);
assertTrue(pAttr.getPartitionListeners()[0] instanceof TestPartitionListener);
}
public static class TestPartitionListener implements PartitionListener {
@Override
public void afterBucketCreated(int arg0, Iterable<?> arg1) {
// TODO Auto-generated method stub
}
@Override
public void afterBucketRemoved(int arg0, Iterable<?> arg1) {
// TODO Auto-generated method stub
}
@Override
public void afterPrimary(int arg0) {
// TODO Auto-generated method stub
}
@Override
public void afterRegionCreate(Region<?, ?> arg0) {
// TODO Auto-generated method stub
}
}
}

View File

@@ -25,6 +25,9 @@
</gfe:cache-listener>
<gfe:cache-loader ref="c-loader"/>
<gfe:cache-writer ref="c-writer"/>
<gfe:partition-listener>
<bean class="org.springframework.data.gemfire.config.PartitionedRegionNamespaceTest.TestPartitionListener"/>
</gfe:partition-listener>
</gfe:partitioned-region>
<bean id="c-listener" class="org.springframework.data.gemfire.SimpleCacheListener"/>