use of org.apache.geode.cache.CacheListener in project geode by apache.
the class CacheXml66DUnitTest method testCacheListener.
/**
* Tests a cache listener with no parameters
*/
@Test
public void testCacheListener() throws Exception {
CacheCreation cache = new CacheCreation();
RegionAttributesCreation attrs = new RegionAttributesCreation(cache);
CacheListener listener = new MyTestCacheListener();
attrs.setCacheListener(listener);
cache.createRegion("root", attrs);
testXml(cache);
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class CacheXml66DUnitTest method testMultipleCacheListener.
/**
* Tests multiple cache listeners on one region
*
* @since GemFire 5.0
*/
@Test
public void testMultipleCacheListener() throws Exception {
CacheCreation cache = new CacheCreation();
RegionAttributesCreation attrs = new RegionAttributesCreation(cache);
CacheListener l1 = new MyTestCacheListener();
CacheListener l2 = new MySecondTestCacheListener();
attrs.addCacheListener(l1);
attrs.addCacheListener(l2);
cache.createRegion("root", attrs);
testXml(cache);
{
Cache c = getCache();
Region r = c.getRegion("root");
assertEquals(Arrays.asList(new CacheListener[] { l1, l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
AttributesMutator am = r.getAttributesMutator();
am.removeCacheListener(l2);
assertEquals(Arrays.asList(new CacheListener[] { l1 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.removeCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] {}), Arrays.asList(r.getAttributes().getCacheListeners()));
am.addCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] { l1 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.addCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] { l1 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.addCacheListener(l2);
assertEquals(Arrays.asList(new CacheListener[] { l1, l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.removeCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] { l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.removeCacheListener(l1);
assertEquals(Arrays.asList(new CacheListener[] { l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
am.initCacheListeners(new CacheListener[] { l1, l2 });
assertEquals(Arrays.asList(new CacheListener[] { l1, l2 }), Arrays.asList(r.getAttributes().getCacheListeners()));
}
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class vmListenerToCheckHARegionQueue method testGIIBug.
@Ignore("TODO")
@Test
public void testGIIBug() throws Exception {
vm0.invoke(putFromVmBeforeGII("vm0_1"));
populateKeySet("vm0_1");
Thread t1 = new Thread() {
public void run() {
try {
createCache(new Properties());
AttributesFactory factory = new AttributesFactory();
factory.setScope(Scope.DISTRIBUTED_ACK);
factory.setDataPolicy(DataPolicy.REPLICATE);
CacheListener regionListener = new vmListenerToCheckHARegionQueue();
factory.setCacheListener(regionListener);
RegionAttributes attrs = factory.create();
Region region = cache.createRegion(REGION_NAME, attrs);
LogWriterUtils.getLogWriter().info("Name of the region is : " + region.getFullPath());
HARegionQueueAttributes hattr = new HARegionQueueAttributes();
// setting expiry time for the regionqueue.
hattr.setExpiryTime(12000000);
RegionQueue regionqueue = null;
regionqueue = HARegionQueue.getHARegionQueueInstance(regionQueueName, cache, hattr, HARegionQueue.NON_BLOCKING_HA_QUEUE, false);
isHARegionQueueUp = true;
vm0.invoke(setStopFlag());
assertNotNull(regionqueue);
} catch (Exception e) {
isTestFailed = true;
e.printStackTrace();
}
}
};
AsyncInvocation[] async = new AsyncInvocation[4];
async[0] = vm0.invokeAsync(putFrmVm("vm0_2"));
t1.start();
ThreadUtils.join(t1, 30 * 1000);
if (isTestFailed)
fail("HARegionQueue can not be created");
for (int count = 0; count < 1; count++) {
ThreadUtils.join(async[count], 30 * 1000);
if (async[count].exceptionOccurred()) {
Assert.fail("Got exception on " + count, async[count].getException());
}
}
total_no_puts[0] = vm0.invoke(() -> HAGIIBugDUnitTest.getTotalNoPuts());
populate_keys_after_gii();
boolean validationFlag = false;
validateResults(validationFlag);
if (keys_set_before_gii.size() != 0)
fail("Data in the HARegion Queue is inconsistent for the keys that are put before GII");
validationFlag = true;
validateResults(validationFlag);
LogWriterUtils.getLogWriter().info("No. of keys that are missed by HARegion Queue during GII " + keys_set_after_gii.size());
if (keys_set_after_gii.size() != 0)
fail("Set of the keys are missed by HARegion Queue during GII");
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class ForceInvalidateEvictionDUnitTest method addListener.
private void addListener(VM vm) {
final String name = getUniqueName();
vm.invoke(new SerializableRunnable() {
public void run() {
Cache cache = getCache();
Region region = cache.getRegion(name);
AttributesMutator am = region.getAttributesMutator();
am.initCacheListeners(new CacheListener[] { new MyListener() });
}
});
}
use of org.apache.geode.cache.CacheListener in project geode by apache.
the class CacheXmlParser method endCacheListener.
/**
* When a <code>cache-listener</code> element is finished, the {@link Parameter}s and class names
* are popped off the stack. The cache listener is instantiated and initialized with the
* parameters, if appropriate.
*/
private void endCacheListener() {
Declarable d = createDeclarable();
if (!(d instanceof CacheListener)) {
throw new CacheXmlException(LocalizedStrings.CacheXmlParser_A_0_IS_NOT_AN_INSTANCE_OF_A_CACHELISTENER.toLocalizedString(d.getClass().getName()));
}
RegionAttributesCreation attrs = peekRegionAttributesContext(CACHE_LISTENER);
attrs.addCacheListener((CacheListener) d);
}
Aggregations