Search in sources :

Example 11 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 12 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 13 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 14 with CacheListener

use of org.apache.geode.cache.CacheListener in project geode by apache.

the class ClearMultiVmCallBkDUnitTest method createCache.

public static void createCache() {
    CacheListener aListener = new ListenerCallBk();
    ds = (new ClearMultiVmCallBkDUnitTest()).getSystem(props);
    cache = CacheFactory.create(ds);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    // Set Cachelisterner : aListener
    factory.setCacheListener(aListener);
    RegionAttributes attr = factory.create();
    region = cache.createRegion("map", attr);
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) CacheListener(org.apache.geode.cache.CacheListener)

Example 15 with CacheListener

use of org.apache.geode.cache.CacheListener in project geode by apache.

the class CallbackArgDUnitTest method doCommitOtherVm.

private void doCommitOtherVm() {
    VM vm = getOtherVm();
    vm.invoke(new CacheSerializableRunnable("create root") {

        public void run2() throws CacheException {
            AttributesFactory af = new AttributesFactory();
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    assertEquals(callbackArg, e.getCallbackArgument());
                }
            };
            af.addCacheListener(cl1);
            af.setScope(Scope.DISTRIBUTED_ACK);
            Region r1 = createRootRegion("r1", af.create());
            Region r2 = r1.createSubregion("r2", af.create());
            Region r3 = r2.createSubregion("r3", af.create());
            CacheTransactionManager ctm = getCache().getCacheTransactionManager();
            TransactionListener tl1 = new TransactionListenerAdapter() {

                public void afterCommit(TransactionEvent e) {
                    assertEquals(6, e.getEvents().size());
                    Iterator it = e.getEvents().iterator();
                    while (it.hasNext()) {
                        EntryEvent ee = (EntryEvent) it.next();
                        assertEquals(callbackArg, ee.getCallbackArgument());
                        assertEquals(true, ee.isCallbackArgumentAvailable());
                    }
                }
            };
            ctm.addListener(tl1);
            ctm.begin();
            r2.put("b", "value1", callbackArg);
            r3.put("c", "value2", callbackArg);
            r1.put("a", "value3", callbackArg);
            r1.put("a2", "value4", callbackArg);
            r3.put("c2", "value5", callbackArg);
            r2.put("b2", "value6", callbackArg);
            ctm.commit();
        }
    });
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) CacheException(org.apache.geode.cache.CacheException) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) TransactionEvent(org.apache.geode.cache.TransactionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region)

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