Search in sources :

Example 16 with CacheListener

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);
}
Also used : RegionAttributesCreation(org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) ClientCacheCreation(org.apache.geode.internal.cache.xmlcache.ClientCacheCreation) CacheListener(org.apache.geode.cache.CacheListener) Test(org.junit.Test)

Example 17 with CacheListener

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()));
    }
}
Also used : RegionAttributesCreation(org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) ClientCacheCreation(org.apache.geode.internal.cache.xmlcache.ClientCacheCreation) CacheListener(org.apache.geode.cache.CacheListener) Cache(org.apache.geode.cache.Cache) ClientCache(org.apache.geode.cache.client.ClientCache) AttributesMutator(org.apache.geode.cache.AttributesMutator) Test(org.junit.Test)

Example 18 with CacheListener

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");
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) Properties(java.util.Properties) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) CacheListener(org.apache.geode.cache.CacheListener) RegionQueue(org.apache.geode.internal.cache.RegionQueue) CacheException(org.apache.geode.cache.CacheException) AttributesFactory(org.apache.geode.cache.AttributesFactory) HARegion(org.apache.geode.internal.cache.HARegion) Region(org.apache.geode.cache.Region) Ignore(org.junit.Ignore) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 19 with CacheListener

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() });
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener) Cache(org.apache.geode.cache.Cache) AttributesMutator(org.apache.geode.cache.AttributesMutator)

Example 20 with CacheListener

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);
}
Also used : Declarable(org.apache.geode.cache.Declarable) CacheXmlException(org.apache.geode.cache.CacheXmlException) CacheListener(org.apache.geode.cache.CacheListener)

Aggregations

CacheListener (org.apache.geode.cache.CacheListener)55 AttributesFactory (org.apache.geode.cache.AttributesFactory)28 Region (org.apache.geode.cache.Region)24 RegionAttributes (org.apache.geode.cache.RegionAttributes)17 EntryEvent (org.apache.geode.cache.EntryEvent)13 Test (org.junit.Test)13 CacheException (org.apache.geode.cache.CacheException)12 Properties (java.util.Properties)11 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)11 VM (org.apache.geode.test.dunit.VM)11 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)11 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)8 CacheWriter (org.apache.geode.cache.CacheWriter)7 AttributesMutator (org.apache.geode.cache.AttributesMutator)6 Host (org.apache.geode.test.dunit.Host)6 Iterator (java.util.Iterator)5 CacheTransactionManager (org.apache.geode.cache.CacheTransactionManager)5 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)5 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)5 CacheLoader (org.apache.geode.cache.CacheLoader)4