Search in sources :

Example 1 with CompletionListener

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

the class LoadingCacheProxy 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;
    executor.execute(() -> {
        try {
            if (replaceExistingValues) {
                int[] ignored = { 0 };
                Map<K, V> loaded = cacheLoader.get().loadAll(keys);
                for (Map.Entry<? extends K, ? extends V> entry : loaded.entrySet()) {
                    putNoCopyOrAwait(entry.getKey(), entry.getValue(), /* publishToWriter */
                    false, ignored);
                }
            } else {
                getAll(keys, /* updateAccessTime */
                false);
            }
            listener.onCompletion();
        } catch (Exception e) {
            listener.onException(e);
        } finally {
            dispatcher.ignoreSynchronous();
        }
    });
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) Objects(java.util.Objects) Map(java.util.Map) CacheException(javax.cache.CacheException)

Example 2 with CompletionListener

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

the class AbstractClientCacheProxyBase method submitLoadAllTask.

protected void submitLoadAllTask(ClientMessage request, CompletionListener completionListener, final Set<Data> keys) {
    final CompletionListener compListener = completionListener != null ? completionListener : NULL_COMPLETION_LISTENER;
    ClientDelegatingFuture<V> delegatingFuture = null;
    try {
        injectDependencies(completionListener);
        final long start = System.nanoTime();
        ClientInvocationFuture future = new ClientInvocation((HazelcastClientInstanceImpl) clientContext.getHazelcastInstance(), request).invoke();
        SerializationService serializationService = clientContext.getSerializationService();
        delegatingFuture = new ClientDelegatingFuture<V>(future, serializationService, LOAD_ALL_DECODER);
        final Future delFuture = delegatingFuture;
        loadAllCalls.put(delegatingFuture, compListener);
        delegatingFuture.andThen(new ExecutionCallback<V>() {

            @Override
            public void onResponse(V response) {
                loadAllCalls.remove(delFuture);
                onLoadAll(keys, response, start, System.nanoTime());
                compListener.onCompletion();
            }

            @Override
            public void onFailure(Throwable t) {
                loadAllCalls.remove(delFuture);
                handleFailureOnCompletionListener(compListener, t);
            }
        });
    } catch (Throwable t) {
        if (delegatingFuture != null) {
            loadAllCalls.remove(delegatingFuture);
        }
        handleFailureOnCompletionListener(compListener, t);
    }
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) SerializationService(com.hazelcast.spi.serialization.SerializationService) ClientDelegatingFuture(com.hazelcast.client.util.ClientDelegatingFuture) Future(java.util.concurrent.Future) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture) ClientInvocation(com.hazelcast.client.spi.impl.ClientInvocation) ClientInvocationFuture(com.hazelcast.client.spi.impl.ClientInvocationFuture)

Example 3 with CompletionListener

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

the class CacheTest method load_noLoaderNoCompletionListener.

@Test
public void load_noLoaderNoCompletionListener() {
    // Added for code coverage.
    Set<Long> keys = new HashSet<Long>();
    keys.add(1L);
    CompletionListener NULL_COMPLETION_LISTENER = null;
    cache.loadAll(keys, true, NULL_COMPLETION_LISTENER);
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 4 with CompletionListener

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

the class ClientCacheProxySupport method submitLoadAllTask.

private void submitLoadAllTask(ClientMessage request, CompletionListener completionListener, final List<Data> binaryKeys) {
    final CompletionListener listener = completionListener != null ? injectDependencies(completionListener) : NULL_COMPLETION_LISTENER;
    ClientDelegatingFuture<V> delegatingFuture = null;
    try {
        final long startNanos = nowInNanosOrDefault();
        ClientInvocationFuture future = new ClientInvocation(getClient(), request, getName()).invoke();
        delegatingFuture = newDelegatingFuture(future, clientMessage -> Boolean.TRUE);
        final Future delFuture = delegatingFuture;
        loadAllCalls.put(delegatingFuture, listener);
        delegatingFuture.whenCompleteAsync((response, t) -> {
            if (t == null) {
                loadAllCalls.remove(delFuture);
                onLoadAll(binaryKeys, startNanos);
                listener.onCompletion();
            } else {
                loadAllCalls.remove(delFuture);
                handleFailureOnCompletionListener(listener, t);
            }
        });
    } catch (Throwable t) {
        if (delegatingFuture != null) {
            loadAllCalls.remove(delegatingFuture);
        }
        handleFailureOnCompletionListener(listener, t);
    }
}
Also used : ExceptionUtil.rethrowAllowedTypeFirst(com.hazelcast.internal.util.ExceptionUtil.rethrowAllowedTypeFirst) Member(com.hazelcast.cluster.Member) CachePutAllCodec(com.hazelcast.client.impl.protocol.codec.CachePutAllCodec) ExceptionUtil.rethrow(com.hazelcast.internal.util.ExceptionUtil.rethrow) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) Future(java.util.concurrent.Future) CacheSetExpiryPolicyCodec(com.hazelcast.client.impl.protocol.codec.CacheSetExpiryPolicyCodec) ICacheService(com.hazelcast.cache.impl.ICacheService) CacheEntryListenerConfiguration(javax.cache.configuration.CacheEntryListenerConfiguration) NULL_KEY_IS_NOT_ALLOWED(com.hazelcast.cache.impl.CacheProxyUtil.NULL_KEY_IS_NOT_ALLOWED) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CacheRemoveAllKeysCodec(com.hazelcast.client.impl.protocol.codec.CacheRemoveAllKeysCodec) Map(java.util.Map) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientCacheProxySupportUtil.getSafely(com.hazelcast.client.cache.impl.ClientCacheProxySupportUtil.getSafely) HazelcastCacheManager(com.hazelcast.cache.HazelcastCacheManager) CacheRemoveAllCodec(com.hazelcast.client.impl.protocol.codec.CacheRemoveAllCodec) CacheProxyUtil.validateNotNull(com.hazelcast.cache.impl.CacheProxyUtil.validateNotNull) CacheProxyUtil.validateConfiguredTypes(com.hazelcast.cache.impl.CacheProxyUtil.validateConfiguredTypes) ClientCacheProxySupportUtil.addCallback(com.hazelcast.client.cache.impl.ClientCacheProxySupportUtil.addCallback) NearCachingHook(com.hazelcast.internal.nearcache.impl.NearCachingHook) ClientPartitionService(com.hazelcast.client.impl.spi.ClientPartitionService) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CacheConfig(com.hazelcast.config.CacheConfig) ExceptionUtil(com.hazelcast.internal.util.ExceptionUtil) Set(java.util.Set) UUID(java.util.UUID) CacheGetAndReplaceCodec(com.hazelcast.client.impl.protocol.codec.CacheGetAndReplaceCodec) CacheListenerRegistrationCodec(com.hazelcast.client.impl.protocol.codec.CacheListenerRegistrationCodec) ClientCacheProxySupportUtil.handleFailureOnCompletionListener(com.hazelcast.client.cache.impl.ClientCacheProxySupportUtil.handleFailureOnCompletionListener) List(java.util.List) ICacheInternal(com.hazelcast.cache.impl.ICacheInternal) ClientInvocationFuture(com.hazelcast.client.impl.spi.impl.ClientInvocationFuture) CacheManager(javax.cache.CacheManager) CachePutIfAbsentCodec(com.hazelcast.client.impl.protocol.codec.CachePutIfAbsentCodec) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) CacheGetCodec(com.hazelcast.client.impl.protocol.codec.CacheGetCodec) CacheEventListenerAdaptor(com.hazelcast.cache.impl.CacheEventListenerAdaptor) CacheEntryProcessorCodec(com.hazelcast.client.impl.protocol.codec.CacheEntryProcessorCodec) ManagedContext(com.hazelcast.core.ManagedContext) FutureUtil(com.hazelcast.internal.util.FutureUtil) CacheContainsKeyCodec(com.hazelcast.client.impl.protocol.codec.CacheContainsKeyCodec) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CollectionUtil.objectToDataCollection(com.hazelcast.internal.util.CollectionUtil.objectToDataCollection) CompletableFuture(java.util.concurrent.CompletableFuture) EmptyCompletionListener(com.hazelcast.client.cache.impl.ClientCacheProxySupportUtil.EmptyCompletionListener) ClientListenerService(com.hazelcast.client.impl.spi.ClientListenerService) EntryProcessorException(javax.cache.processor.EntryProcessorException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) CacheSyncListenerCompleter(com.hazelcast.cache.impl.CacheSyncListenerCompleter) ExceptionUtil.sneakyThrow(com.hazelcast.internal.util.ExceptionUtil.sneakyThrow) ILogger(com.hazelcast.logging.ILogger) CacheClearCodec(com.hazelcast.client.impl.protocol.codec.CacheClearCodec) CacheEntryListener(javax.cache.event.CacheEntryListener) ClientContext(com.hazelcast.client.impl.spi.ClientContext) BiConsumer(java.util.function.BiConsumer) ClientMessageDecoder(com.hazelcast.client.impl.clientside.ClientMessageDecoder) CacheGetAndRemoveCodec(com.hazelcast.client.impl.protocol.codec.CacheGetAndRemoveCodec) CacheException(javax.cache.CacheException) CachePutCodec(com.hazelcast.client.impl.protocol.codec.CachePutCodec) Nonnull(javax.annotation.Nonnull) IGNORE_COMPLETION(com.hazelcast.cache.impl.operation.MutableOperation.IGNORE_COMPLETION) Timer(com.hazelcast.internal.util.Timer) IOUtil(com.hazelcast.internal.nio.IOUtil) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) Iterator(java.util.Iterator) Data(com.hazelcast.internal.serialization.Data) CompletionListener(javax.cache.integration.CompletionListener) CacheReplaceCodec(com.hazelcast.client.impl.protocol.codec.CacheReplaceCodec) CacheGetAllCodec(com.hazelcast.client.impl.protocol.codec.CacheGetAllCodec) ClientProxy(com.hazelcast.client.impl.spi.ClientProxy) CacheRemoveCodec(com.hazelcast.client.impl.protocol.codec.CacheRemoveCodec) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) AbstractMap(java.util.AbstractMap) ClientDelegatingFuture(com.hazelcast.client.impl.ClientDelegatingFuture) Closeable(java.io.Closeable) CacheLoadAllCodec(com.hazelcast.client.impl.protocol.codec.CacheLoadAllCodec) Collections(java.util.Collections) 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) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) ClientInvocationFuture(com.hazelcast.client.impl.spi.impl.ClientInvocationFuture)

Example 5 with CompletionListener

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

the class CacheLoadAllTest method testLoadAll.

@Test
public void testLoadAll() throws InterruptedException {
    ICache<String, String> cache = createCache();
    String cacheName = cache.getName();
    Map<String, String> entries = createAndFillEntries();
    final CountDownLatch latch = new CountDownLatch(1);
    cache.loadAll(entries.keySet(), true, new CompletionListener() {

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

        @Override
        public void onException(Exception e) {
            latch.countDown();
        }
    });
    assertTrue(latch.await(60, TimeUnit.SECONDS));
    // Verify that load-all works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        String actualValue = cache.get(key);
        assertEquals(expectedValue, actualValue);
    }
    Node node = getNode(hazelcastInstance);
    InternalPartitionService partitionService = node.getPartitionService();
    SerializationService serializationService = node.getSerializationService();
    // Verify that backup of load-all works
    for (Map.Entry<String, String> entry : entries.entrySet()) {
        String key = entry.getKey();
        String expectedValue = entries.get(key);
        Data keyData = serializationService.toData(key);
        int keyPartitionId = partitionService.getPartitionId(keyData);
        for (int i = 0; i < INSTANCE_COUNT; i++) {
            Node n = getNode(hazelcastInstances[i]);
            ICacheService cacheService = n.getNodeEngine().getService(ICacheService.SERVICE_NAME);
            ICacheRecordStore recordStore = cacheService.getRecordStore("/hz/" + cacheName, keyPartitionId);
            assertNotNull(recordStore);
            String actualValue = serializationService.toObject(recordStore.get(keyData, null));
            assertEquals(expectedValue, actualValue);
        }
    }
}
Also used : CompletionListener(javax.cache.integration.CompletionListener) InternalPartitionService(com.hazelcast.internal.partition.InternalPartitionService) Accessors.getNode(com.hazelcast.test.Accessors.getNode) Node(com.hazelcast.instance.impl.Node) SerializationService(com.hazelcast.internal.serialization.SerializationService) Data(com.hazelcast.internal.serialization.Data) ICacheRecordStore(com.hazelcast.cache.impl.ICacheRecordStore) CountDownLatch(java.util.concurrent.CountDownLatch) CacheLoaderException(javax.cache.integration.CacheLoaderException) ICacheService(com.hazelcast.cache.impl.ICacheService) HashMap(java.util.HashMap) Map(java.util.Map) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

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