Fixed JIRA issues SGF-219, SGF-220 and SGF-221 involving registering CacheListeners and adding Async Event Queues and Gateway Senders to GemFire Sub-Regions.

This commit is contained in:
John Blum
2013-11-08 16:53:35 -08:00
parent 1c24a402ce
commit ffadfc6cb2
4 changed files with 245 additions and 37 deletions

View File

@@ -68,7 +68,7 @@ public class RegionFactoryBean<K, V> extends RegionLookupFactoryBean<K, V> imple
private Boolean enableGateway;
private Boolean persistent;
private CacheListener<K, V> cacheListeners[];
private CacheListener<K, V>[] cacheListeners;
private CacheLoader<K, V> cacheLoader;

View File

@@ -21,50 +21,42 @@ import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.util.Assert;
import org.springframework.util.ObjectUtils;
import com.gemstone.gemfire.cache.AttributesFactory;
import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.asyncqueue.AsyncEventQueue;
import com.gemstone.gemfire.cache.wan.GatewaySender;
@SuppressWarnings("deprecation")
/**
* FactoryBean for creating a Gemfire Region as a subregion
* FactoryBean for creating a Gemfire Region as a sub-Region.
* <p/>
* @author David Turanski
*
* @author John Blum
* @param <K> - Region Key Type
* @param <V> - Region Value Type
*/
// TODO why does this class extend the com.gemstone.gemfire.cache.AttributesFactory class? AttributesFactory is deprecated!
public class SubRegionFactoryBean<K, V> extends AttributesFactory<K, V> implements FactoryBean<Region<K, V>>,
InitializingBean {
protected final Log log = LogFactory.getLog(getClass());
@SuppressWarnings("unused")
private String name;
private String regionName;
private Region<K, V> subRegion;
private Region<?, ?> parent;
private boolean lookupOnly;
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(parent, "parent region must not be null");
private CacheListener<K, V>[] cacheListeners;
this.subRegion = parent.getSubregion(regionName);
if (this.subRegion == null) {
if (lookupOnly) {
throw new BeanInitializationException("Cannot find region [" + regionName + "] in cache "
+ parent.getRegionService());
}
else {
log.debug("creating subregion of [" + ( parent.getFullPath() == null ? parent.getName() : parent.getFullPath()) + "] with name " + regionName);
this.subRegion = this.parent.createSubregion(regionName, create());
}
}
}
private Object[] asyncEventQueues;
private Object[] gatewaySenders;
private Region<?, ?> parentRegion;
private Region<K, V> subRegion;
@SuppressWarnings("unused")
private String name;
private String regionName;
@Override
public Region<K, V> getObject() throws Exception {
@@ -73,6 +65,7 @@ public class SubRegionFactoryBean<K, V> extends AttributesFactory<K, V> implemen
@Override
public Class<?> getObjectType() {
// TODO perhaps this should be 'return (subRegion != null ? subRegion.getClass() : Region.class);' for consistency.
return Region.class;
}
@@ -81,6 +74,81 @@ public class SubRegionFactoryBean<K, V> extends AttributesFactory<K, V> implemen
return true;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.notNull(parentRegion, "The parent Region cannot be null.");
this.subRegion = parentRegion.getSubregion(regionName);
if (this.subRegion == null) {
if (lookupOnly) {
throw new BeanInitializationException(String.format("Cannot find Region [%1$s] in Cache %2$s",
regionName, parentRegion.getRegionService()));
}
else {
log.debug(String.format("Creating sub-Region of [%1$s] with name [%2$s]...",
(parentRegion.getFullPath() != null ? parentRegion.getFullPath() : parentRegion.getName()),
regionName));
if (!ObjectUtils.isEmpty(asyncEventQueues)) {
for (Object asyncEventQueue : asyncEventQueues) {
addAsyncEventQueueId(((AsyncEventQueue) asyncEventQueue).getId());
}
}
if (!ObjectUtils.isEmpty(cacheListeners)) {
for (CacheListener<K, V> listener : cacheListeners) {
addCacheListener(listener);
}
}
if (!ObjectUtils.isEmpty(gatewaySenders)) {
for (Object gatewaySender : gatewaySenders) {
addGatewaySenderId(((GatewaySender) gatewaySender).getId());
}
}
this.subRegion = this.parentRegion.createSubregion(regionName, create());
}
}
}
/**
*
* @param asyncEventQueues defined as Object for backward compatibility with Gemfire 6.
*/
public void setAsyncEventQueues(Object[] asyncEventQueues) {
this.asyncEventQueues = asyncEventQueues;
}
/**
* Sets the cache listeners used for the region used by this factory. Used
* only when a new region is created.Overrides the settings specified
* through {@link #setAttributes(com.gemstone.gemfire.cache.RegionAttributes)}.
*
* @param cacheListeners the cacheListeners to set on a newly created region
*/
public void setCacheListeners(CacheListener<K, V>[] cacheListeners) {
this.cacheListeners = cacheListeners;
}
/**
*
* @param gatewaySenders
*/
public void setGatewaySenders(Object[] gatewaySenders) {
this.gatewaySenders = gatewaySenders;
}
/**
* Set to true if the subregion should already exist, e.g., specified by
* &lt;lookup-region&gt;
* @param lookupOnly
*/
public void setLookupOnly(boolean lookupOnly) {
this.lookupOnly = lookupOnly;
}
/**
* Set the bean name - the same as the subregion full path
* @param name
@@ -102,16 +170,7 @@ public class SubRegionFactoryBean<K, V> extends AttributesFactory<K, V> implemen
* @param parent
*/
public void setParent(Region<?, ?> parent) {
this.parent = parent;
}
/**
* Set to true if the subregion should already exist, e.g., specified by
* &lt;lookup-region&gt;
* @param lookupOnly
*/
public void setLookupOnly(boolean lookupOnly) {
this.lookupOnly = lookupOnly;
this.parentRegion = parent;
}
}

View File

@@ -0,0 +1,111 @@
/*
* 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.config;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.data.gemfire.test.GemfireTestApplicationContextInitializer;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.gemstone.gemfire.cache.CacheListener;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.asyncqueue.AsyncEvent;
import com.gemstone.gemfire.cache.asyncqueue.AsyncEventListener;
import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
/**
* The SubRegionSubElementNamespaceTest class...
* <p/>
* @author John Blum
* @see org.junit.Test
* @see org.springframework.test.context.ContextConfiguration
* @see org.springframework.test.context.junit4.SpringJUnit4ClassRunner
* @link https://jira.springsource.org/browse/SGF-219
* @link https://jira.springsource.org/browse/SGF-220
* @link https://jira.springsource.org/browse/SGF-221
* @since 1.3.3
*/
@ContextConfiguration(locations = "subregionsubelement-ns.xml",
initializers = GemfireTestApplicationContextInitializer.class)
@RunWith(SpringJUnit4ClassRunner.class)
@SuppressWarnings("unused")
public class SubRegionSubElementNamespaceTest {
@Resource(name = "/Customers/Accounts")
private Region customersAccountsRegion;
@Resource(name = "/Orders/Items")
private Region orderItemsRegion;
@Resource(name = "/Parent/Child")
private Region parentChildRegion;
@Test
public void testCustomersAccountsSubRegionCacheListener() {
assertNotNull(customersAccountsRegion);
assertNotNull(customersAccountsRegion.getAttributes());
assertNotNull(customersAccountsRegion.getAttributes().getCacheListeners());
boolean found = false;
for (CacheListener listener : customersAccountsRegion.getAttributes().getCacheListeners()) {
found |= (listener instanceof TestNoOpCacheListener);
}
assertTrue(String.format("Expected a GemFire CacheListener of type (%1$s) to be registered on Region (%2$s)!",
TestNoOpCacheListener.class.getName(), customersAccountsRegion.getName()), found);
}
@Test
public void testOrderItemsSubRegionGatewaySender() {
assertNotNull(orderItemsRegion);
assertNotNull(orderItemsRegion.getAttributes());
assertNotNull(orderItemsRegion.getAttributes().getGatewaySenderIds());
assertTrue(orderItemsRegion.getAttributes().getGatewaySenderIds().contains("testSender"));
}
@Test
public void testParentChildSubRegionAsyncEventQueue() {
assertNotNull(parentChildRegion);
assertNotNull(parentChildRegion.getAttributes());
assertNotNull(parentChildRegion.getAttributes().getAsyncEventQueueIds());
assertTrue(parentChildRegion.getAttributes().getAsyncEventQueueIds().contains("testQueue"));
}
public static final class TestNoOpCacheListener extends CacheListenerAdapter {
}
public static final class TestNoOpAsyncEventListener implements AsyncEventListener {
@Override
public boolean processEvents(final List<AsyncEvent> events) {
throw new UnsupportedOperationException("Not Implemented!");
}
@Override
public void close() {
}
}
}

View File

@@ -0,0 +1,38 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:gfe="http://www.springframework.org/schema/gemfire"
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-3.2.xsd
http://www.springframework.org/schema/gemfire http://www.springframework.org/schema/gemfire/spring-gemfire-1.3.xsd
">
<gfe:cache/>
<gfe:replicated-region id="Parent" persistent="false">
<gfe:replicated-region name="Child" persistent="false">
<gfe:async-event-queue name="testQueue" maximum-queue-memory="50" parallel="true" dispatcher-threads="4"
order-policy="KEY">
<gfe:async-event-listener>
<bean class="org.springframework.data.gemfire.config.SubRegionSubElementNamespaceTest.TestNoOpAsyncEventListener"/>
</gfe:async-event-listener>
</gfe:async-event-queue>
</gfe:replicated-region>
</gfe:replicated-region>
<gfe:replicated-region id="Customers" persistent="false">
<gfe:replicated-region name="Accounts" persistent="false">
<gfe:cache-listener>
<bean class="org.springframework.data.gemfire.config.SubRegionSubElementNamespaceTest.TestNoOpCacheListener"/>
</gfe:cache-listener>
</gfe:replicated-region>
</gfe:replicated-region>
<gfe:replicated-region id="Orders" persistent="false">
<gfe:replicated-region name="Items" persistent="false">
<gfe:gateway-sender remote-distributed-system-id="21" name="testSender" parallel="true"
manual-start="true"/>
</gfe:replicated-region>
</gfe:replicated-region>
</beans>