Search in sources :

Example 21 with CacheListenerAdapter

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

the class ConflationDUnitTest method createClientCache2CommonWriter.

/**
   * create client 2 with 2 regions with sharing a common writer and having a common listener
   * 
   * @throws Exception
   */
public static void createClientCache2CommonWriter(String host, Integer port) throws Exception {
    ConflationDUnitTest test = new ConflationDUnitTest();
    cache = test.createCache(createProperties1());
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setPoolName(createPool(host, "p1", port, true).getName());
    factory.addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            String val = (String) event.getNewValue();
            synchronized (ConflationDUnitTest.class) {
                if (val.equals(MARKER)) {
                    count++;
                } else {
                    counterCreate++;
                }
                if (2 == count) {
                    ConflationDUnitTest.class.notify();
                }
            }
        }

        public void afterUpdate(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            synchronized (this) {
                counterUpdate++;
            }
        }

        public void afterDestroy(EntryEvent event) {
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            synchronized (this) {
                if (!event.getKey().equals(MARKER)) {
                    counterDestroy++;
                }
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME1, attrs);
    cache.createRegion(REGION_NAME2, attrs);
}
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)

Example 22 with CacheListenerAdapter

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

the class ClientConflationDUnitTest method createClientCache.

/**
   * create client 2 with 2 regions each with a unique writer and unique listeners
   * 
   * @throws Exception
   */
public static void createClientCache(String host, Integer port, String conflation) throws Exception {
    ClientConflationDUnitTest test = new ClientConflationDUnitTest();
    cacheClient = test.createCache(createProperties1(conflation));
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    createPool2(host, factory, port);
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            synchronized (ClientConflationDUnitTest.class) {
                counterCreate1++;
            }
        }

        public void afterUpdate(EntryEvent event) {
            // getLogWriter().info("afterUpdate event = " + event, new Exception());
            synchronized (this) {
                counterUpdate1++;
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cacheClient.createRegion(REGION_NAME1, attrs);
    createPool2(host, factory, port);
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            synchronized (ClientConflationDUnitTest.class) {
                counterCreate2++;
            }
        }

        public void afterUpdate(EntryEvent event) {
            synchronized (this) {
                counterUpdate2++;
            }
        }
    });
    attrs = factory.create();
    cacheClient.createRegion(REGION_NAME2, attrs);
}
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)

Example 23 with CacheListenerAdapter

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

the class MyGatewayEventSubstitutionFilter method addCacheListenerAndCloseCache.

public static void addCacheListenerAndCloseCache(String regionName) {
    final Region region = cache.getRegion(Region.SEPARATOR + regionName);
    assertNotNull(region);
    CacheListenerAdapter cl = new CacheListenerAdapter() {

        @Override
        public void afterCreate(EntryEvent event) {
            if ((Long) event.getKey() == 900) {
                cache.getLogger().fine(" Gateway sender is killed by a test");
                cache.close();
                cache.getDistributedSystem().disconnect();
            }
        }
    };
    region.getAttributesMutator().addCacheListener(cl);
}
Also used : CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region)

Example 24 with CacheListenerAdapter

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

the class ClientServerCCEDUnitTest method testClientDoesNotExpireEntryPrematurely.

@Test
public void testClientDoesNotExpireEntryPrematurely() throws Exception {
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm1 = host.getVM(1);
    final String name = this.getUniqueName() + "Region";
    final String key = "testKey";
    int port = createServerRegion(vm0, name, true);
    vm0.invoke(new SerializableCallable("create old entry") {

        public Object call() throws Exception {
            LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
            r.put(key, "value");
            AbstractRegionEntry entry = (AbstractRegionEntry) r.basicGetEntry(key);
            // set an old timestamp in the entry - thirty minutes ago
            entry.getVersionStamp().setVersionTimeStamp(System.currentTimeMillis() - 1800000L);
            return null;
        }
    });
    createClientRegion(vm1, name, port, true, ClientRegionShortcut.CACHING_PROXY, false);
    vm1.invoke(new SerializableCallable("fetch entry and validate") {

        public Object call() throws Exception {
            final Long[] expirationTimeMillis = new Long[1];
            int expirationSeconds = 15;
            LocalRegion r = (LocalRegion) basicGetCache().getRegion(name);
            AttributesMutator mutator = r.getAttributesMutator();
            mutator.setEntryIdleTimeout(new ExpirationAttributes(expirationSeconds, ExpirationAction.LOCAL_DESTROY));
            mutator.addCacheListener(new CacheListenerAdapter() {

                @Override
                public void afterDestroy(EntryEvent event) {
                    expirationTimeMillis[0] = System.currentTimeMillis();
                }
            });
            // fetch the entry from the server and make sure it doesn't expire early
            if (!r.containsKey(key)) {
                r.get(key);
            }
            final long expirationTime = System.currentTimeMillis() + (expirationSeconds * 1000);
            Awaitility.await("waiting for object to expire").atMost(expirationSeconds * 2, TimeUnit.SECONDS).until(() -> {
                return expirationTimeMillis[0] != null;
            });
            disconnectFromDS();
            assertTrue("entry expired " + (expirationTime - expirationTimeMillis[0]) + " milliseconds early", expirationTimeMillis[0] >= expirationTime);
            return null;
        }
    });
    vm0.invoke(new SerializableRunnable() {

        public void run() {
            disconnectFromDS();
        }
    });
}
Also used : SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) LocalRegion(org.apache.geode.internal.cache.LocalRegion) IgnoredException(org.apache.geode.test.dunit.IgnoredException) AbstractRegionEntry(org.apache.geode.internal.cache.AbstractRegionEntry) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) EntryEvent(org.apache.geode.cache.EntryEvent) ExpirationAttributes(org.apache.geode.cache.ExpirationAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test)

Example 25 with CacheListenerAdapter

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

the class PartitionedRegionDUnitTest method testRegionInvalidationWithAdjunctMessages.

/**
   * Bug #47235 concerns assertion failures being thrown when there is a member that receives
   * adjunct messages (as in a WAN gateway, a peer with clients, etc).
   */
@Test
public void testRegionInvalidationWithAdjunctMessages() throws Exception {
    final String name = getUniqueName();
    VM vm1 = Host.getHost(0).getVM(1);
    Cache cache = getCache();
    RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
    Region pr = fact.create(name + "Region");
    pr.put("Object1", "Value1");
    vm1.invoke(new SerializableRunnable("create PR") {

        @Override
        public void run() {
            RegionFactory fact = getCache().createRegionFactory(RegionShortcut.PARTITION);
            fact.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
            fact.addCacheListener(new CacheListenerAdapter() {

                @Override
                public void afterInvalidate(EntryEvent event) {
                    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("afterInvalidate invoked with " + event);
                    InvalidateInvoked = true;
                }
            });
            fact.create(name + "Region");
        }
    });
    try {
        pr.invalidateRegion();
        assertTrue("vm1 should have invoked the listener for an invalidateRegion operation", (Boolean) vm1.invoke(new SerializableCallable("getStatus") {

            public Object call() {
                return InvalidateInvoked;
            }
        }));
    } finally {
        disconnectAllFromDS();
    }
}
Also used : RegionFactory(org.apache.geode.cache.RegionFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) VM(org.apache.geode.test.dunit.VM) EntryEvent(org.apache.geode.cache.EntryEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Region(org.apache.geode.cache.Region) Cache(org.apache.geode.cache.Cache) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

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