Search in sources :

Example 6 with CompletionListener

use of javax.cache.integration.CompletionListener in project caffeine by ben-manes.

the class CacheProxy method loadAll.

@Override
public void loadAll(Set<? extends K> keys, boolean replaceExistingValues, CompletionListener completionListener) {
    requireNotClosed();
    keys.forEach(Objects::requireNonNull);
    CompletionListener listener = (completionListener == null) ? NullCompletionListener.INSTANCE : completionListener;
    if (!cacheLoader.isPresent()) {
        listener.onCompletion();
        return;
    }
    executor.execute(() -> {
        try {
            if (replaceExistingValues) {
                loadAllAndReplaceExisting(keys);
            } else {
                loadAllAndKeepExisting(keys);
            }
            listener.onCompletion();
        } catch (CacheLoaderException e) {
            listener.onException(e);
        } catch (Exception e) {
            listener.onException(new CacheLoaderException(e));
        } finally {
            dispatcher.ignoreSynchronous();
        }
    });
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) CacheLoaderException(javax.cache.integration.CacheLoaderException) Objects(java.util.Objects) EntryProcessorException(javax.cache.processor.EntryProcessorException) CacheLoaderException(javax.cache.integration.CacheLoaderException) NoSuchElementException(java.util.NoSuchElementException) CacheWriterException(javax.cache.integration.CacheWriterException)

Example 7 with CompletionListener

use of javax.cache.integration.CompletionListener in project hazelcast by hazelcast.

the class ClientNearCacheTestSupport method testLoadAllNearCacheInvalidation.

protected void testLoadAllNearCacheInvalidation(InMemoryFormat inMemoryFormat) throws Exception {
    NearCacheConfig nearCacheConfig = createNearCacheConfig(inMemoryFormat);
    nearCacheConfig.setInvalidateOnChange(true);
    NearCacheTestContext nearCacheTestContext1 = createNearCacheTest(DEFAULT_CACHE_NAME, nearCacheConfig);
    final NearCacheTestContext nearCacheTestContext2 = createNearCacheTest(DEFAULT_CACHE_NAME, nearCacheConfig);
    Set<Integer> testKeys = new HashSet<Integer>(DEFAULT_RECORD_COUNT);
    Set<Integer> loadKeys = new HashSet<Integer>(DEFAULT_RECORD_COUNT / 2);
    for (int i = 0; i < DEFAULT_RECORD_COUNT; i++) {
        if (i % 2 == 0) {
            loadKeys.add(i);
        }
        testKeys.add(i);
    }
    // populate cache from client-1
    for (int i : testKeys) {
        nearCacheTestContext1.cache.put(i, generateValueFromKey(i));
    }
    final CountDownLatch completed = new CountDownLatch(1);
    nearCacheTestContext1.cache.loadAll(loadKeys, true, new CompletionListener() {

        @Override
        public void onCompletion() {
            completed.countDown();
        }

        @Override
        public void onException(Exception e) {
        }
    });
    completed.await(3, TimeUnit.SECONDS);
    // can't get replaced keys from client-2
    for (int i : loadKeys) {
        final int key = i;
        // records are stored in the Near Cache will be invalidated eventually, since cache records are updated
        assertTrueEventually(new AssertTask() {

            @Override
            public void run() throws Exception {
                Data keyData = nearCacheTestContext2.serializationService.toData(key);
                assertNull(nearCacheTestContext2.nearCache.get(keyData));
            }
        });
    }
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) NearCacheConfig(com.hazelcast.config.NearCacheConfig) AssertTask(com.hazelcast.test.AssertTask) Data(com.hazelcast.nio.serialization.Data) CountDownLatch(java.util.concurrent.CountDownLatch) CacheLoaderException(javax.cache.integration.CacheLoaderException) HashSet(java.util.HashSet)

Example 8 with CompletionListener

use of javax.cache.integration.CompletionListener in project hazelcast by hazelcast.

the class AbstractClientCacheProxyBase method waitOnGoingLoadAllCallsToFinish.

private void waitOnGoingLoadAllCallsToFinish() {
    Iterator<Map.Entry<Future, CompletionListener>> iterator = loadAllCalls.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<Future, CompletionListener> entry = iterator.next();
        Future f = entry.getKey();
        CompletionListener completionListener = entry.getValue();
        try {
            f.get(TIMEOUT, TimeUnit.SECONDS);
        } catch (Throwable t) {
            logger.finest("Error occurred at loadAll operation execution while waiting it to finish on cache close!", t);
            handleFailureOnCompletionListener(completionListener, t);
        }
        iterator.remove();
    }
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) ClientDelegatingFuture(com.hazelcast.client.util.ClientDelegatingFuture) Future(java.util.concurrent.Future) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 9 with CompletionListener

use of javax.cache.integration.CompletionListener in project cache2k by cache2k.

the class CacheLoaderTest method testLoadAllWithExecptionAndNoCompletionListener.

/**
 * Added for code coverage.
 */
@Test
public void testLoadAllWithExecptionAndNoCompletionListener() throws Exception {
    FailingCacheLoader<String, String> cacheLoader = new FailingCacheLoader<>();
    cacheLoaderServer.setCacheLoader(cacheLoader);
    HashSet<String> keys = new HashSet<>();
    keys.add("gudday");
    keys.add("hello");
    keys.add("howdy");
    keys.add("bonjour");
    CompletionListener NULL_COMPLETION_LISTENER = null;
    cache.loadAll(keys, false, NULL_COMPLETION_LISTENER);
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 10 with CompletionListener

use of javax.cache.integration.CompletionListener in project hazelcast by hazelcast.

the class ClientCacheProxySupport method waitOnGoingLoadAllCallsToFinish.

private void waitOnGoingLoadAllCallsToFinish() {
    Iterator<Map.Entry<Future, CompletionListener>> iterator = loadAllCalls.entrySet().iterator();
    while (iterator.hasNext()) {
        Map.Entry<Future, CompletionListener> entry = iterator.next();
        Future future = entry.getKey();
        CompletionListener completionListener = entry.getValue();
        try {
            future.get(TIMEOUT, TimeUnit.SECONDS);
        } catch (Throwable t) {
            logger.finest("Error occurred at loadAll operation execution while waiting it to finish on cache close!", t);
            handleFailureOnCompletionListener(completionListener, t);
        }
        iterator.remove();
    }
}
Also used : ClientCacheProxySupportUtil.handleFailureOnCompletionListener(com.hazelcast.client.cache.impl.ClientCacheProxySupportUtil.handleFailureOnCompletionListener) EmptyCompletionListener(com.hazelcast.client.cache.impl.ClientCacheProxySupportUtil.EmptyCompletionListener) CompletionListener(javax.cache.integration.CompletionListener) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Future(java.util.concurrent.Future) ClientInvocationFuture(com.hazelcast.client.impl.spi.impl.ClientInvocationFuture) CompletableFuture(java.util.concurrent.CompletableFuture) ClientDelegatingFuture(com.hazelcast.client.impl.ClientDelegatingFuture) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) AbstractMap(java.util.AbstractMap)

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