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