Search in sources :

Example 16 with CqListener

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

the class CqQueryDUnitTest method validateCQError.

private void validateCQError(VM vm, final String cqName, final int numError) {
    vm.invoke(new CacheSerializableRunnable("Validate CQs") {

        public void run2() throws CacheException {
            LogWriterUtils.getLogWriter().info("### Validating CQ. ### " + cqName);
            // Get CQ Service.
            QueryService cqService = null;
            try {
                cqService = getCache().getQueryService();
            } catch (Exception cqe) {
                cqe.printStackTrace();
                fail("Failed to getCQService.");
            }
            CqQuery cQuery = cqService.getCq(cqName);
            if (cQuery == null) {
                fail("Failed to get CqQuery for CQ : " + cqName);
            }
            CqAttributes cqAttr = cQuery.getCqAttributes();
            CqListener cqListener = cqAttr.getCqListener();
            CqQueryTestListener listener = (CqQueryTestListener) cqListener;
            listener.printInfo(false);
            // Check for totalEvents count.
            if (numError != noTest) {
                // Result size validation.
                listener.printInfo(true);
                assertEquals("Total Event Count mismatch", numError, listener.getErrorEventCount());
            }
        }
    });
}
Also used : 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) CqListener(org.apache.geode.cache.query.CqListener) 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 17 with CqListener

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

the class CqQueryDUnitTest method mutateCQAttributes.

// Exercise CQ attributes mutator functions
private void mutateCQAttributes(VM vm, final String cqName, final int mutator_function) throws Exception {
    vm.invoke(new CacheSerializableRunnable("Stop CQ :" + cqName) {

        public void run2() throws CacheException {
            CqQuery cq1 = null;
            LogWriterUtils.getLogWriter().info("### CQ attributes mutator for ###" + cqName);
            // Get CQ Service.
            QueryService cqService = null;
            try {
                cqService = getCache().getQueryService();
            } catch (Exception cqe) {
                cqe.printStackTrace();
                fail("Failed to getCQService.");
            }
            // Get CQ.
            try {
                cq1 = cqService.getCq(cqName);
            } catch (Exception ex) {
                ex.printStackTrace();
                fail("Failed to get CQ " + cqName + " . " + ex.getMessage());
            }
            CqAttributesMutator cqAttrMutator = cq1.getCqAttributesMutator();
            CqAttributes cqAttr = cq1.getCqAttributes();
            CqListener[] cqListeners;
            switch(mutator_function) {
                case CREATE:
                    // Reinitialize with 2 CQ Listeners
                    CqListener[] cqListenersArray = { new CqQueryTestListener(getCache().getLogger()), new CqQueryTestListener(getCache().getLogger()) };
                    cqAttrMutator.initCqListeners(cqListenersArray);
                    cqListeners = cqAttr.getCqListeners();
                    assertEquals("CqListener count mismatch", cqListeners.length, 2);
                    break;
                case UPDATE:
                    // Add 2 new CQ Listeners
                    CqListener newListener1 = new CqQueryTestListener(getCache().getLogger());
                    CqListener newListener2 = new CqQueryTestListener(getCache().getLogger());
                    cqAttrMutator.addCqListener(newListener1);
                    cqAttrMutator.addCqListener(newListener2);
                    cqListeners = cqAttr.getCqListeners();
                    assertEquals("CqListener count mismatch", cqListeners.length, 3);
                    break;
                case DESTROY:
                    cqListeners = cqAttr.getCqListeners();
                    cqAttrMutator.removeCqListener(cqListeners[0]);
                    cqListeners = cqAttr.getCqListeners();
                    assertEquals("CqListener count mismatch", cqListeners.length, 2);
                    // Remove a listener and validate
                    cqAttrMutator.removeCqListener(cqListeners[0]);
                    cqListeners = cqAttr.getCqListeners();
                    assertEquals("CqListener count mismatch", cqListeners.length, 1);
                    break;
            }
        }
    });
}
Also used : 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) CqListener(org.apache.geode.cache.query.CqListener) CqQuery(org.apache.geode.cache.query.CqQuery) CqAttributesMutator(org.apache.geode.cache.query.CqAttributesMutator) 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 18 with CqListener

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

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

the class CQListGIIDUnitTest method createCQ.

/* Register CQs */
public static void createCQ(String cqName, String queryStr) {
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("### Create CQ. ###" + cqName);
    // Get CQ Service.
    QueryService cqService = null;
    try {
        cqService = cache.getQueryService();
    } catch (Exception cqe) {
        Assert.fail("Failed to getCQService.", cqe);
    }
    // Create CQ Attributes.
    CqAttributesFactory cqf = new CqAttributesFactory();
    CqListener[] cqListeners = { new CqQueryTestListener(org.apache.geode.test.dunit.LogWriterUtils.getLogWriter()) };
    ((CqQueryTestListener) cqListeners[0]).cqName = cqName;
    cqf.initCqListeners(cqListeners);
    CqAttributes cqa = cqf.create();
    // Create CQ.
    try {
        CqQuery cq1 = cqService.newCq(cqName, queryStr, cqa);
        assertTrue("newCq() state mismatch", cq1.getState().isStopped());
    } catch (Exception ex) {
        fail("Failed to create CQ " + cqName + " . ", ex);
    }
}
Also used : CqQueryTestListener(org.apache.geode.cache.query.cq.dunit.CqQueryTestListener) 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) CqQuery(org.apache.geode.cache.query.CqQuery) IOException(java.io.IOException) CacheException(org.apache.geode.cache.CacheException)

Example 20 with CqListener

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

the class CqServiceImpl method invokeListeners.

private void invokeListeners(String cqName, ClientCQImpl cQuery, CqEventImpl cqEvent, Object[] fullValue) {
    if (!cQuery.isRunning() || cQuery.getCqAttributes() == null) {
        return;
    }
    // invoke CQ Listeners.
    CqListener[] cqListeners = cQuery.getCqAttributes().getCqListeners();
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        logger.debug("Invoking CQ listeners for {}, number of listeners : {} cqEvent : {}", cqName, cqListeners.length, cqEvent);
    }
    for (int lCnt = 0; lCnt < cqListeners.length; lCnt++) {
        try {
            // by the CqAttributeMutator.
            if (cqListeners[lCnt] != null) {
                cQuery.getVsdStats().incNumCqListenerInvocations();
                try {
                    if (cqEvent.getThrowable() != null) {
                        cqListeners[lCnt].onError(cqEvent);
                    } else {
                        cqListeners[lCnt].onEvent(cqEvent);
                    }
                } catch (InvalidDeltaException ide) {
                    if (isDebugEnabled) {
                        logger.debug("CqService.dispatchCqListeners(): Requesting full value...");
                    }
                    Part result = (Part) GetEventValueOp.executeOnPrimary(cqEvent.getQueueManager().getPool(), cqEvent.getEventID(), null);
                    Object newVal = result.getObject();
                    if (result == null || newVal == null) {
                        if (!cache.getCancelCriterion().isCancelInProgress()) {
                            Exception ex = new Exception("Failed to retrieve full value from server for eventID " + cqEvent.getEventID());
                            logger.warn(LocalizedMessage.create(LocalizedStrings.CqService_EXCEPTION_IN_THE_CQLISTENER_OF_THE_CQ_CQNAME_0_ERROR__1, new Object[] { cqName, ex.getMessage() }));
                            if (isDebugEnabled) {
                                logger.debug(ex.getMessage(), ex);
                            }
                        }
                    } else {
                        this.cache.getCachePerfStats().incDeltaFullValuesRequested();
                        cqEvent = new CqEventImpl(cQuery, cqEvent.getBaseOperation(), cqEvent.getQueryOperation(), cqEvent.getKey(), newVal, cqEvent.getDeltaValue(), cqEvent.getQueueManager(), cqEvent.getEventID());
                        if (cqEvent.getThrowable() != null) {
                            cqListeners[lCnt].onError(cqEvent);
                        } else {
                            cqListeners[lCnt].onEvent(cqEvent);
                        }
                        if (fullValue != null) {
                            fullValue[0] = newVal;
                        }
                    }
                }
            }
        // Handle client side exceptions.
        } catch (Exception ex) {
            if (!cache.getCancelCriterion().isCancelInProgress()) {
                logger.warn(LocalizedMessage.create(LocalizedStrings.CqService_EXCEPTION_IN_THE_CQLISTENER_OF_THE_CQ_CQNAME_0_ERROR__1, new Object[] { cqName, ex.getMessage() }));
                if (isDebugEnabled) {
                    logger.debug(ex.getMessage(), ex);
                }
            }
        } catch (VirtualMachineError err) {
            SystemFailure.initiateFailure(err);
            // now, so don't let this thread continue.
            throw err;
        } catch (Throwable t) {
            // Whenever you catch Error or Throwable, you must also
            // catch VirtualMachineError (see above). However, there is
            // _still_ a possibility that you are dealing with a cascading
            // error condition, so you also need to check to see if the JVM
            // is still usable:
            SystemFailure.checkFailure();
            logger.warn(LocalizedMessage.create(LocalizedStrings.CqService_RUNTIME_EXCEPTION_IN_THE_CQLISTENER_OF_THE_CQ_CQNAME_0_ERROR__1, new Object[] { cqName, t.getLocalizedMessage() }));
            if (isDebugEnabled) {
                logger.debug(t.getMessage(), t);
            }
        }
    }
}
Also used : InvalidDeltaException(org.apache.geode.InvalidDeltaException) CqListener(org.apache.geode.cache.query.CqListener) Part(org.apache.geode.internal.cache.tier.sockets.Part) TimeoutException(org.apache.geode.cache.TimeoutException) CqExistsException(org.apache.geode.cache.query.CqExistsException) CqException(org.apache.geode.cache.query.CqException) QueryInvalidException(org.apache.geode.cache.query.QueryInvalidException) InvalidDeltaException(org.apache.geode.InvalidDeltaException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CacheLoaderException(org.apache.geode.cache.CacheLoaderException) CqClosedException(org.apache.geode.cache.query.CqClosedException) QueryException(org.apache.geode.cache.query.QueryException)

Aggregations

CqListener (org.apache.geode.cache.query.CqListener)23 QueryService (org.apache.geode.cache.query.QueryService)15 CqAttributes (org.apache.geode.cache.query.CqAttributes)14 CqQuery (org.apache.geode.cache.query.CqQuery)12 CqAttributesFactory (org.apache.geode.cache.query.CqAttributesFactory)10 CacheException (org.apache.geode.cache.CacheException)8 RegionNotFoundException (org.apache.geode.cache.query.RegionNotFoundException)8 CqClosedException (org.apache.geode.cache.query.CqClosedException)7 CqQueryTestListener (org.apache.geode.cache.query.cq.dunit.CqQueryTestListener)7 CqExistsException (org.apache.geode.cache.query.CqExistsException)6 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)6 IOException (java.io.IOException)5 CqException (org.apache.geode.cache.query.CqException)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 Region (org.apache.geode.cache.Region)4 CqEvent (org.apache.geode.cache.query.CqEvent)4 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)4 List (java.util.List)3 SelectResults (org.apache.geode.cache.query.SelectResults)3