Search in sources :

Example 1 with CqListenerAdapter

use of org.apache.geode.cache.util.CqListenerAdapter 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 2 with CqListenerAdapter

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

the class DeltaPropagationWithCQDUnitTest method registerCQs.

public static void registerCQs(Integer numOfListeners, String name) throws Exception {
    QueryService qs = pool.getQueryService();
    CqAttributesFactory caf = new CqAttributesFactory();
    CqListenerAdapter[] cqListeners = new CqListenerAdapter[numOfListeners];
    for (int i = 0; i < numOfListeners; i++) {
        cqListeners[i] = new CqListenerAdapter() {

            @Override
            public void onEvent(CqEvent event) {
                System.out.println("CqListener.onEvent invoked.  Event=" + event);
                if (event.getDeltaValue() != null) {
                    deltasFound++;
                }
                // The first CQ event dispatched with a delta will not have a newValue.
                // Attempting to access the newValue will cause an exception to be
                // thrown, exiting this listener and causing the full value to be
                // read from the server. The listener is then invoked a second time
                // and getNewValue will succeed
                event.getNewValue();
                if (event.getDeltaValue() != null) {
                    // if there's a newValue we should ignore the delta bytes
                    deltasFound--;
                }
                System.out.println("deltasFound=" + deltasFound);
                cqEvents++;
                System.out.println("cqEvents is now " + cqEvents);
            }

            @Override
            public void onError(CqEvent event) {
                System.out.println("CqListener.onError invoked.  Event=" + event);
                if (event.getDeltaValue() != null) {
                    deltasFound++;
                }
                event.getNewValue();
                if (event.getDeltaValue() != null) {
                    deltasFound--;
                }
                System.out.println("deltasFound=" + deltasFound);
                cqErrors++;
                System.out.println("cqErrors is now " + cqErrors);
            }
        };
        caf.addCqListener(cqListeners[i]);
    }
    CqQuery cQuery = qs.newCq(name, CQ1, caf.create());
    cQuery.execute();
    if (qs.getCq(name) == null) {
        fail("Failed to get CQ " + name);
    }
}
Also used : CqEvent(org.apache.geode.cache.query.CqEvent) QueryService(org.apache.geode.cache.query.QueryService) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter)

Example 3 with CqListenerAdapter

use of org.apache.geode.cache.util.CqListenerAdapter 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 4 with CqListenerAdapter

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

the class ClientSnapshotDUnitTest method testImport.

@Test
public void testImport() throws Exception {
    int count = 1000;
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "clienttest " + i));
    }
    SerializableCallable export = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            File f = new File(getDiskDirs()[0], "client-import.snapshot");
            Region<Integer, MyObject> r = getCache().getRegion("clienttest");
            r.getSnapshotService().save(f, SnapshotFormat.GEMFIRE);
            return f;
        }
    };
    Host.getHost(0).getVM(3).invoke(export);
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "XXX"));
    }
    SerializableCallable imp = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            final AtomicBoolean cqtest = new AtomicBoolean(false);
            CqAttributesFactory af = new CqAttributesFactory();
            af.addCqListener(new CqListenerAdapter() {

                @Override
                public void onEvent(CqEvent aCqEvent) {
                    cqtest.set(true);
                }
            });
            Region<Integer, MyObject> r = getCache().getRegion("clienttest");
            CqQuery cq = r.getRegionService().getQueryService().newCq("SELECT * FROM /clienttest", af.create());
            cq.execute();
            File f = new File(getDiskDirs()[0], "client-import.snapshot");
            r.getSnapshotService().load(f, SnapshotFormat.GEMFIRE);
            return cqtest.get();
        }
    };
    // add callbacks
    region.getAttributesMutator().setCacheWriter(new CacheWriterAdapter<Integer, MyObject>() {

        @Override
        public void beforeUpdate(EntryEvent<Integer, MyObject> event) {
            fail("CacheWriter invoked during import");
        }
    });
    final AtomicBoolean cltest = new AtomicBoolean(false);
    region.getAttributesMutator().addCacheListener(new CacheListenerAdapter<Integer, MyObject>() {

        @Override
        public void afterUpdate(EntryEvent<Integer, MyObject> event) {
            cltest.set(true);
        }
    });
    boolean cqtest = (Boolean) Host.getHost(0).getVM(3).invoke(imp);
    assertEquals("CacheListener invoked during import", false, cltest.get());
    assertEquals("CqListener invoked during import", false, cqtest);
    for (MyObject obj : region.values()) {
        assertTrue(obj.getF2().startsWith("clienttest"));
    }
}
Also used : CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CqEvent(org.apache.geode.cache.query.CqEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) MyObject(com.examples.snapshot.MyObject) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) File(java.io.File) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 5 with CqListenerAdapter

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

the class ClientSnapshotDUnitTest method testClientCallbacks.

@Test
public void testClientCallbacks() throws Exception {
    int count = 1000;
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "clienttest " + i));
    }
    File f = new File(getDiskDirs()[0], "client-callback.snapshot");
    region.getSnapshotService().save(f, SnapshotFormat.GEMFIRE);
    for (int i = 0; i < count; i++) {
        region.put(i, new MyObject(i, "XXX"));
    }
    SerializableCallable callbacks = new SerializableCallable() {

        @Override
        public Object call() throws Exception {
            Region<Integer, MyObject> r = getCache().getRegion("clienttest");
            r.registerInterestRegex(".*");
            r.getAttributesMutator().setCacheWriter(new CacheWriterAdapter<Integer, MyObject>() {

                @Override
                public void beforeUpdate(EntryEvent<Integer, MyObject> event) {
                    fail("CacheWriter invoked during import");
                }
            });
            r.getAttributesMutator().addCacheListener(new CacheListenerAdapter<Integer, MyObject>() {

                @Override
                public void afterUpdate(EntryEvent<Integer, MyObject> event) {
                    fail("CacheListener was invoked during import");
                }
            });
            final AtomicBoolean cqtest = new AtomicBoolean(false);
            CqAttributesFactory af = new CqAttributesFactory();
            af.addCqListener(new CqListenerAdapter() {

                @Override
                public void onEvent(CqEvent aCqEvent) {
                    fail("Cq was invoked during import");
                }
            });
            CqQuery cq = r.getRegionService().getQueryService().newCq("SELECT * FROM /clienttest", af.create());
            cq.execute();
            return null;
        }
    };
    Host.getHost(0).getVM(3).invoke(callbacks);
    region.getSnapshotService().load(f, SnapshotFormat.GEMFIRE);
}
Also used : CqListenerAdapter(org.apache.geode.cache.util.CqListenerAdapter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CqEvent(org.apache.geode.cache.query.CqEvent) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) MyObject(com.examples.snapshot.MyObject) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) File(java.io.File) ClientSubscriptionTest(org.apache.geode.test.junit.categories.ClientSubscriptionTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Aggregations

CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)5 CqEvent (org.apache.geode.cache.query.CqEvent)5 CqQuery (org.apache.geode.cache.query.CqQuery)5 CqListenerAdapter (org.apache.geode.cache.util.CqListenerAdapter)5 MyObject (com.examples.snapshot.MyObject)2 File (java.io.File)2 Properties (java.util.Properties)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AttributesFactory (org.apache.geode.cache.AttributesFactory)2 EntryEvent (org.apache.geode.cache.EntryEvent)2 RegionAttributes (org.apache.geode.cache.RegionAttributes)2 SubscriptionAttributes (org.apache.geode.cache.SubscriptionAttributes)2 CqAttributes (org.apache.geode.cache.query.CqAttributes)2 CacheListenerAdapter (org.apache.geode.cache.util.CacheListenerAdapter)2 SerializableCallable (org.apache.geode.test.dunit.SerializableCallable)2 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)2 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)2 Test (org.junit.Test)2 Delta (org.apache.geode.Delta)1 DeltaTestImpl (org.apache.geode.DeltaTestImpl)1