use of org.apache.geode.cache.query.CqResults in project geode by apache.
the class CQPDXPostProcessorDUnitTest method testCQ.
@Test
public void testCQ() {
String query = "select * from /" + REGION_NAME;
client1.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 CqListenerImpl() {
@Override
public void onEvent(final CqEvent aCqEvent) {
Object key = aCqEvent.getKey();
Object value = aCqEvent.getNewValue();
if (key.equals("key1")) {
assertTrue(value instanceof SimpleClass);
} else if (key.equals("key2")) {
assertTrue(Arrays.equals(BYTES, (byte[]) value));
}
}
});
CqAttributes cqa = factory.create();
// Create the CqQuery
CqQuery cq = qs.newCq("CQ1", query, cqa);
CqResults results = cq.executeWithInitialResults();
});
client2.invoke(() -> {
ClientCache cache = createClientCache("authRegionUser", "1234567", server.getPort());
Region region = createProxyRegion(cache, REGION_NAME);
region.put("key1", new SimpleClass(1, (byte) 1));
region.put("key2", BYTES);
});
// wait for events to fire
Awaitility.await().atMost(1, TimeUnit.SECONDS);
PDXPostProcessor pp = (PDXPostProcessor) SecurityService.getSecurityService().getPostProcessor();
assertEquals(pp.getCount(), 2);
}
use of org.apache.geode.cache.query.CqResults in project geode by apache.
the class CQPostProcessorDunitTest method testPostProcess.
@Test
public void testPostProcess() {
String query = "select * from /AuthRegion";
client1.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 CqListenerImpl() {
@Override
public void onEvent(final CqEvent aCqEvent) {
assertEquals("key6", aCqEvent.getKey());
assertEquals("super-user/AuthRegion/key6/value6", aCqEvent.getNewValue());
}
});
CqAttributes cqa = factory.create();
// Create the CqQuery
CqQuery cq = qs.newCq("CQ1", query, cqa);
CqResults results = cq.executeWithInitialResults();
assertEquals(5, results.size());
String resultString = results.toString();
assertTrue(resultString, resultString.contains("key:key0,value:super-user/null/key0/value0"));
assertTrue(resultString.contains("key:key1,value:super-user/null/key1/value1"));
assertTrue(resultString.contains("key:key2,value:super-user/null/key2/value2"));
assertTrue(resultString.contains("key:key3,value:super-user/null/key3/value3"));
assertTrue(resultString.contains("key:key4,value:super-user/null/key4/value4"));
});
client2.invoke(() -> {
ClientCache cache = createClientCache("authRegionUser", "1234567", server.getPort());
Region region = createProxyRegion(cache, REGION_NAME);
region.put("key6", "value6");
});
}
use of org.apache.geode.cache.query.CqResults in project geode by apache.
the class ClientCQImpl method executeWithInitialResults.
/**
* Start or resume executing the query. Gets or updates the CQ results and returns them.
*/
@Override
public <E> CqResults<E> executeWithInitialResults() throws CqClosedException, RegionNotFoundException, CqException {
synchronized (queuedEventsSynchObject) {
// until first call is completed.
while (queuedEvents != null) {
try {
queuedEventsSynchObject.wait();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
// At this point we know queuedEvents is null and no one is adding to queuedEvents yet.
this.queuedEvents = new ConcurrentLinkedQueue<CqEventImpl>();
}
if (CqQueryImpl.testHook != null) {
testHook.pauseUntilReady();
}
// Send CQ request to servers.
// If an exception is thrown, we need to clean up the queuedEvents
// or else client will hang on next executeWithInitialResults
CqResults initialResults;
try {
initialResults = (CqResults) executeCqOnRedundantsAndPrimary(true);
} catch (RegionNotFoundException | CqException | RuntimeException e) {
queuedEvents = null;
throw e;
}
// initial results can be added to the queue.
synchronized (queuedEventsSynchObject) {
// Invoke the CQ Listeners with the received CQ Events.
try {
if (!this.queuedEvents.isEmpty()) {
try {
Runnable r = new Runnable() {
@Override
public void run() {
Object[] eventArray = null;
if (CqQueryImpl.testHook != null) {
testHook.setEventCount(queuedEvents.size());
}
// Synchronization for the executer thread.
synchronized (queuedEventsSynchObject) {
try {
eventArray = queuedEvents.toArray();
// Process through the events
for (Object cqEvent : eventArray) {
cqService.invokeListeners(cqName, ClientCQImpl.this, (CqEventImpl) cqEvent);
stats.decQueuedCqListenerEvents();
}
} finally {
// Make sure that we notify waiting threads or else possible dead lock
queuedEvents.clear();
queuedEvents = null;
queuedEventsSynchObject.notify();
}
}
}
};
final LoggingThreadGroup group = LoggingThreadGroup.createThreadGroup("CQEventHandler", logger);
Thread thread = new Thread(group, r, "CQEventHandler For " + cqName);
thread.setDaemon(true);
thread.start();
} catch (Exception ex) {
if (logger.isDebugEnabled()) {
logger.debug("Exception while invoking the CQ Listener with queued events.", ex);
}
}
} else {
queuedEvents = null;
}
} finally {
queuedEventsSynchObject.notify();
}
return initialResults;
}
}
Aggregations