Search in sources :

Example 1 with CqListener

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

the class CqQueryDUnitTest method clearCQListenerEvents.

public void clearCQListenerEvents(VM vm, final String cqName) {
    vm.invoke(new CacheSerializableRunnable("validate cq count") {

        public void run2() throws CacheException {
            // 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.getEventHistory();
        }
    });
}
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 2 with CqListener

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

the class ClientCQPostAuthorizationDUnitTest method checkCQListeners.

private boolean checkCQListeners(final int numOfUsers, final boolean[] expectedListenerInvocation, final int createEventsSize, final int updateEventsSize, final boolean closeCache) {
    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());
            }
        }
        if (closeCache) {
            getProxyCaches(i).close();
        }
    }
    return true;
}
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)

Example 3 with CqListener

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

the class MultiUserDurableCQAuthzDUnitTest method createCQ.

private void createCQ(final int num, final boolean isDurable) 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(regionName).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, isDurable);
        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) CqQuery(org.apache.geode.cache.query.CqQuery)

Example 4 with CqListener

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

the class ClientQueryAuthDUnitTest method testCQ.

@Test
public void testCQ() {
    String query = "select * from /AuthRegion";
    client1.invoke(() -> {
        ClientCache cache = createClientCache("stranger", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributes cqa = new CqAttributesFactory().create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        assertNotAuthorized(() -> cq.executeWithInitialResults(), "DATA:READ:AuthRegion");
        assertNotAuthorized(() -> cq.execute(), "DATA:READ:AuthRegion");
        assertNotAuthorized(() -> cq.close(), "DATA:MANAGE");
    });
    client2.invoke(() -> {
        ClientCache cache = createClientCache("authRegionReader", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributes cqa = new CqAttributesFactory().create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        cq.execute();
        assertNotAuthorized(() -> cq.stop(), "DATA:MANAGE");
        assertNotAuthorized(() -> qs.getAllDurableCqsFromServer(), "CLUSTER:READ");
    });
    client3.invoke(() -> {
        ClientCache cache = createClientCache("super-user", "1234567", server.getPort());
        Region region = createProxyRegion(cache, REGION_NAME);
        Pool pool = PoolManager.find(region);
        QueryService qs = pool.getQueryService();
        CqAttributesFactory factory = new CqAttributesFactory();
        factory.addCqListener(new CqListener() {

            @Override
            public void onEvent(final CqEvent aCqEvent) {
                System.out.println(aCqEvent);
            }

            @Override
            public void onError(final CqEvent aCqEvent) {
            }

            @Override
            public void close() {
            }
        });
        CqAttributes cqa = factory.create();
        // Create the CqQuery
        CqQuery cq = qs.newCq("CQ1", query, cqa);
        System.out.println("query result: " + cq.executeWithInitialResults());
        cq.stop();
    });
}
Also used : CqEvent(org.apache.geode.cache.query.CqEvent) QueryService(org.apache.geode.cache.query.QueryService) CqAttributes(org.apache.geode.cache.query.CqAttributes) CqListener(org.apache.geode.cache.query.CqListener) SecurityTestUtil.createProxyRegion(org.apache.geode.security.SecurityTestUtil.createProxyRegion) Region(org.apache.geode.cache.Region) CqAttributesFactory(org.apache.geode.cache.query.CqAttributesFactory) Pool(org.apache.geode.cache.client.Pool) ClientCache(org.apache.geode.cache.client.ClientCache) SecurityTestUtil.createClientCache(org.apache.geode.security.SecurityTestUtil.createClientCache) CqQuery(org.apache.geode.cache.query.CqQuery) SecurityTest(org.apache.geode.test.junit.categories.SecurityTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 5 with CqListener

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

the class CqServiceImpl method invokeCqConnectedListeners.

private void invokeCqConnectedListeners(String cqName, ClientCQImpl cQuery, boolean connected) {
    if (!cQuery.isRunning() || cQuery.getCqAttributes() == null) {
        return;
    }
    cQuery.setConnected(connected);
    // invoke CQ Listeners.
    CqListener[] cqListeners = cQuery.getCqAttributes().getCqListeners();
    if (logger.isDebugEnabled()) {
        logger.debug("Invoking CQ status listeners for {}, number of listeners : {}", cqName, cqListeners.length);
    }
    for (int lCnt = 0; lCnt < cqListeners.length; lCnt++) {
        try {
            if (cqListeners[lCnt] != null) {
                if (cqListeners[lCnt] instanceof CqStatusListener) {
                    CqStatusListener listener = (CqStatusListener) cqListeners[lCnt];
                    if (connected) {
                        listener.onCqConnected();
                    } else {
                        listener.onCqDisconnected();
                    }
                }
            }
        // 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 (logger.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 (logger.isDebugEnabled()) {
                logger.debug(t.getMessage(), t);
            }
        }
    }
}
Also used : CqStatusListener(org.apache.geode.cache.query.CqStatusListener) CqListener(org.apache.geode.cache.query.CqListener) 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