Search in sources :

Example 16 with CacheListenerAdapter

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

the class EventIDVerificationDUnitTest method createServerCache.

public static Integer createServerCache() throws Exception {
    new EventIDVerificationDUnitTest().createCache(new Properties());
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setDataPolicy(DataPolicy.REPLICATE);
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            synchronized (EventIDVerificationDUnitTest.class) {
                gotCallback = true;
                testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
                EventIDVerificationDUnitTest.class.notify();
            }
        }

        public void afterUpdate(EntryEvent event) {
            synchronized (EventIDVerificationDUnitTest.class) {
                gotCallback = true;
                testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
                EventIDVerificationDUnitTest.class.notify();
            }
        }

        public void afterDestroy(EntryEvent event) {
            synchronized (EventIDVerificationDUnitTest.class) {
                gotCallback = true;
                testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
                EventIDVerificationDUnitTest.class.notify();
            }
        }

        public void afterRegionDestroy(RegionEvent event) {
            synchronized (EventIDVerificationDUnitTest.class) {
                gotCallback = true;
                testEventIDResult = ((RegionEventImpl) event).getEventId().equals(eventId);
                EventIDVerificationDUnitTest.class.notify();
            }
        }

        public void afterRegionClear(RegionEvent event) {
            synchronized (EventIDVerificationDUnitTest.class) {
                gotCallback = true;
                // verifyEventIDsDuringRegionDestroy(event);
                testEventIDResult = ((RegionEventImpl) event).getEventId().equals(eventId);
                EventIDVerificationDUnitTest.class.notify();
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, attrs);
    int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    CacheServer server1 = cache.addCacheServer();
    server1.setPort(port);
    server1.setNotifyBySubscription(true);
    server1.start();
    return new Integer(server1.getPort());
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) EntryEvent(org.apache.geode.cache.EntryEvent) CacheServer(org.apache.geode.cache.server.CacheServer) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent)

Example 17 with CacheListenerAdapter

use of org.apache.geode.cache.util.CacheListenerAdapter 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)

Example 18 with CacheListenerAdapter

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

the class ClientServerTransactionDUnitTest method testCallbacks.

// Disabled due to bug 47083
@Ignore
@Test
public void testCallbacks() {
    Host host = Host.getHost(0);
    VM datastore = host.getVM(1);
    VM client = host.getVM(2);
    int port = createRegionsAndStartServer(datastore, false);
    createClientRegion(client, port, true);
    class ClientTxListener extends TransactionListenerAdapter {

        private boolean afterRollbackInvoked = false;

        boolean afterCommitInvoked = false;

        @Override
        public void afterCommit(TransactionEvent event) {
            afterCommitInvoked = true;
            verifyEvent(event);
        }

        @Override
        public void afterRollback(TransactionEvent event) {
            afterRollbackInvoked = true;
            verifyEvent(event);
        }

        protected void verifyEvent(TransactionEvent event) {
            Iterator it = event.getEvents().iterator();
            int i = 0;
            while (it.hasNext()) {
                EntryEvent ev = (EntryEvent) it.next();
                if (i == 0)
                    assertNull(ev.getNewValue());
                if (i > 1) {
                    assertEquals(new CustId(i), ev.getKey());
                    assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
                }
                assertTrue(ev.isOriginRemote());
                i++;
            }
            assertEquals(5, event.getEvents().size());
        }
    }
    class ClientTxWriter implements TransactionWriter {

        boolean txWriterCalled = false;

        public void close() {
        }

        public void beforeCommit(TransactionEvent event) throws TransactionWriterException {
            txWriterCalled = true;
            Iterator it = event.getEvents().iterator();
            int i = 0;
            while (it.hasNext()) {
                EntryEvent ev = (EntryEvent) it.next();
                if (i == 0)
                    assertNull(ev.getNewValue());
                if (i > 1) {
                    assertEquals(new CustId(i), ev.getKey());
                    assertEquals(new Customer("name" + i, "address" + i), ev.getNewValue());
                }
                assertTrue(ev.isOriginRemote());
                i++;
            }
            assertEquals(5, event.getEvents().size());
        }
    }
    class ClientListener extends CacheListenerAdapter {

        boolean invoked = false;

        @Override
        public void afterCreate(EntryEvent event) {
            CustId c = (CustId) event.getKey();
            if (c.getCustId() > 1) {
                invoked = true;
            }
            // we document that client transaction are proxied to the server
            // and that the callback should be handled appropriately (hence true)
            assertTrue(event.isOriginRemote());
            assertTrue(event.isOriginRemote());
        }

        @Override
        public void afterUpdate(EntryEvent event) {
            assertFalse(event.isOriginRemote());
        }
    }
    class ClientWriter extends CacheWriterAdapter {

        @Override
        public void beforeCreate(EntryEvent event) throws CacheWriterException {
            CustId c = (CustId) event.getKey();
            if (c.getCustId() < 2) {
                return;
            }
            fail("cache writer should not be invoked");
        }

        @Override
        public void beforeUpdate(EntryEvent event) throws CacheWriterException {
            fail("cache writer should not be invoked");
        }
    }
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            mgr.addListener(new ClientTxListener());
            mgr.setWriter(new ClientTxWriter());
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            pr.getAttributesMutator().addCacheListener(new ServerListener());
            pr.getAttributesMutator().setCacheWriter(new ServerWriter());
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            mgr.addListener(new ClientTxListener());
            try {
                mgr.setWriter(new ClientTxWriter());
                fail("expected exception not thrown");
            } catch (IllegalStateException e) {
            }
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            pr.getAttributesMutator().addCacheListener(new ClientListener());
            pr.getAttributesMutator().setCacheWriter(new ClientWriter());
            return null;
        }
    });
    class doOps extends SerializableCallable {

        public doOps(boolean commit) {
            this.commit = commit;
        }

        boolean commit = false;

        public Object call() throws Exception {
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            CacheTransactionManager mgr = getCache().getCacheTransactionManager();
            pr.put(new CustId(0), new Customer("name0", "address0"));
            pr.put(new CustId(1), new Customer("name1", "address1"));
            mgr.begin();
            pr.invalidate(new CustId(0));
            pr.destroy(new CustId(1));
            for (int i = 2; i < 5; i++) {
                pr.put(new CustId(i), new Customer("name" + i, "address" + i));
            }
            if (commit) {
                mgr.commit();
            } else {
                mgr.rollback();
            }
            return null;
        }
    }
    client.invoke(new doOps(false));
    datastore.invoke(new SerializableCallable() {

        @SuppressWarnings("synthetic-access")
        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
            ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
            assertTrue(l.afterRollbackInvoked);
            return null;
        }
    });
    client.invoke(new doOps(true));
    datastore.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
            ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
            assertTrue(l.afterCommitInvoked);
            ClientTxWriter w = (ClientTxWriter) mgr.getWriter();
            assertTrue(w.txWriterCalled);
            return null;
        }
    });
    client.invoke(new SerializableCallable() {

        public Object call() throws Exception {
            CacheTransactionManager mgr = getGemfireCache().getCacheTransactionManager();
            ClientTxListener l = (ClientTxListener) mgr.getListeners()[0];
            assertFalse(l.afterCommitInvoked);
            Region<CustId, Customer> pr = getGemfireCache().getRegion(CUSTOMER);
            ClientListener cl = (ClientListener) pr.getAttributes().getCacheListeners()[0];
            assertTrue(cl.invoked);
            return null;
        }
    });
}
Also used : TransactionListenerAdapter(org.apache.geode.cache.util.TransactionListenerAdapter) Customer(org.apache.geode.internal.cache.execute.data.Customer) CustId(org.apache.geode.internal.cache.execute.data.CustId) Iterator(java.util.Iterator) CacheWriterAdapter(org.apache.geode.cache.util.CacheWriterAdapter) RollbackException(javax.transaction.RollbackException) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) Ignore(org.junit.Ignore) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Example 19 with CacheListenerAdapter

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

the class PartitionedRegionEvictionDUnitTest method testEntryLRUDeadlock.

/**
   * Test that gets do not need to acquire a lock on the region entry when LRU is enabled. This is
   * bug 42265
   */
@Test
public void testEntryLRUDeadlock() {
    final Host host = Host.getHost(0);
    final VM vm0 = host.getVM(0);
    final VM vm1 = host.getVM(1);
    final String uniqName = getUniqueName();
    final int redundantCopies = 0;
    final int maxBuckets = 8;
    final int maxEntries = 16;
    final String name = uniqName + "-PR";
    final int extraEntries = 4;
    // final int heapPercentage = 66;
    // final int evictorInterval = 100;
    final SerializableRunnable create = new CacheSerializableRunnable("Create Entry LRU with local destroy on a partitioned Region") {

        public void run2() {
            final AttributesFactory factory = new AttributesFactory();
            factory.setOffHeap(isOffHeap());
            factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setTotalNumBuckets(maxBuckets).create());
            factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries, EvictionAction.LOCAL_DESTROY));
            final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
            assertNotNull(pr);
        }
    };
    vm0.invoke(create);
    final SerializableRunnable create2 = new SerializableRunnable("Create Entry LRU with local destroy on a partitioned Region") {

        public void run() {
            try {
                final AttributesFactory factory = new AttributesFactory();
                factory.setOffHeap(isOffHeap());
                factory.setPartitionAttributes(new PartitionAttributesFactory().setRedundantCopies(redundantCopies).setLocalMaxMemory(0).setTotalNumBuckets(maxBuckets).create());
                factory.setEvictionAttributes(EvictionAttributes.createLRUEntryAttributes(maxEntries));
                factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
                // this listener will cause a deadlock if the get ends up
                // locking the entry.
                factory.addCacheListener(new CacheListenerAdapter() {

                    @Override
                    public void afterCreate(EntryEvent event) {
                        Region region = event.getRegion();
                        Object key = event.getKey();
                        region.get(key);
                    }
                });
                final PartitionedRegion pr = (PartitionedRegion) createRootRegion(name, factory.create());
                assertNotNull(pr);
            } catch (final CacheException ex) {
                Assert.fail("While creating Partitioned region", ex);
            }
        }
    };
    vm1.invoke(create2);
    final SerializableRunnable doPuts = new SerializableRunnable("Do Puts") {

        public void run() {
            final PartitionedRegion pr = (PartitionedRegion) getRootRegion(name);
            assertNotNull(pr);
            for (int counter = 0; counter <= maxEntries + extraEntries; counter++) {
                pr.put(new Integer(counter), "value");
            }
        }
    };
    vm0.invoke(doPuts);
}
Also used : CacheException(org.apache.geode.cache.CacheException) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) Host(org.apache.geode.test.dunit.Host) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 20 with CacheListenerAdapter

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

the class HASlowReceiverDUnitTest method createClientCache.

public static void createClientCache(String host, Integer port1, Integer port2, Integer port3, Integer rLevel, Boolean addListener) throws Exception {
    CacheServerTestUtil.disableShufflingOfEndpoints();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new HASlowReceiverDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    PoolImpl p = (PoolImpl) PoolManager.createFactory().addServer("localhost", port1).addServer("localhost", port2).addServer("localhost", port3).setSubscriptionEnabled(true).setSubscriptionRedundancy(rLevel.intValue()).setThreadLocalConnections(true).setMinConnections(6).setReadTimeout(20000).setPingInterval(1000).setRetryAttempts(5).create("HASlowRecieverDUnitTestPool");
    factory.setScope(Scope.LOCAL);
    factory.setPoolName(p.getName());
    if (addListener.booleanValue()) {
        factory.addCacheListener(new CacheListenerAdapter() {

            @Override
            public void afterUpdate(EntryEvent event) {
                if (event.getNewValue().equals("v20")) {
                    try {
                        Thread.sleep(120000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
    }
    RegionAttributes attrs = factory.create();
    cache.createRegion(regionName, attrs);
    pool = p;
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl)

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