Search in sources :

Example 26 with CacheListener

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

the class HAClientCountEventListener method createClientCache.

public static void createClientCache(String host, Integer port1, Boolean isListenerPresent) throws Exception {
    int PORT1 = port1.intValue();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new HAConflationDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    ClientServerTestCase.configureConnectionPool(factory, host, new int[] { PORT1 }, true, -1, -1, null);
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setEnableConflation(true);
    if (isListenerPresent.booleanValue() == true) {
        CacheListener clientListener = new HAClientCountEventListener();
        factory.setCacheListener(clientListener);
    }
    RegionAttributes attrs = factory.create();
    basicGetCache().createRegion(regionName, attrs);
    Region region = basicGetCache().getRegion(Region.SEPARATOR + regionName);
    assertNotNull(region);
    region.registerInterest(KEY1);
    region.registerInterest(KEY2);
    region.registerInterest(KEY3);
    region.registerInterest(LAST_KEY);
    lastKeyArrived = false;
    actualNoEvents = 0;
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) RegionAttributes(org.apache.geode.cache.RegionAttributes) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) CacheListener(org.apache.geode.cache.CacheListener)

Example 27 with CacheListener

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

the class DurableClientQueueSizeDUnitTest method ackedEventsShouldBeRemovedFromTheQueueEventuallyEvenIfNoNewMessagesAreSent.

// Slows down client consumption from queue, this should cause a delay from server queue dispatch
// and receiving an ack from the client. The server should still clean up acked events from the
// queue even if no more messages are being sent. Previously events would get stuck in the queue
// if no new messages were sent because clean up was only done while sending messages
@Test
public void ackedEventsShouldBeRemovedFromTheQueueEventuallyEvenIfNoNewMessagesAreSent() throws Exception {
    int num = 10;
    CacheListener slowListener = new SlowListener();
    vm1.invoke(() -> DurableClientQueueSizeDUnitTest.closeCache());
    vm2.invoke(DurableClientQueueSizeDUnitTest.class, "createClientCache", new Object[] { vm2.getHost(), new Integer[] { port0, port1 }, "300", Boolean.TRUE, Boolean.FALSE, slowListener });
    vm2.invoke(() -> DurableClientQueueSizeDUnitTest.doRI());
    vm2.invoke(() -> DurableClientQueueSizeDUnitTest.readyForEvents());
    vm0.invoke(() -> DurableClientQueueSizeDUnitTest.doPutsIntoRegion(REGION_NAME, num));
    vm0.invoke(() -> Awaitility.waitAtMost(45, TimeUnit.SECONDS).until(() -> {
        CacheClientProxy ccp = DurableClientQueueSizeDUnitTest.getCacheClientProxy(MY_DURABLE_CLIENT);
        assertEquals(0, ccp.getQueueSize());
        assertEquals(0, ccp.getQueueSizeStat());
    }));
}
Also used : CacheListener(org.apache.geode.cache.CacheListener) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 28 with CacheListener

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

the class GemfireDataCommandsDUnitTest method verifyCacheListenerInvocations.

private SerializableRunnable verifyCacheListenerInvocations(final String regionName, boolean callbacksShouldHaveBeenInvoked) {
    return new SerializableRunnable() {

        public void run() {
            Region region = getCache().getRegion(regionName);
            CacheListener<?, ?>[] listeners = region.getAttributes().getCacheListeners();
            for (CacheListener<?, ?> listener : listeners) {
                if (listener instanceof CountingCacheListener) {
                    CountingCacheListener ccl = (CountingCacheListener) listener;
                    if (callbacksShouldHaveBeenInvoked) {
                        assertNotEquals(0, ccl.getEvents());
                    } else {
                        assertEquals(0, ccl.getEvents());
                    }
                }
            }
        }
    };
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Region(org.apache.geode.cache.Region) CacheListener(org.apache.geode.cache.CacheListener)

Example 29 with CacheListener

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

the class AbstractRegion method initCacheListeners.

@Override
public void initCacheListeners(CacheListener[] newListeners) {
    checkReadiness();
    CacheListener[] listenersToAdd = null;
    if (newListeners != null) {
        listenersToAdd = new CacheListener[newListeners.length];
        for (int i = 0; i < newListeners.length; i++) {
            listenersToAdd[i] = wrapRegionMembershipListener(newListeners[i]);
        }
    }
    CacheListener[] oldListeners;
    synchronized (this.clSync) {
        oldListeners = this.cacheListeners;
        if (listenersToAdd == null || listenersToAdd.length == 0) {
            this.cacheListeners = EMPTY_LISTENERS;
        } else {
            // we have some listeners to add
            if (Arrays.asList(listenersToAdd).contains(null)) {
                throw new IllegalArgumentException(LocalizedStrings.AbstractRegion_INITCACHELISTENERS_PARAMETER_HAD_A_NULL_ELEMENT.toLocalizedString());
            }
            CacheListener[] newCacheListeners = new CacheListener[listenersToAdd.length];
            System.arraycopy(listenersToAdd, 0, newCacheListeners, 0, newCacheListeners.length);
            this.cacheListeners = newCacheListeners;
        }
    }
    // moved the following out of the sync for bug 34512
    if (listenersToAdd == null || listenersToAdd.length == 0) {
        if (oldListeners != null && oldListeners.length > 0) {
            for (CacheListener oldListener : oldListeners) {
                closeCacheCallback(oldListener);
            }
            cacheListenersChanged(false);
        }
    } else {
        // we had some listeners to add
        if (oldListeners != null && oldListeners.length > 0) {
            for (CacheListener oldListener : oldListeners) {
                closeCacheCallback(oldListener);
            }
        } else {
            cacheListenersChanged(true);
        }
    }
}
Also used : CacheListener(org.apache.geode.cache.CacheListener) ExtensionPoint(org.apache.geode.internal.cache.extension.ExtensionPoint) SimpleExtensionPoint(org.apache.geode.internal.cache.extension.SimpleExtensionPoint)

Example 30 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)

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