Search in sources :

Example 46 with CacheListener

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

the class AbstractRegion method addCacheListener.

@Override
public void addCacheListener(CacheListener aListener) {
    checkReadiness();
    if (aListener == null) {
        throw new IllegalArgumentException(LocalizedStrings.AbstractRegion_ADDCACHELISTENER_PARAMETER_WAS_NULL.toLocalizedString());
    }
    CacheListener wcl = wrapRegionMembershipListener(aListener);
    boolean changed = false;
    synchronized (this.clSync) {
        CacheListener[] oldListeners = this.cacheListeners;
        if (oldListeners == null || oldListeners.length == 0) {
            this.cacheListeners = new CacheListener[] { wcl };
            changed = true;
        } else {
            List<CacheListener> listeners = Arrays.asList(oldListeners);
            if (!listeners.contains(aListener)) {
                this.cacheListeners = (CacheListener[]) ArrayUtils.insert(oldListeners, oldListeners.length, wcl);
            }
        }
    }
    if (changed) {
        // moved the following out of the sync for bug 34512
        cacheListenersChanged(true);
    }
}
Also used : CacheListener(org.apache.geode.cache.CacheListener)

Example 47 with CacheListener

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

the class AbstractRegion method removeCacheListener.

@Override
public void removeCacheListener(CacheListener aListener) {
    checkReadiness();
    if (aListener == null) {
        throw new IllegalArgumentException(LocalizedStrings.AbstractRegion_REMOVECACHELISTENER_PARAMETER_WAS_NULL.toLocalizedString());
    }
    boolean changed = false;
    synchronized (this.clSync) {
        CacheListener[] oldListeners = this.cacheListeners;
        if (oldListeners != null && oldListeners.length > 0) {
            List newListeners = new ArrayList(Arrays.asList(oldListeners));
            if (newListeners.remove(aListener)) {
                if (newListeners.isEmpty()) {
                    this.cacheListeners = EMPTY_LISTENERS;
                } else {
                    CacheListener[] newCacheListeners = new CacheListener[newListeners.size()];
                    newListeners.toArray(newCacheListeners);
                    this.cacheListeners = newCacheListeners;
                }
                closeCacheCallback(aListener);
                if (newListeners.isEmpty()) {
                    changed = true;
                }
            }
        }
    }
    if (changed) {
        cacheListenersChanged(false);
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) CacheListener(org.apache.geode.cache.CacheListener)

Example 48 with CacheListener

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

the class SlowRecDUnitTest method doCreateOtherVm.

private void doCreateOtherVm(final Properties p, final boolean addListener) {
    VM vm = getOtherVm();
    vm.invoke(new CacheSerializableRunnable("create root") {

        public void run2() throws CacheException {
            getSystem(p);
            createAckRegion(true, false);
            AttributesFactory af = new AttributesFactory();
            af.setScope(Scope.DISTRIBUTED_NO_ACK);
            af.setDataPolicy(DataPolicy.REPLICATE);
            if (addListener) {
                CacheListener cl = new CacheListenerAdapter() {

                    public void afterUpdate(EntryEvent event) {
                        // make the slow receiver event slower!
                        try {
                            Thread.sleep(500);
                        } catch (InterruptedException shuttingDown) {
                            fail("interrupted");
                        }
                    }
                };
                af.setCacheListener(cl);
            } else {
                CacheListener cl = new CacheListenerAdapter() {

                    public void afterCreate(EntryEvent event) {
                        if (event.getCallbackArgument() != null) {
                            lastCallback = event.getCallbackArgument();
                        }
                        if (event.getKey().equals("sleepkey")) {
                            int sleepMs = ((Integer) event.getNewValue()).intValue();
                            try {
                                Thread.sleep(sleepMs);
                            } catch (InterruptedException ignore) {
                                fail("interrupted");
                            }
                        }
                    }

                    public void afterUpdate(EntryEvent event) {
                        if (event.getCallbackArgument() != null) {
                            lastCallback = event.getCallbackArgument();
                        }
                        if (event.getKey().equals("sleepkey")) {
                            int sleepMs = ((Integer) event.getNewValue()).intValue();
                            try {
                                Thread.sleep(sleepMs);
                            } catch (InterruptedException ignore) {
                                fail("interrupted");
                            }
                        }
                    }

                    public void afterInvalidate(EntryEvent event) {
                        if (event.getCallbackArgument() != null) {
                            lastCallback = event.getCallbackArgument();
                        }
                    }

                    public void afterDestroy(EntryEvent event) {
                        if (event.getCallbackArgument() != null) {
                            lastCallback = event.getCallbackArgument();
                        }
                    }
                };
                af.setCacheListener(cl);
            }
            Region r1 = createRootRegion("slowrec", af.create());
            // place holder so we receive updates
            r1.create("key", "value");
        }
    });
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CacheException(org.apache.geode.cache.CacheException) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener)

Example 49 with CacheListener

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

the class TXOrderDUnitTest method testFarSideOpForLoad.

/**
   * Tests fix for #40870 Remote CacheListeners invoke afterCreate with Operation.LOCAL_LOAD_CREATE
   * when create executed transactionally"
   */
@Ignore("TODO: test is disabled")
@Test
public void testFarSideOpForLoad() throws Exception {
    Host host = Host.getHost(0);
    VM vm1 = host.getVM(0);
    VM vm2 = host.getVM(1);
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            AttributesFactory af = new AttributesFactory();
            af.setDataPolicy(DataPolicy.REPLICATE);
            af.setScope(Scope.DISTRIBUTED_ACK);
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    assertTrue(e.getOperation().isLocalLoad());
                }
            };
            af.addCacheListener(cl1);
            CacheLoader cl = new CacheLoader() {

                public Object load(LoaderHelper helper) throws CacheLoaderException {
                    LogWriterUtils.getLogWriter().info("Loading value:" + helper.getKey() + "_value");
                    return helper.getKey() + "_value";
                }

                public void close() {
                }
            };
            af.setCacheLoader(cl);
            createRootRegion("r1", af.create());
            return null;
        }
    });
    vm2.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            AttributesFactory af = new AttributesFactory();
            af.setDataPolicy(DataPolicy.REPLICATE);
            af.setScope(Scope.DISTRIBUTED_ACK);
            CacheListener cl1 = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    LogWriterUtils.getLogWriter().info("op:" + e.getOperation().toString());
                    assertTrue(!e.getOperation().isLocalLoad());
                }
            };
            af.addCacheListener(cl1);
            createRootRegion("r1", af.create());
            return null;
        }
    });
    vm1.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            Region r = getRootRegion("r1");
            getCache().getCacheTransactionManager().begin();
            r.get("obj_2");
            getCache().getCacheTransactionManager().commit();
            return null;
        }
    });
}
Also used : Host(org.apache.geode.test.dunit.Host) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CacheException(org.apache.geode.cache.CacheException) CacheListener(org.apache.geode.cache.CacheListener) LoaderHelper(org.apache.geode.cache.LoaderHelper) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) CacheLoader(org.apache.geode.cache.CacheLoader) Ignore(org.junit.Ignore) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 50 with CacheListener

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

the class TXOrderDUnitTest method testFarSideOrder.

/**
   * make sure listeners get invoked in correct order on far side of tx
   */
@Test
public void testFarSideOrder() throws CacheException {
    initOtherId();
    AttributesFactory af = new AttributesFactory();
    af.setDataPolicy(DataPolicy.REPLICATE);
    af.setScope(Scope.DISTRIBUTED_ACK);
    CacheListener cl1 = new CacheListenerAdapter() {

        public void afterCreate(EntryEvent e) {
            assertEquals(getCurrentExpectedKey(), e.getKey());
        }
    };
    af.addCacheListener(cl1);
    Region r1 = createRootRegion("r1", af.create());
    Region r2 = r1.createSubregion("r2", af.create());
    r2.createSubregion("r3", af.create());
    TransactionListener tl1 = new TransactionListenerAdapter() {

        public void afterCommit(TransactionEvent e) {
            assertEquals(6, e.getEvents().size());
            ArrayList keys = new ArrayList();
            Iterator it = e.getEvents().iterator();
            while (it.hasNext()) {
                EntryEvent ee = (EntryEvent) it.next();
                keys.add(ee.getKey());
                assertEquals(null, ee.getCallbackArgument());
                assertEquals(true, ee.isCallbackArgumentAvailable());
            }
            assertEquals(TXOrderDUnitTest.this.expectedKeys, keys);
            TXOrderDUnitTest.this.invokeCount = 1;
        }
    };
    CacheTransactionManager ctm = getCache().getCacheTransactionManager();
    ctm.addListener(tl1);
    this.invokeCount = 0;
    this.clCount = 0;
    this.expectedKeys = Arrays.asList(new String[] { "b", "c", "a", "a2", "c2", "b2" });
    doCommitOtherVm();
    assertEquals(1, this.invokeCount);
    assertEquals(6, this.clCount);
}
Also used : TransactionListener(org.apache.geode.cache.TransactionListener) TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) TransactionEvent(org.apache.geode.cache.TransactionEvent) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener) CacheTransactionManager(org.apache.geode.cache.CacheTransactionManager) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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