Search in sources :

Example 36 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.

the class CallbackArgDUnitTest method doTest.

private void doTest() 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());
            assertEquals(callbackArg, e.getCallbackArgument());
            assertEquals(true, e.isCallbackArgumentAvailable());
        }
    };
    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(callbackArg, ee.getCallbackArgument());
                assertEquals(true, ee.isCallbackArgumentAvailable());
            }
            assertEquals(CallbackArgDUnitTest.this.expectedKeys, keys);
            CallbackArgDUnitTest.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) 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)

Example 37 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.

the class DistributionManagerDUnitTest method getSleepingListener.

static CacheListener getSleepingListener(final boolean playDead) {
    regionDestroyedInvoked = false;
    return new CacheListenerAdapter() {

        @Override
        public void afterCreate(EntryEvent event) {
            try {
                if (playDead) {
                    MembershipManagerHelper.beSickMember(getSystemStatic());
                    MembershipManagerHelper.playDead(getSystemStatic());
                }
                Thread.sleep(15000);
            } catch (InterruptedException ie) {
                fail("interrupted", ie);
            }
        }

        @Override
        public void afterRegionDestroy(RegionEvent event) {
            LogWriter logger = myCache.getLogger();
            logger.info("afterRegionDestroyed invoked in sleeping listener");
            logger.info("<ExpectedException action=remove>service failure</ExpectedException>");
            logger.info("<ExpectedException action=remove>org.apache.geode.ForcedDisconnectException</ExpectedException>");
            regionDestroyedInvoked = true;
        }
    };
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) LogWriter(org.apache.geode.LogWriter) EntryEvent(org.apache.geode.cache.EntryEvent) RegionEvent(org.apache.geode.cache.RegionEvent)

Example 38 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.

the class LRUEvictionControllerDUnitTest method testBug31592.

/**
   * Tests that a Region with an LRU capacity controller can be accessed from inside a cache
   * listener.
   */
@Test
public void testBug31592() throws Exception {
    final String name = this.getUniqueName();
    final Object key = "KEY";
    final Object value = "VALUE";
    final Object key2 = "KEY2";
    final Object value2 = "VALUE2";
    AttributesFactory factory = new AttributesFactory();
    factory.setOffHeap(isOffHeapEnabled());
    factory.setScope(Scope.LOCAL);
    factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(10));
    final Region region = createRegion(name, factory.create());
    region.put(key, value);
    final Throwable[] errors = new Throwable[1];
    region.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            try {
                LogWriterUtils.getLogWriter().info("AFTER CREATE");
                region.put(key, value2);
            } catch (VirtualMachineError e) {
                SystemFailure.initiateFailure(e);
                throw e;
            } catch (Throwable ex) {
                region.getCache().getLogger().severe("failed to access cache from listener", ex);
                errors[0] = ex;
            }
        }
    });
    region.put(key2, value2);
    assertNull(errors[0]);
    assertEquals(value2, region.get(key));
    assertEquals(value2, region.get(key2));
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 39 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.

the class DistributedNoAckRegionCCEDUnitTest method addBlockingListener.

static void addBlockingListener() {
    ListenerBlocking = true;
    CCRegion.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            onEvent(event);
        }

        private void onEvent(EntryEvent event) {
            boolean blocked = false;
            if (event.isOriginRemote()) {
                synchronized (this) {
                    while (ListenerBlocking) {
                        LogWriterUtils.getLogWriter().info("blocking cache operations for " + event.getDistributedMember());
                        blocked = true;
                        try {
                            wait();
                        } catch (InterruptedException e) {
                            Thread.currentThread().interrupt();
                            LogWriterUtils.getLogWriter().info("blocking cache listener interrupted");
                            return;
                        }
                    }
                }
                if (blocked) {
                    LogWriterUtils.getLogWriter().info("allowing cache operations for " + event.getDistributedMember());
                }
            }
        }

        @Override
        public void close() {
            LogWriterUtils.getLogWriter().info("closing blocking listener");
            ListenerBlocking = false;
            synchronized (this) {
                notifyAll();
            }
        }

        @Override
        public void afterUpdate(EntryEvent event) {
            onEvent(event);
        }

        @Override
        public void afterInvalidate(EntryEvent event) {
            onEvent(event);
        }

        @Override
        public void afterDestroy(EntryEvent event) {
            onEvent(event);
        }
    });
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent)

Example 40 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter in project geode by apache.

the class ConnectionPoolDUnitTest method test034NotifyAllUpdates.

/**
   * Tests 'notify-all' client updates. This test verifies that: - only invalidates are sent as part
   * of the 'notify-all' mode of client updates - originators of updates are not sent invalidates -
   * non-originators of updates are sent invalidates - multiple invalidates are not sent for the
   * same update
   */
@Test
public void test034NotifyAllUpdates() throws CacheException {
    final String name = this.getName();
    final Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    VM vm2 = host.getVM(2);
    VM vm3 = host.getVM(3);
    disconnectAllFromDS();
    // Create the cache servers with distributed, mirrored region
    SerializableRunnable createServer = new CacheSerializableRunnable("Create Cache Server") {

        public void run2() throws CacheException {
            CacheLoader cl = new CacheLoader() {

                public Object load(LoaderHelper helper) {
                    return helper.getKey();
                }

                public void close() {
                }
            };
            AttributesFactory factory = getBridgeServerMirroredAckRegionAttributes(cl, null);
            createRegion(name, factory.create());
            // pause(1000);
            try {
                startBridgeServer(0);
            } catch (Exception ex) {
                org.apache.geode.test.dunit.Assert.fail("While starting CacheServer", ex);
            }
        }
    };
    getSystem().getLogWriter().info("before create server");
    vm0.invoke(createServer);
    vm1.invoke(createServer);
    // Create cache server clients
    final int numberOfKeys = 10;
    final String host0 = NetworkUtils.getServerHostName(host);
    final int vm0Port = vm0.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
    final int vm1Port = vm1.invoke(() -> ConnectionPoolDUnitTest.getCacheServerPort());
    SerializableRunnable createClient = new CacheSerializableRunnable("Create Cache Server Client") {

        public void run2() throws CacheException {
            // reset all static listener variables in case this is being rerun in a subclass
            numberOfAfterInvalidates = 0;
            numberOfAfterCreates = 0;
            numberOfAfterUpdates = 0;
            getLonerSystem();
            // create the region
            AttributesFactory factory = new AttributesFactory();
            factory.setScope(Scope.LOCAL);
            factory.setConcurrencyChecksEnabled(false);
            // create bridge writer
            ClientServerTestCase.configureConnectionPool(factory, host0, vm0Port, vm1Port, true, -1, -1, null);
            Region rgn = createRegion(name, factory.create());
        }
    };
    getSystem().getLogWriter().info("before create client");
    vm2.invoke(createClient);
    vm3.invoke(createClient);
    // Initialize each client with entries (so that afterInvalidate is called)
    SerializableRunnable initializeClient = new CacheSerializableRunnable("Initialize Client") {

        public void run2() throws CacheException {
            numberOfAfterInvalidates = 0;
            numberOfAfterCreates = 0;
            numberOfAfterUpdates = 0;
            LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
            for (int i = 0; i < numberOfKeys; i++) {
                assertEquals("key-" + i, region.get("key-" + i));
            }
        }
    };
    getSystem().getLogWriter().info("before initialize client");
    vm2.invoke(initializeClient);
    vm3.invoke(initializeClient);
    // Add a CacheListener to both vm2 and vm3
    vm2.invoke(new CacheSerializableRunnable("Add CacheListener 1") {

        public void run2() throws CacheException {
            LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
            CacheListener listener = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    numberOfAfterCreates++;
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("vm2 numberOfAfterCreates: " + numberOfAfterCreates);
                }

                public void afterUpdate(EntryEvent e) {
                    numberOfAfterUpdates++;
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("vm2 numberOfAfterUpdates: " + numberOfAfterUpdates);
                }

                public void afterInvalidate(EntryEvent e) {
                    numberOfAfterInvalidates++;
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("vm2 numberOfAfterInvalidates: " + numberOfAfterInvalidates);
                }
            };
            region.getAttributesMutator().setCacheListener(listener);
            region.registerInterestRegex(".*", false, false);
        }
    });
    vm3.invoke(new CacheSerializableRunnable("Add CacheListener 2") {

        public void run2() throws CacheException {
            LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
            CacheListener listener = new CacheListenerAdapter() {

                public void afterCreate(EntryEvent e) {
                    numberOfAfterCreates++;
                // getLogWriter().info("vm3 numberOfAfterCreates: " + numberOfAfterCreates);
                }

                public void afterUpdate(EntryEvent e) {
                    numberOfAfterUpdates++;
                // getLogWriter().info("vm3 numberOfAfterUpdates: " + numberOfAfterUpdates);
                }

                public void afterInvalidate(EntryEvent e) {
                    numberOfAfterInvalidates++;
                // getLogWriter().info("vm3 numberOfAfterInvalidates: " + numberOfAfterInvalidates);
                }
            };
            region.getAttributesMutator().setCacheListener(listener);
            region.registerInterestRegex(".*", false, false);
        }
    });
    Wait.pause(3000);
    getSystem().getLogWriter().info("before puts");
    // Use vm2 to put new values
    // This should cause 10 afterUpdates to vm2 and 10 afterInvalidates to vm3
    vm2.invoke(new CacheSerializableRunnable("Put New Values") {

        public void run2() throws CacheException {
            LocalRegion region = (LocalRegion) getRootRegion().getSubregion(name);
            for (int i = 0; i < 10; i++) {
                region.put("key-" + i, "key-" + i);
            }
        }
    });
    getSystem().getLogWriter().info("after puts");
    // Wait to make sure all the updates are received
    Wait.pause(1000);
    long vm2AfterCreates = vm2.invoke(() -> ConnectionPoolDUnitTest.getNumberOfAfterCreates());
    long vm2AfterUpdates = vm2.invoke(() -> ConnectionPoolDUnitTest.getNumberOfAfterUpdates());
    long vm2AfterInvalidates = vm2.invoke(() -> ConnectionPoolDUnitTest.getNumberOfAfterInvalidates());
    long vm3AfterCreates = vm3.invoke(() -> ConnectionPoolDUnitTest.getNumberOfAfterCreates());
    long vm3AfterUpdates = vm3.invoke(() -> ConnectionPoolDUnitTest.getNumberOfAfterUpdates());
    long vm3AfterInvalidates = vm3.invoke(() -> ConnectionPoolDUnitTest.getNumberOfAfterInvalidates());
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("vm2AfterCreates: " + vm2AfterCreates);
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("vm2AfterUpdates: " + vm2AfterUpdates);
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("vm2AfterInvalidates: " + vm2AfterInvalidates);
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("vm3AfterCreates: " + vm3AfterCreates);
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("vm3AfterUpdates: " + vm3AfterUpdates);
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("vm3AfterInvalidates: " + vm3AfterInvalidates);
    assertTrue("VM2 should not have received any afterCreate messages", vm2AfterCreates == 0);
    assertTrue("VM2 should not have received any afterInvalidate messages", vm2AfterInvalidates == 0);
    assertTrue("VM2 received " + vm2AfterUpdates + " afterUpdate messages. It should have received " + numberOfKeys, vm2AfterUpdates == numberOfKeys);
    assertTrue("VM3 should not have received any afterCreate messages", vm3AfterCreates == 0);
    assertTrue("VM3 should not have received any afterUpdate messages", vm3AfterUpdates == 0);
    assertTrue("VM3 received " + vm3AfterInvalidates + " afterInvalidate messages. It should have received " + numberOfKeys, vm3AfterInvalidates == numberOfKeys);
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) NoAvailableServersException(org.apache.geode.cache.client.NoAvailableServersException) CancelException(org.apache.geode.CancelException) IOException(java.io.IOException) Endpoint(org.apache.geode.cache.client.internal.Endpoint) CertifiableTestCacheListener(org.apache.geode.cache30.CertifiableTestCacheListener) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) TestCacheLoader(org.apache.geode.cache30.TestCacheLoader) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Aggregations

CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)66 EntryEvent (org.apache.geode.cache.EntryEvent)55 AttributesFactory (org.apache.geode.cache.AttributesFactory)40 Region (org.apache.geode.cache.Region)30 Test (org.junit.Test)25 RegionAttributes (org.apache.geode.cache.RegionAttributes)21 Properties (java.util.Properties)20 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)16 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)15 VM (org.apache.geode.test.dunit.VM)14 CacheException (org.apache.geode.cache.CacheException)11 CacheListener (org.apache.geode.cache.CacheListener)11 LocalRegion (org.apache.geode.internal.cache.LocalRegion)9 Host (org.apache.geode.test.dunit.Host)9 RegionEvent (org.apache.geode.cache.RegionEvent)8 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)8 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)7 CacheServer (org.apache.geode.cache.server.CacheServer)7 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)7 SubscriptionAttributes (org.apache.geode.cache.SubscriptionAttributes)6