Search in sources :

Example 21 with CqListener

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

the class ClientCQImpl method close.

@Override
public void close(boolean sendRequestToServer) throws CqClosedException, CqException {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        logger.debug("Started closing CQ CqName: {} SendRequestToServer: {}", cqName, sendRequestToServer);
    }
    // Synchronize with stop and execute CQ commands
    synchronized (this.cqState) {
        // Check if the cq is already closed.
        if (this.isClosed()) {
            // throw new CqClosedException("CQ is already closed, CqName : " + this.cqName);
            if (isDebugEnabled) {
                logger.debug("CQ is already closed, CqName: {}", this.cqName);
            }
            return;
        }
        int stateBeforeClosing = this.cqState.getState();
        this.cqState.setState(CqStateImpl.CLOSING);
        boolean isClosed = false;
        // Client Close. Proxy is null in case of server.
        // Check if this has been sent to server, if so send
        // Close request to server.
        Exception exception = null;
        if (this.cqProxy != null && sendRequestToServer) {
            try {
                if (this.proxyCache != null) {
                    if (this.proxyCache.isClosed()) {
                        throw new CacheClosedException("Cache is closed for this user.");
                    }
                    UserAttributes.userAttributes.set(this.proxyCache.getUserAttributes());
                }
                cqProxy.close(this);
                isClosed = true;
            } catch (CancelException e) {
                throw e;
            } catch (Exception ex) {
                if (shutdownInProgress()) {
                    return;
                }
                exception = ex;
            } finally {
                UserAttributes.userAttributes.set(null);
            }
        }
        // Cleanup the resource used by cq.
        this.removeFromCqMap();
        if (cqProxy == null || !sendRequestToServer || isClosed) {
            // Stat update.
            if (stateBeforeClosing == CqStateImpl.RUNNING) {
                cqService.stats().decCqsActive();
            } else if (stateBeforeClosing == CqStateImpl.STOPPED) {
                cqService.stats().decCqsStopped();
            }
            // Set the state to close, and update stats
            this.cqState.setState(CqStateImpl.CLOSED);
            cqService.stats().incCqsClosed();
            cqService.stats().decCqsOnClient();
            if (this.stats != null)
                this.stats.close();
        } else {
            if (shutdownInProgress()) {
                return;
            }
            // Hasn't able to send close request to any server.
            if (exception != null) {
                throw new CqException(LocalizedStrings.CqQueryImpl_FAILED_TO_CLOSE_THE_CQ_CQNAME_0_ERROR_FROM_LAST_ENDPOINT_1.toLocalizedString(this.cqName, exception.getLocalizedMessage()), exception.getCause());
            } else {
                throw new CqException(LocalizedStrings.CqQueryImpl_FAILED_TO_CLOSE_THE_CQ_CQNAME_0_THE_SERVER_ENDPOINTS_ON_WHICH_THIS_CQ_WAS_REGISTERED_WERE_NOT_FOUND.toLocalizedString(this.cqName));
            }
        }
    }
    // Invoke close on Listeners if any.
    if (this.cqAttributes != null) {
        CqListener[] cqListeners = this.getCqAttributes().getCqListeners();
        if (cqListeners != null) {
            if (isDebugEnabled) {
                logger.debug("Invoking CqListeners close() api for the CQ, CqName: {} Number of CqListeners: {}", cqName, cqListeners.length);
            }
            for (int lCnt = 0; lCnt < cqListeners.length; lCnt++) {
                try {
                    cqListeners[lCnt].close();
                // Handle client side exceptions.
                } catch (Exception ex) {
                    logger.warn(LocalizedMessage.create(LocalizedStrings.CqQueryImpl_EXCEPTION_OCCOURED_IN_THE_CQLISTENER_OF_THE_CQ_CQNAME_0_ERROR_1, new Object[] { cqName, ex.getLocalizedMessage() }));
                    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.CqQueryImpl_RUNTIMEEXCEPTION_OCCOURED_IN_THE_CQLISTENER_OF_THE_CQ_CQNAME_0_ERROR_1, new Object[] { cqName, t.getLocalizedMessage() }));
                    if (isDebugEnabled) {
                        logger.debug(t.getMessage(), t);
                    }
                }
            }
        }
    }
    if (isDebugEnabled) {
        logger.debug("Successfully closed the CQ. {}", cqName);
    }
}
Also used : CqException(org.apache.geode.cache.query.CqException) CqListener(org.apache.geode.cache.query.CqListener) CacheClosedException(org.apache.geode.cache.CacheClosedException) CancelException(org.apache.geode.CancelException) RegionNotFoundException(org.apache.geode.cache.query.RegionNotFoundException) CqException(org.apache.geode.cache.query.CqException) CacheClosedException(org.apache.geode.cache.CacheClosedException) CancelException(org.apache.geode.CancelException) GemFireSecurityException(org.apache.geode.security.GemFireSecurityException) CqClosedException(org.apache.geode.cache.query.CqClosedException)

Example 22 with CqListener

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

the class ClientCQPostAuthorizationDUnitTest method createCQ.

private void createCQ(final int num) throws CqException, CqExistsException {
    for (int i = 0; i < num; i++) {
        QueryService cqService = getProxyCaches(i).getQueryService();
        String cqName = "CQ_" + i;
        String queryStr = cqNameToQueryStrings.get(cqName) + getProxyCaches(i).getRegion(REGION_NAME).getFullPath();
        // Create CQ Attributes.
        CqAttributesFactory cqf = new CqAttributesFactory();
        CqListener[] cqListeners = { new CqQueryTestListener(getLogWriter()) };
        ((CqQueryTestListener) cqListeners[0]).cqName = cqName;
        cqf.initCqListeners(cqListeners);
        CqAttributes cqa = cqf.create();
        // Create CQ.
        CqQuery cq1 = cqService.newCq(cqName, queryStr, cqa);
        assertTrue("newCq() state mismatch", cq1.getState().isStopped());
    }
}
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) InternalCqQuery(org.apache.geode.cache.query.internal.cq.InternalCqQuery) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 23 with CqListener

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

the class MultiUserDurableCQAuthzDUnitTest method checkCQListeners.

private void checkCQListeners(final int numOfUsers, final boolean[] expectedListenerInvocation, final int createEventsSize, final int updateEventsSize) {
    for (int i = 0; i < numOfUsers; i++) {
        String cqName = "CQ_" + i;
        QueryService qService = getProxyCaches(i).getQueryService();
        ClientCQImpl cqQuery = (ClientCQImpl) qService.getCq(cqName);
        if (expectedListenerInvocation[i]) {
            for (CqListener listener : cqQuery.getCqListeners()) {
                assertEquals(createEventsSize, ((CqQueryTestListener) listener).getCreateEventCount());
                assertEquals(updateEventsSize, ((CqQueryTestListener) listener).getUpdateEventCount());
            }
        } else {
            for (CqListener listener : cqQuery.getCqListeners()) {
                assertEquals(0, ((CqQueryTestListener) listener).getTotalEventCount());
            }
        }
    }
}
Also used : ClientCQImpl(org.apache.geode.cache.query.internal.cq.ClientCQImpl) QueryService(org.apache.geode.cache.query.QueryService) CqListener(org.apache.geode.cache.query.CqListener)

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