Search in sources :

Example 26 with CqAttributesFactory

use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.

the class CqQueryDUnitTest method createAndExecCQNoName.

/* Register CQs with no name, execute, and close */
public void createAndExecCQNoName(VM vm, final String queryStr) {
    vm.invoke(new CacheSerializableRunnable("Create CQ with no name:") {

        public void run2() throws CacheException {
            // pause(60 * 1000);
            LogWriterUtils.getLogWriter().info("### DEBUG CREATE CQ START ####");
            // pause(20 * 1000);
            LogWriterUtils.getLogWriter().info("### Create CQ with no name. ###");
            // Get CQ Service.
            QueryService cqService = null;
            CqQuery cq1 = null;
            String cqName = null;
            try {
                cqService = getCache().getQueryService();
            } catch (Exception cqe) {
                cqe.printStackTrace();
                fail("Failed to getCQService.");
            }
            SelectResults cqResults = null;
            for (int i = 0; i < 20; ++i) {
                // Create CQ Attributes.
                CqAttributesFactory cqf = new CqAttributesFactory();
                CqListener[] cqListeners = { new CqQueryTestListener(LogWriterUtils.getLogWriter()) };
                cqf.initCqListeners(cqListeners);
                CqAttributes cqa = cqf.create();
                // Create CQ with no name and execute with initial results.
                try {
                    cq1 = cqService.newCq(queryStr, cqa);
                    ((CqQueryTestListener) cqListeners[0]).cqName = cq1.getName();
                } catch (Exception ex) {
                    LogWriterUtils.getLogWriter().info("CQService is :" + cqService);
                    ex.printStackTrace();
                    fail("Failed to create CQ with no name" + " . " + ex.getMessage());
                }
                if (cq1 == null) {
                    LogWriterUtils.getLogWriter().info("Failed to get CqQuery object for CQ with no name.");
                } else {
                    cqName = cq1.getName();
                    LogWriterUtils.getLogWriter().info("Created CQ with no name, generated CQ name: " + cqName + " CQ state:" + cq1.getState());
                    assertTrue("Create CQ with no name illegal state", cq1.getState().isStopped());
                }
                if (i % 2 == 0) {
                    try {
                        cqResults = cq1.executeWithInitialResults();
                    } catch (Exception ex) {
                        LogWriterUtils.getLogWriter().info("CqService is :" + cqService);
                        ex.printStackTrace();
                        fail("Failed to execute CQ with initial results, cq name: " + cqName + " . " + ex.getMessage());
                    }
                    LogWriterUtils.getLogWriter().info("initial result size = " + cqResults.size());
                    LogWriterUtils.getLogWriter().info("CQ state after execute with initial results = " + cq1.getState());
                    assertTrue("executeWithInitialResults() state mismatch", cq1.getState().isRunning());
                } else {
                    try {
                        cq1.execute();
                    } catch (Exception ex) {
                        LogWriterUtils.getLogWriter().info("CQService is :" + cqService);
                        ex.printStackTrace();
                        fail("Failed to execute CQ " + cqName + " . " + ex.getMessage());
                    }
                    LogWriterUtils.getLogWriter().info("CQ state after execute = " + cq1.getState());
                    assertTrue("execute() state mismatch", cq1.getState().isRunning());
                }
                // Close the CQ
                try {
                    cq1.close();
                } catch (Exception ex) {
                    LogWriterUtils.getLogWriter().info("CqService is :" + cqService, ex);
                    fail("Failed to close CQ " + cqName + " . " + ex.getMessage());
                }
                assertTrue("closeCq() state mismatch", cq1.getState().isClosed());
            }
        }
    });
}
Also used : SelectResults(org.apache.geode.cache.query.SelectResults) CacheSerializableRunnable(org.apache.geode.cache30.CacheSerializableRunnable) CacheException(org.apache.geode.cache.CacheException) DefaultQueryService(org.apache.geode.cache.query.internal.DefaultQueryService) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery) CqExistsException(org.apache.geode.cache.query.CqExistsException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqClosedException(org.apache.geode.cache.query.CqClosedException) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Example 27 with CqAttributesFactory

use of org.apache.geode.cache.query.CqAttributesFactory 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 28 with CqAttributesFactory

use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.

the class DeltaToRegionRelationCQRegistrationDUnitTest method registerCq.

/*
   * register cq
   */
public static void registerCq(String name, String Cquery, Boolean cqWithIR) {
    QueryService cqService = null;
    // get cq service
    try {
        cqService = cache.getQueryService();
    } catch (Exception cqe) {
        cqe.printStackTrace();
        fail("Failed to getCqService.");
    }
    // Create CQ Attributes.
    // do not attach any listiner lets see its response
    CqAttributesFactory cqf = new CqAttributesFactory();
    CqAttributes cqa = cqf.create();
    // Create and Execute CQ.
    try {
        CqQuery cq1 = cqService.newCq(name, Cquery, cqa);
        assertTrue("newCq() state mismatch", cq1.getState().isStopped());
        if (cqWithIR)
            cq1.executeWithInitialResults();
        else
            cq1.execute();
    } catch (Exception ex) {
        fail("Failed to create CQ " + cqName1, ex);
    }
    CqQuery cQuery = cqService.getCq(name);
    if (cQuery == null) {
        fail("Failed to get CqQuery for CQ : " + cqName1);
    }
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 29 with CqAttributesFactory

use of org.apache.geode.cache.query.CqAttributesFactory in project geode by apache.

the class DurableClientTestCase method createCq.

protected CqQuery createCq(String cqName, String cqQuery, boolean durable) throws CqException, CqExistsException {
    QueryService qs = CacheServerTestUtil.getCache().getQueryService();
    CqAttributesFactory cqf = new CqAttributesFactory();
    CqListener[] cqListeners = { new CacheServerTestUtil.ControlCqListener() };
    cqf.initCqListeners(cqListeners);
    CqAttributes cqa = cqf.create();
    return qs.newCq(cqName, cqQuery, cqa, durable);
}
Also used : QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqListener(org.apache.geode.cache.query.CqListener) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory)

Example 30 with CqAttributesFactory

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

Aggregations

CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)36 CqAttributes (org.apache.geode.cache.query.CqAttributes)31 CqQuery (org.apache.geode.cache.query.CqQuery)30 QueryService (org.apache.geode.cache.query.QueryService)29 CacheException (org.apache.geode.cache.CacheException)16 Test (org.junit.Test)16 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)15 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)15 Region (org.apache.geode.cache.Region)11 CqEvent (org.apache.geode.cache.query.CqEvent)11 CqExistsException (org.apache.geode.cache.query.CqExistsException)10 CqListener (org.apache.geode.cache.query.CqListener)10 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)8 Host (org.apache.geode.test.dunit.Host)8 VM (org.apache.geode.test.dunit.VM)8 IOException (java.io.IOException)7 CqException (org.apache.geode.cache.query.CqException)7 SelectResults (org.apache.geode.cache.query.SelectResults)7 DefaultQueryService (org.apache.geode.cache.query.internal.DefaultQueryService)7 ClientSubscriptionTest (org.apache.geode.test.junit.categories.ClientSubscriptionTest)7