use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testNodeJoinWithoutCache.
/**
* @throws Exception If failed.
*/
@SuppressWarnings("TryFinallyCanBeTryWithResources")
public void testNodeJoinWithoutCache() throws Exception {
IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
final CountDownLatch latch = new CountDownLatch(1);
qry.setLocalListener(new CacheEntryUpdatedListener<Integer, Integer>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends Integer>> evts) {
latch.countDown();
}
});
QueryCursor<Cache.Entry<Integer, Integer>> cur = cache.query(qry);
try {
try (Ignite ignite = startGrid(NO_CACHE_IGNITE_INSTANCE_NAME)) {
log.info("Started node without cache: " + ignite);
}
cache.put(1, 1);
assertTrue(latch.await(5000, MILLISECONDS));
} finally {
cur.close();
}
}
use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class GridCacheContinuousQueryReplicatedTxOneNodeTest method doTestOneNode.
/**
* @throws Exception If failed.
*/
private void doTestOneNode(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);
for (int i = 0; i < 10; i++) cache.put("key" + i, i);
cache.clear();
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));
for (int i = 0; i < 10; i++) cache.put("key" + i, i);
assert latch.await(5000, TimeUnit.MILLISECONDS);
assertEquals(10, cnt.get());
} finally {
stopAllGrids();
}
}
use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testBuffering.
/**
* @throws Exception If failed.
*/
public void testBuffering() throws Exception {
if (grid(0).cache(DEFAULT_CACHE_NAME).getConfiguration(CacheConfiguration.class).getCacheMode() != PARTITIONED)
return;
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();
}
}
});
qry.setPageSize(5);
try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
ClusterNode node = F.first(grid(0).cluster().forRemotes().nodes());
Collection<Integer> keys = new HashSet<>();
int key = 0;
while (true) {
ClusterNode n = grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key);
assert n != null;
if (n.equals(node))
keys.add(key);
key++;
if (keys.size() == 6)
break;
}
Iterator<Integer> it = keys.iterator();
for (int i = 0; i < 4; i++) cache.put(it.next(), 0);
assert !latch.await(2, SECONDS);
for (int i = 0; i < 2; i++) cache.put(it.next(), 0);
assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
assertEquals(5, map.size());
it = keys.iterator();
for (int i = 0; i < 5; i++) {
Integer k = it.next();
List<Integer> vals = map.get(k);
assertNotNull(vals);
assertEquals(1, vals.size());
assertEquals(0, (int) vals.get(0));
}
}
}
use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testTimeInterval.
/**
* @throws Exception If failed.
*/
public void testTimeInterval() throws Exception {
IgniteCache<Integer, Integer> cache = grid(0).cache(DEFAULT_CACHE_NAME);
if (cache.getConfiguration(CacheConfiguration.class).getCacheMode() != PARTITIONED)
return;
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();
}
}
});
qry.setPageSize(10);
qry.setTimeInterval(3000);
try (QueryCursor<Cache.Entry<Integer, Integer>> ignored = cache.query(qry)) {
ClusterNode node = F.first(grid(0).cluster().forRemotes().nodes());
Collection<Integer> keys = new HashSet<>();
int key = 0;
while (true) {
ClusterNode n = grid(0).affinity(DEFAULT_CACHE_NAME).mapKeyToNode(key);
assert n != null;
if (n.equals(node))
keys.add(key);
key++;
if (keys.size() == 5)
break;
}
for (Integer k : keys) cache.put(k, 0);
assert !latch.await(2, SECONDS);
assert latch.await(1000 + LATCH_TIMEOUT, MILLISECONDS);
assertEquals(5, map.size());
Iterator<Integer> it = keys.iterator();
for (int i = 0; i < 5; i++) {
Integer k = it.next();
List<Integer> vals = map.get(k);
assertNotNull(vals);
assertEquals(1, vals.size());
assertEquals(0, (int) vals.get(0));
}
}
}
use of javax.cache.event.CacheEntryEvent in project ignite by apache.
the class GridCacheContinuousQueryAbstractSelfTest method testExpired.
/**
* @throws Exception If failed.
*/
public void testExpired() throws Exception {
IgniteCache<Object, Object> cache = grid(0).cache(DEFAULT_CACHE_NAME).withExpiryPolicy(new CreatedExpiryPolicy(new Duration(MILLISECONDS, 1000)));
final Map<Object, Object> map = new ConcurrentHashMap8<>();
final CountDownLatch latch = new CountDownLatch(2);
ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
qry.setIncludeExpired(true);
qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {
@Override
public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
for (CacheEntryEvent<?, ?> e : evts) {
if (e.getEventType() == EventType.EXPIRED) {
assertNull(e.getValue());
map.put(e.getKey(), e.getOldValue());
latch.countDown();
}
}
}
});
try (QueryCursor<Cache.Entry<Object, Object>> ignored = cache.query(qry)) {
cache.put(1, 1);
cache.put(2, 2);
// Wait for expiration.
Thread.sleep(2000);
assert latch.await(LATCH_TIMEOUT, MILLISECONDS);
assertEquals(2, map.size());
assertEquals(1, (int) map.get(1));
assertEquals(2, (int) map.get(2));
}
}
Aggregations