Search in sources :

Example 11 with CompletionListener

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());
    }
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) CountDownLatch(java.util.concurrent.CountDownLatch) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheWriterException(javax.cache.integration.CacheWriterException) CacheManager(javax.cache.CacheManager) HashSet(java.util.HashSet)

Example 12 with CompletionListener

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));
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) CacheException(javax.cache.CacheException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) CacheLoaderException(javax.cache.integration.CacheLoaderException) CacheException(javax.cache.CacheException) CacheWriterException(javax.cache.integration.CacheWriterException) IgniteEx(org.apache.ignite.internal.IgniteEx) HashSet(java.util.HashSet) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 13 with CompletionListener

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();
        }
    });
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) IgniteCache(org.apache.ignite.IgniteCache) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) TransactionRollbackException(org.apache.ignite.transactions.TransactionRollbackException) IgniteException(org.apache.ignite.IgniteException) ClusterTopologyException(org.apache.ignite.cluster.ClusterTopologyException) CacheException(javax.cache.CacheException) Ignite(org.apache.ignite.Ignite)

Aggregations

CompletionListener (javax.cache.integration.CompletionListener)13 Map (java.util.Map)5 HashSet (java.util.HashSet)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 Future (java.util.concurrent.Future)4 CacheException (javax.cache.CacheException)4 CacheLoaderException (javax.cache.integration.CacheLoaderException)4 Test (org.junit.Test)4 CacheWriterException (javax.cache.integration.CacheWriterException)3 ICacheService (com.hazelcast.cache.impl.ICacheService)2 EmptyCompletionListener (com.hazelcast.client.cache.impl.ClientCacheProxySupportUtil.EmptyCompletionListener)2 ClientCacheProxySupportUtil.handleFailureOnCompletionListener (com.hazelcast.client.cache.impl.ClientCacheProxySupportUtil.handleFailureOnCompletionListener)2 ClientDelegatingFuture (com.hazelcast.client.impl.ClientDelegatingFuture)2 Data (com.hazelcast.internal.serialization.Data)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 HazelcastCacheManager (com.hazelcast.cache.HazelcastCacheManager)1 CacheEventListenerAdaptor (com.hazelcast.cache.impl.CacheEventListenerAdaptor)1 NULL_KEY_IS_NOT_ALLOWED (com.hazelcast.cache.impl.CacheProxyUtil.NULL_KEY_IS_NOT_ALLOWED)1