Search in sources :

Example 56 with CacheListenerAdapter

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

the class ConflationDUnitTest method createClientCache2UniqueWriter.

/**
   * create client 2 with 2 regions each with a unique writer but both having a common listener
   * 
   * @throws Exception
   */
public static void createClientCache2UniqueWriter(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) {
            String val = (String) event.getNewValue();
            LogWriterUtils.getLogWriter().info("Listener received event " + event);
            synchronized (ConflationDUnitTest.class) {
                if (val.equals(MARKER)) {
                    count++;
                } else {
                    counterCreate++;
                }
                if (2 == count) {
                    ConflationDUnitTest.class.notify();
                }
            }
        }

        public void afterUpdate(EntryEvent event) {
            synchronized (this) {
                counterUpdate++;
            }
        }

        public void afterDestroy(EntryEvent event) {
            synchronized (this) {
                if (!event.getKey().equals(MARKER)) {
                    counterDestroy++;
                }
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME1, attrs);
    factory.setPoolName(createPool(host, "p2", port, true).getName());
    attrs = factory.create();
    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 57 with CacheListenerAdapter

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

the class ConflationDUnitTest method createClientCache2CommonWriterTest3.

public static void createClientCache2CommonWriterTest3(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 58 with CacheListenerAdapter

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

the class ClientToServerDeltaDUnitTest method createClientCache.

/*
   * create client cache
   */
public static void createClientCache(String host, Integer port, Boolean attachListener, Boolean isEmpty, Boolean isCq, String[] cqQueryString, Boolean registerInterestAll, Boolean enableSubscription) throws Exception {
    updates = 0;
    create = 0;
    firstUpdate = null;
    secondUpdate = null;
    error = false;
    lastKeyReceived = false;
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new ClientToServerDeltaDUnitTest().createCache(props);
    pool = (PoolImpl) PoolManager.createFactory().addServer(host, port.intValue()).setThreadLocalConnections(true).setMinConnections(2).setSubscriptionEnabled(enableSubscription).setSubscriptionRedundancy(0).setReadTimeout(10000).setPingInterval(1000).setSocketBufferSize(32768).create("ClientToServerDeltaDunitTestPool");
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setConcurrencyChecksEnabled(true);
    if (isEmpty) {
        factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
        factory.setDataPolicy(DataPolicy.EMPTY);
    } else {
        factory.setDataPolicy(DataPolicy.NORMAL);
    }
    factory.setPoolName(pool.getName());
    factory.setCloningEnabled(false);
    // region with empty data policy
    RegionAttributes attrs = factory.create();
    region = cache.createRegion(REGION_NAME, attrs);
    if (attachListener) {
        region.getAttributesMutator().addCacheListener(new CacheListenerAdapter() {

            @Override
            public void afterCreate(EntryEvent event) {
                create++;
                if (LAST_KEY.equals(event.getKey())) {
                    lastKeyReceived = true;
                }
                ;
            }

            @Override
            public void afterUpdate(EntryEvent event) {
                switch(updates) {
                    case 0:
                        // first delta
                        validateUpdates(event, firstUpdate, "FIRST");
                        updates++;
                        break;
                    case 1:
                        // combine delta
                        validateUpdates(event, firstUpdate, "FIRST");
                        validateUpdates(event, secondUpdate, "SECOND");
                        updates++;
                        break;
                    default:
                        break;
                }
            }
        });
    }
    if (registerInterestAll) {
        region.registerInterest("ALL_KEYS");
    }
    if (isCq) {
        CqAttributesFactory cqf = new CqAttributesFactory();
        CqListenerAdapter cqlist = new CqListenerAdapter() {

            @Override
            public void onEvent(CqEvent cqEvent) {
                Object key = cqEvent.getKey();
                if (LAST_KEY.equals(key)) {
                    lastKeyReceived = true;
                }
                logger.fine("CQ event received for (key, value): (" + key + ", " + cqEvent.getNewValue() + ")");
            }

            @Override
            public void onError(CqEvent cqEvent) {
                logger.fine("CQ error received for key: " + cqEvent.getKey());
            }
        };
        cqf.addCqListener(cqlist);
        CqAttributes cqa = cqf.create();
        for (int i = 0; i < cqQueryString.length; i++) {
            CqQuery cq = cache.getQueryService().newCq("Delta_Query_" + i, cqQueryString[i], cqa);
            cq.execute();
        }
    }
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) Properties(java.util.Properties) CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CqEvent(org.apache.geode.cache.query.CqEvent) CqAttributes(org.apache.geode.cache.query.CqAttributes) EntryEvent(org.apache.geode.cache.EntryEvent) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes)

Example 59 with CacheListenerAdapter

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

the class P2PDeltaPropagationDUnitTest method createServerCache.

public static void createServerCache(Boolean flag, DataPolicy policy, Scope scope, Boolean listener, Boolean interestPolicyAll, Integer port) throws Exception {
    P2PDeltaPropagationDUnitTest test = new P2PDeltaPropagationDUnitTest();
    Properties props = new Properties();
    if (!flag) {
        props.setProperty(DELTA_PROPAGATION, "false");
    }
    cache = test.createCache(props);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(scope);
    factory.setDataPolicy(policy);
    if (policy == DataPolicy.EMPTY && interestPolicyAll) {
        factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
    }
    if (listener) {
        factory.addCacheListener(new CacheListenerAdapter() {

            @SuppressWarnings("synthetic-access")
            public void afterUpdate(EntryEvent event) {
                numOfUpdates++;
                cache.getLoggerI18n().fine("afterUpdate(): numOfUpdates = " + numOfUpdates);
                cache.getLoggerI18n().fine("(key, val): " + event.getKey() + ", " + event.getNewValue());
                if (event.getOldValue() != null) {
                    if (event.getOldValue() == event.getNewValue()) {
                        check = Boolean.TRUE;
                    }
                }
                if (((EntryEventImpl) event).getDeltaBytes() != null) {
                    cache.getLoggerI18n().fine("delta bytes received. " + hasDeltaBytes);
                    assertTrue("No full value received for event " + event, ((EntryEventImpl) event).getNewValue() != null);
                    hasDeltaBytes++;
                } else {
                    cache.getLoggerI18n().fine("delta bytes not received.");
                }
            }
        });
    }
    Region region = cache.createRegion(REGION_NAME, factory.create());
    if (!policy.isReplicate()) {
        region.create("KEY", "KEY");
    }
    if (port != null) {
        CacheServer server1 = cache.addCacheServer();
        server1.setPort(port);
        server1.start();
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) EntryEvent(org.apache.geode.cache.EntryEvent) Region(org.apache.geode.cache.Region) CacheServer(org.apache.geode.cache.server.CacheServer) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes)

Example 60 with CacheListenerAdapter

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

the class HARegionJUnitTest method testEventIdSetForEvictDestroy.

/**
   * Test to verify event id exists when evict destroy happens.
   */
@Test
public void testEventIdSetForEvictDestroy() throws Exception {
    AttributesFactory factory = new AttributesFactory();
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterDestroy(EntryEvent event) {
            assertTrue("eventId has not been set for " + event, ((EntryEventImpl) event).getEventId() != null);
        }
    });
    EvictionAttributes evAttr = EvictionAttributes.createLRUEntryAttributes(1, EvictionAction.LOCAL_DESTROY);
    factory.setEvictionAttributes(evAttr);
    RegionAttributes attrs = factory.createRegionAttributes();
    Region region = cache.createVMRegion("TEST_REGION", attrs);
    region.put("key1", "value1");
    region.put("key2", "value2");
}
Also used : EvictionAttributes(org.apache.geode.cache.EvictionAttributes) 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) HARegion(org.apache.geode.internal.cache.HARegion) Region(org.apache.geode.cache.Region) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

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