use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class GridCacheContinuousQueryReplicatedSelfTest method testCrossCallback.
/**
* Ensure that every node see every update.
*
* @throws Exception If failed.
*/
public void testCrossCallback() throws Exception {
// Prepare.
IgniteCache<Integer, Integer> cache1 = grid(0).cache(DEFAULT_CACHE_NAME);
IgniteCache<Integer, Integer> cache2 = grid(1).cache(DEFAULT_CACHE_NAME);
final int key1 = primaryKey(cache1);
final int key2 = primaryKey(cache2);
final CountDownLatch latch1 = new CountDownLatch(2);
final CountDownLatch latch2 = new CountDownLatch(2);
ContinuousQuery<Integer, Integer> qry1 = new ContinuousQuery<>();
qry1.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
log.info("Update in cache 1: " + evt);
if (evt.getKey() == key1 || evt.getKey() == key2)
latch1.countDown();
}
}
});
ContinuousQuery<Integer, Integer> qry2 = new ContinuousQuery<>();
qry2.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends Integer> evt : evts) {
log.info("Update in cache 2: " + evt);
if (evt.getKey() == key1 || evt.getKey() == key2)
latch2.countDown();
}
}
});
try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache2.query(qry1);
QueryCursor<Cache.Entry<Integer, Integer>> ignore = cache2.query(qry2)) {
cache1.put(key1, key1);
cache1.put(key2, key2);
assert latch1.await(LATCH_TIMEOUT, MILLISECONDS);
assert latch2.await(LATCH_TIMEOUT, MILLISECONDS);
}
}
use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testAllEntries.
/**
* @throws Exception If failed.
*/
public void testAllEntries() throws Exception {
IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
final Map<Integer, List<Integer>> map = new HashMap<>();
final CountDownLatch latch = new CountDownLatch(5);
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
for (CacheEntryEvent<? extends Integer, ? extends Integer> e : evts) {
synchronized (map) {
List<Integer> vals = map.get(e.getKey());
if (vals == null) {
vals = new ArrayList<>();
map.put(e.getKey(), vals);
}
vals.add(e.getValue());
}
latch.countDown();
}
}
});
try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
cache.put(1, 1);
cache.put(2, 2);
cache.put(3, 3);
cache.remove(2);
cache.put(1, 10);
assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
assertEquals(3, map.size());
List<Integer> vals = map.get(1);
assertNotNull(vals);
assertEquals(2, vals.size());
assertEquals(1, (int) vals.get(0));
assertEquals(10, (int) vals.get(1));
vals = map.get(2);
assertNotNull(vals);
assertEquals(2, vals.size());
assertEquals(2, (int) vals.get(0));
assertNull(vals.get(1));
vals = map.get(3);
assertNotNull(vals);
assertEquals(1, vals.size());
assertEquals(3, (int) vals.get(0));
}
}
use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testInternalKey.
/**
* @throws Exception If failed.
*/
public void testInternalKey() throws Exception {
if (atomicityMode() == ATOMIC)
return;
IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME);
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
final Map<Object, Object> map = new ConcurrentHashMap8<>();
final CountDownLatch latch = new CountDownLatch(2);
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent<?, ?> e : evts) {
map.put(e.getKey(), e.getValue());
latch.countDown();
}
}
});
try (QueryCursor<Cache.Entry<Object, Object>> ignored = cache.query(qry)) {
cache.put(new GridCacheInternalKeyImpl("test"), 1);
cache.put(1, 1);
cache.put(2, 2);
assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
assertEquals(2, map.size());
assertEquals(1, (int) map.get(1));
assertEquals(2, (int) map.get(2));
}
}
use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class PlatformUtils method applyContinuousQueryEvents.
/**
* Apply continuous query events to listener.
*
* @param ctx Context.
* @param lsnrPtr Listener pointer.
* @param evts Events.
* @throws javax.cache.event.CacheEntryListenerException In case of failure.
*/
public static void applyContinuousQueryEvents(PlatformContext ctx, long lsnrPtr, Iterable<CacheEntryEvent> evts) throws CacheEntryListenerException {
assert lsnrPtr != 0;
assert evts != null;
try (PlatformMemory mem = ctx.memory().allocate()) {
PlatformOutputStream out = mem.output();
BinaryRawWriterEx writer = ctx.writer(out);
writer.writeLong(lsnrPtr);
int cntPos = writer.reserveInt();
int cnt = 0;
for (CacheEntryEvent evt : evts) {
writeCacheEntryEvent(writer, evt);
cnt++;
}
writer.writeInt(cntPos, cnt);
out.synchronize();
ctx.gateway().continuousQueryListenerApply(mem.pointer());
} catch (Exception e) {
throw toCacheEntryListenerException(e);
}
}
use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class GridCacheContinuousQueryReplicatedTxOneNodeTest method doTest.
/**
* @throws Exception If failed.
*/
private void doTest(boolean loc) throws Exception {
try {
IgniteCache<String, Integer> cache = startGrid(0).cache(DEFAULT_CACHE_NAME);
ContinuousQuery<String, Integer> qry = new ContinuousQuery<>();
final AtomicInteger cnt = new AtomicInteger();
final CountDownLatch latch = new CountDownLatch(10);
qry.setLocalListener(new CacheEntryUpdatedListener<String, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends String, ? extends Integer>> evts) throws CacheEntryListenerException {
for (CacheEntryEvent<? extends String, ? extends Integer> evt : evts) {
cnt.incrementAndGet();
latch.countDown();
}
}
});
cache.query(qry.setLocal(loc));
startGrid(1);
awaitPartitionMapExchange();
for (int i = 0; i < 10; i++) cache.put("key" + i, i);
assert latch.await(5000, TimeUnit.MILLISECONDS);
assertEquals(10, cnt.get());
} finally {
stopAllGrids();
}
}
Aggregations