use of javax.cache.integration.CompletionListener in project hazelcast by hazelcast.
the class CacheReadWriteThroughTest method loadAll_readThrough.
private void loadAll_readThrough(boolean throwError) throws Exception {
final String cacheName = randomName();
CacheManager cacheManager = cachingProvider.getCacheManager();
assertNotNull(cacheManager);
assertNull(cacheManager.getCache(cacheName));
CacheConfig<Integer, Integer> config = createCacheConfig();
config.setReadThrough(true);
config.setCacheLoaderFactory(FactoryBuilder.factoryOf(new GetAllAsyncCacheLoader(throwError)));
Cache<Integer, Integer> cache = cacheManager.createCache(cacheName, config);
assertNotNull(cache);
Set<Integer> keys = new HashSet<Integer>();
for (int i = 0; i < 150; i++) {
keys.add(i);
}
final CountDownLatch latch = new CountDownLatch(1);
cache.loadAll(keys, false, new CompletionListener() {
@Override
public void onCompletion() {
latch.countDown();
}
@Override
public void onException(Exception e) {
e.printStackTrace();
latch.countDown();
}
});
assertOpenEventually(latch);
if (!throwError) {
assertEquals(100, cache.unwrap(ICache.class).size());
}
}
use of javax.cache.integration.CompletionListener in project ignite by apache.
the class DataStreamerStopCacheTest method testLoadAllAndCacheStop.
/**
* Tests that stopping a cache does not lead to a deadlock while loading data through DataStreamer.
*
* @throws Exception if failed.
*/
@Test
public void testLoadAllAndCacheStop() throws Exception {
final AtomicReference<Exception> fail = new AtomicReference<>();
final IgniteEx crd = startGrid(0);
final IgniteEx node1 = startGrid(1);
IgniteCache<Integer, String> c = node1.getOrCreateCache(DEFAULT_CACHE_NAME);
awaitPartitionMapExchange();
Set<Integer> keys = new HashSet<>();
for (int i = 0; i < PART_NUM; ++i) {
if (node1.affinity(DEFAULT_CACHE_NAME).isPrimary(node1.localNode(), i)) {
keys.add(i);
break;
}
}
final CountDownLatch loadFinished = new CountDownLatch(1);
GridTestUtils.runAsync(() -> {
c.loadAll(keys, true, new CompletionListener() {
@Override
public void onCompletion() {
loadFinished.countDown();
}
@Override
public void onException(Exception e) {
fail.compareAndSet(null, e);
loadFinished.countDown();
}
});
});
assertTrue("loadAll() has not finished in " + TIMEOUT + " millis", loadFinished.await(TIMEOUT, TimeUnit.MILLISECONDS));
assertTrue("Expected CacheException is not thrown", X.hasCause(fail.get(), CacheException.class));
}
use of javax.cache.integration.CompletionListener in project ignite by apache.
the class GridCommonAbstractTest method loadAll.
/**
* @param cache Cache.
* @param keys Keys.
* @param replaceExistingValues Replace existing values.
* @throws Exception If failed.
*/
protected static <K> void loadAll(Cache<K, ?> cache, final Set<K> keys, final boolean replaceExistingValues) throws Exception {
IgniteCache<K, Object> cacheCp = (IgniteCache<K, Object>) cache;
GridAbstractTest.executeOnLocalOrRemoteJvm(cacheCp, new TestCacheRunnable<K, Object>() {
private static final long serialVersionUID = -3030833765012500545L;
@Override
public void run(Ignite ignite, IgniteCache<K, Object> cache) throws Exception {
final AtomicReference<Exception> ex = new AtomicReference<>();
final CountDownLatch latch = new CountDownLatch(1);
cache.loadAll(keys, replaceExistingValues, new CompletionListener() {
@Override
public void onCompletion() {
latch.countDown();
}
@Override
public void onException(Exception e) {
ex.set(e);
latch.countDown();
}
});
latch.await();
if (ex.get() != null)
throw ex.get();
}
});
}
Aggregations