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();
}
});
}
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;
}
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());
}
}
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();
});
}
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);
}
}
}
}
Aggregations