Search in sources :

Example 31 with CacheListenerAdapter

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

the class HAClearDUnitTest method createClientCache.

public static void createClientCache(String hostName, Integer port1, Integer port2, Boolean listenerAttached, Boolean registerInterest) throws Exception {
    int PORT1 = port1.intValue();
    int PORT2 = port2.intValue();
    boolean isListenerAttached = listenerAttached.booleanValue();
    boolean isRegisterInterest = registerInterest.booleanValue();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new HAClearDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    ClientServerTestCase.configureConnectionPool(factory, hostName, new int[] { PORT1, PORT2 }, true, -1, 2, null);
    if (isListenerAttached) {
        factory.setCacheListener(new CacheListenerAdapter() {

            public void afterRegionClear(RegionEvent event) {
                LogWriterUtils.getLogWriter().info("-------> afterRegionClear received");
                synchronized (HAClearDUnitTest.class) {
                    gotClearCallback = true;
                    HAClearDUnitTest.class.notifyAll();
                }
            }

            public void afterRegionDestroy(RegionEvent event) {
                synchronized (HAClearDUnitTest.class) {
                    LogWriterUtils.getLogWriter().info("-------> afterRegionDestroy received");
                    gotDestroyRegionCallback = true;
                    HAClearDUnitTest.class.notifyAll();
                }
            }
        });
    }
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, attrs);
    Region region = cache.getRegion(Region.SEPARATOR + REGION_NAME);
    assertNotNull(region);
    if (isRegisterInterest) {
        region.registerInterest("ALL_KEYS", InterestResultPolicy.NONE);
    } else {
        // don't want data but do want events
        region.registerInterestRegex("*.", false, false);
    }
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) RegionAttributes(org.apache.geode.cache.RegionAttributes) LocalRegion(org.apache.geode.internal.cache.LocalRegion) Region(org.apache.geode.cache.Region) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent)

Example 32 with CacheListenerAdapter

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

the class EventIdOptimizationDUnitTest method createClientCache2.

/**
   * Creates the client cache2, connected to server3
   * 
   * @param port - bridgeserver port
   * @throws Exception - thrown if any problem occurs in setting up the client
   */
public static void createClientCache2(String hostName, Integer port) throws Exception {
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    new EventIdOptimizationDUnitTest().createCache(props);
    AttributesFactory factory = new AttributesFactory();
    ClientServerTestCase.configureConnectionPool(factory, hostName, port.intValue(), -1, true, -1, 2, null);
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.addCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            String key = (String) event.getKey();
            validateEventsAtReceivingClientListener(key);
        }

        public void afterDestroy(EntryEvent event) {
            String key = (String) event.getKey();
            validateEventsAtReceivingClientListener(key);
        }

        public void afterRegionDestroy(RegionEvent event) {
            validateEventsAtReceivingClientListener(" <destroyRegion Event> ");
        }

        public void afterRegionClear(RegionEvent event) {
            validateEventsAtReceivingClientListener(" <clearRegion Event> ");
        }
    });
    RegionAttributes attrs = factory.create();
    Region region = cache.createRegion(REGION_NAME, attrs);
    region.registerInterest("ALL_KEYS");
    for (int i = 0; i < eventIds.length; i++) {
        region = cache.createRegion(REGION_NAME + i, attrs);
        region.registerInterest("ALL_KEYS");
    }
    pool = (PoolImpl) PoolManager.find("testPool");
}
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) Region(org.apache.geode.cache.Region) Properties(java.util.Properties) RegionEvent(org.apache.geode.cache.RegionEvent)

Example 33 with CacheListenerAdapter

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

the class VerifyEventIDGenerationInP2PDUnitTest method createServerCache.

public static void createServerCache() throws Exception {
    new VerifyEventIDGenerationInP2PDUnitTest().createCache(new Properties());
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.DISTRIBUTED_ACK);
    factory.setMirrorType(MirrorType.NONE);
    factory.setCacheListener(new CacheListenerAdapter() {

        public void afterCreate(EntryEvent event) {
            if (!receiver) {
                vm0.invoke(() -> VerifyEventIDGenerationInP2PDUnitTest.setEventIDData(((EntryEventImpl) event).getEventId()));
            } else {
                testEventIDResult = ((EntryEventImpl) event).getEventId().equals(eventId);
            }
        }
    });
    RegionAttributes attrs = factory.create();
    cache.createRegion(REGION_NAME, 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) Properties(java.util.Properties)

Example 34 with CacheListenerAdapter

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

the class PRDeltaPropagationDUnitTest method createClientCache.

public static void createClientCache(Integer port1, Boolean subscriptionEnable, Boolean isEmpty, Boolean isCq) throws Exception {
    PRDeltaPropagationDUnitTest test = new PRDeltaPropagationDUnitTest();
    Properties props = new Properties();
    props.setProperty(MCAST_PORT, "0");
    props.setProperty(LOCATORS, "");
    test.createCache(props);
    lastKeyReceived = false;
    queryUpdateExecuted = false;
    queryDestroyExecuted = false;
    notADeltaInstanceObj = false;
    isFailed = false;
    procced = false;
    numValidCqEvents = 0;
    PoolImpl p = (PoolImpl) PoolManager.createFactory().addServer("localhost", port1).setSubscriptionEnabled(true).setSubscriptionRedundancy(0).setThreadLocalConnections(true).setMinConnections(6).setReadTimeout(20000).setPingInterval(10000).setRetryAttempts(5).create("PRDeltaPropagationDUnitTestPool");
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    factory.setConcurrencyChecksEnabled(true);
    if (isEmpty.booleanValue()) {
        factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
        factory.setDataPolicy(DataPolicy.EMPTY);
    }
    factory.setPoolName(p.getName());
    factory.setCloningEnabled(false);
    factory.addCacheListener(new CacheListenerAdapter() {

        @Override
        public void afterCreate(EntryEvent event) {
            if (LAST_KEY.equals(event.getKey())) {
                lastKeyReceived = true;
            }
        }
    });
    RegionAttributes attrs = factory.create();
    deltaPR = cache.createRegion(REGION_NAME, attrs);
    if (subscriptionEnable.booleanValue()) {
        deltaPR.registerInterest("ALL_KEYS");
    }
    pool = p;
    if (isCq.booleanValue()) {
        CqAttributesFactory cqf = new CqAttributesFactory();
        CqListenerAdapter cqlist = new CqListenerAdapter() {

            @Override
            @SuppressWarnings("synthetic-access")
            public void onEvent(CqEvent cqEvent) {
                if (LAST_KEY.equals(cqEvent.getKey().toString())) {
                    lastKeyReceived = true;
                } else if (!(cqEvent.getNewValue() instanceof Delta)) {
                    notADeltaInstanceObj = true;
                } else if (cqEvent.getQueryOperation().isUpdate() && cqEvent.getBaseOperation().isUpdate() && DELTA_KEY.equals(cqEvent.getKey().toString())) {
                    queryUpdateExecuted = true;
                } else if (cqEvent.getQueryOperation().isDestroy() && cqEvent.getBaseOperation().isUpdate() && DELTA_KEY.equals(cqEvent.getKey().toString())) {
                    queryDestroyExecuted = true;
                }
                if (forOldNewCQVarification) {
                    if (DELTA_KEY.equals(cqEvent.getKey().toString())) {
                        if (numValidCqEvents == 0 && ((DeltaTestImpl) cqEvent.getNewValue()).getIntVar() == 8) {
                            procced = true;
                        } else if (procced && numValidCqEvents == 1 && ((DeltaTestImpl) cqEvent.getNewValue()).getIntVar() == 10) {
                            // this tell us that every thing is fine
                            isFailed = true;
                        }
                    }
                }
                numValidCqEvents++;
            }
        };
        cqf.addCqListener(cqlist);
        CqAttributes cqa = cqf.create();
        CqQuery cq = cache.getQueryService().newCq("CQ_Delta", CQ, cqa);
        cq.execute();
    }
}
Also used : RegionAttributes(org.apache.geode.cache.RegionAttributes) DeltaTestImpl(org.apache.geode.DeltaTestImpl) ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) PoolImpl(org.apache.geode.cache.client.internal.PoolImpl) CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) CacheListenerAdapter(org.apache.geode.cache.util.CacheListenerAdapter) CqEvent(org.apache.geode.cache.query.CqEvent) Delta(org.apache.geode.Delta) 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 35 with CacheListenerAdapter

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

the class ClientToServerDeltaDUnitTest method createServerCache.

/*
   * create server cache
   */
public static Integer createServerCache(Boolean attachListener, Boolean isEmpty, Boolean clone, Boolean enableDelta) throws Exception {
    // for validation
    updates = 0;
    create = 0;
    firstUpdate = null;
    secondUpdate = null;
    error = false;
    Properties props = new Properties();
    props.setProperty(DELTA_PROPAGATION, enableDelta.toString());
    new ClientToServerDeltaDUnitTest().createCache(props);
    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.REPLICATE);
    }
    factory.setCloningEnabled(clone);
    RegionAttributes attrs = factory.create();
    region = cache.createRegion(REGION_NAME, attrs);
    AttributesMutator am = region.getAttributesMutator();
    if (attachListener) {
        am.addCacheListener(new CacheListenerAdapter() {

            @Override
            public void afterCreate(EntryEvent event) {
                create++;
            }

            @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;
                }
            }
        });
    } else if (!isEmpty) {
        am.addCacheListener(new CacheListenerAdapter() {

            @Override
            public void afterCreate(EntryEvent event) {
                switch(create) {
                    case 1:
                        validateUpdates(event, firstUpdate, "FIRST");
                        create++;
                        break;
                    case 2:
                        validateUpdates(event, secondUpdate, "SECOND");
                        create++;
                        break;
                    default:
                        create++;
                        break;
                }
            }
        });
    }
    CacheServer server = cache.addCacheServer();
    int port = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
    server.setPort(port);
    // ensures updates to be sent instead of invalidations
    server.setNotifyBySubscription(true);
    server.start();
    return new Integer(server.getPort());
}
Also used : AttributesFactory(org.apache.geode.cache.AttributesFactory) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) 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) Properties(java.util.Properties) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) AttributesMutator(org.apache.geode.cache.AttributesMutator)

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