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